2004-07-16

Linux的对用户的限制

俗话说“没有规距不成方圆”,可是这个世界上总有些不守规距的人。也许他会在公用服务器上运行一个大程序把内存吃光;也许他会把无聊的电影文件也放到文件服务器上,把其它人的空间也抢占了;也许……,遇到这样的人光是口头说教也许并没有效果反而会引发人际关系的紧张,怎么办呢?

一、/etc/security/limits.conf的限制
在limits.conf中限制,限制用户的进程数和使用的内存数量,打开文件数等…防止用户把服务器的资源消耗过多,影响其他用户。


[代码]:

[root@XiShi root]# cat /etc/security/limits.conf
# /etc/security/limits.conf
#
#Each line describes a limit for a user in the form:
#
#<domain> <type> <item> <value>
#
#Where:
#<domain> can be:
# - an user name
# - a group name, with @group syntax
# - the wildcard *, for default entry
#
#<type> can have the two values:
# - "soft" for enforcing the soft limits
# - "hard" for enforcing hard limits
#
#<item> can be one of the following:
# - core - limits the core file size (KB)
# - data - max data size (KB)
# - fsize - maximum filesize (KB)
# - memlock - max locked-in-memory address space (KB)
# - nofile - max number of open files
# - rss - max resident set size (KB)
# - stack - max stack size (KB)
# - cpu - max CPU time (MIN)
# - nproc - max number of processes
# - as - address space limit
# - maxlogins - max number of logins for this user
# - priority - the priority to run user process with
# - locks - max number of file locks the user can hold
#
#<domain> <type> <item> <value>
#

#* soft core 0
#* hard rss 10000
#@student hard nproc 20
#@faculty soft nproc 20
#@faculty hard nproc 50
#ftp hard nproc 0
#@student - maxlogins 4

# End of file

上面是默认的配置文件,简单解释一下:
#<item> can be one of the following:
# - core - limits the core file size (KB) 限制core文件的大小(以KB为单位);


# - data - max data size (KB) 进程数据段的最大值(以KB为单位);
# - fsize - maximum filesize (KB) 文件的最大值(以KB为单位);
# - memlock - max locked-in-memory address space (KB) 最大的加锁内存空间(单位KB);
# - nofile - max number of open files 打开文件的最大数量;
# - rss - max resident set size (KB) RSS(Resident Set Size)的最大值(单位KB);
# - stack - max stack size (KB) 最大的栈值(以KB为单位);
# - cpu - max CPU time (MIN) 最大的CPU时间;
# - nproc - max number of processes 用户的最大进程数量;
# - as - address space limit 地址空间限制;
# - maxlogins - max number of logins for this user 这个用户最大的登录数;
# - priority - the priority to run user process with 用户进程的优先级;
# - locks - max number of file locks the user can hold 用户最大锁定文件的数量



这个文件针对除了root外的所有用户,更改完成后,用户重新登录即生效。不过,要让这个配置文件生效,要先在/etc/pam.d/login中加入一句:
session required /lib/security/pam_limits.so


二、磁盘配额
可有效解决滥用磁盘的问题。



三、账户管理策略
如果还有个别用户特别不听话,也可以暂时关闭其用户。最常用的办法就是在/etc/shadow该用户的密码部分加上一个*号,例如:








代码:
test:$1J$*Xvek6Mw$yljNnoouX5VGlWFSNUcPJ0:12576:0:99999:7:::


不过,你最好让他知道为什么被封了,否则……


我们可以写一个文件代替他的默认shell,例/bin/lockaccount
内容如下:










代码:
#!/usr/bin/tail +1
This account has been locked because ……

然后chmod +x /bin/lockaccount,然后vipw,找到该用户的那一行,把他的shell改成/bin/lockaccount
例:










代码:
test:x:1005:1005:/home/test:/bin/lockaccount

没有评论:

发表评论