非root用户只能越设置越小,不能越设置越大
我在机器上以非root先执行:
[wxx@br162 etc]$ ulimit -n 900
[wxx@br162 etc]$ 执行成功,再增大:
[wxx@br162 etc]$ ulimit -n 901-bash: ulimit: open files: cannot modify limit: Operation not permitted [wxx@br162 etc]$
增加失败,如果减少则是OK的:
[wxx@br162 etc]$ ulimit -n 899[wxx@br162 etc]$
如果再增加到900是不行的:
[wxx@br162 etc]$ ulimit -n 900-bash: ulimit: open files: cannot modify limit: Operation not permitted root用户不受限制
首先切换到root:
[wxx@br162 etc]$ sudo su -
[root@br162 ~]# 查看下当前限制:
[root@br162 ~]# ulimit -n1000000 [root@br162 ~]#
增大:
[root@br162 ~]# ulimit -n 1000001
[root@br162 ~]#
可以成功增大,再减小:
[root@br162 ~]# ulimit -n 999999[root@br162 ~]#
减小也是成功的,再增大:
[root@br162 ~]# ulimit -n 1000002
[root@br162 ~]#
也是ok的,可见root是不受限制的。
ulimit里的最大文件打开数量的默认值
如果在limits.conf里没有设置,则默认值是1024,如果limits.con有设置,则默认值以limits.conf为准。例如我换了一台机器,登录进去,ulimit -n显示如下:
[root@zk203 ~]# ulimit -n
2000 这是因为我的limits.conf里的文件打开数是2000,如下:
[root@zk203 ~]# cat /etc/security/limits.conf
root soft nofile 2000root hard nofile 2001
如果limits.conf里不做任何限制,则重新登录进来后,ulimit -n显示为1024。
[root@zk203 ~]# ulimit -n
1024
ulimit修改后生效周期
修改后立即生效,重新登录进来后失效,因为被重置为limits.conf里的设定值
二 /etc/security/limits.conf网上还有缪传,ulimit -n设定的值不能超过limits.conf里设定的文件打开数(即soft nofile)
好吧,其实这要分两种情况,root用户是可以超过的,比如当前limits.conf设定如下:
root soft nofile 2000
root hard nofile 2001
但是我用root将最大文件数设定到5000是成功的:
[root@zk203 ~]# ulimit -n 5000
[root@zk203 ~]# ulimit -n 5000 [root@zk203 ~]# 但是非root用户是不能超出limits.conf的设定,我切换到wxx,执行命令如下:
[wxx@zk203 ~]# ulimit -n 5000
-bash: ulimit: open files: cannot modify limit: Operation not permitted
所以网上的说法是错误的,即使非root用户的最大文件数设置不能超过limits.conf的设置,这也只是一个表象,实际上是因为,每个用户登录进来,ulimit -n的默认值是limits.conf的?0?2soft nofile指定的,但是对于非root用户,ulimit -n只能越来越小,不能越来越大,其实这个才是真正的原因,但是结果是一样的。
修改了limits.conf需要重启系统?
这个说法非常搞笑,修改了limits.conf,重新登录进来就生效了。在机器上试试就知道了,好多人真的很懒,宁愿到处问也不愿意花一分钟时间实际操作一下。
三 /proc/sys/fs/file-max网上说,ulimit -n 和limits.conf里最大文件数设定不能超过/proc/sys/fs/file-max的值,这也是搞笑了,/proc/sys/fs/file-max是系统给出的建议值,系统会计算资源给出一个和合理值,一般跟内存有关系,内存越大,改值越大,但是仅仅是一个建议值,limits.conf的设定完全可以超过/proc/sys/fs/file-max。
[root@zk203 ~]# cat /proc/sys/fs/file-max
1610495 我将limits.conf里文件最大数量设定为1610496,保存后,打印出来:
[root@zk203 ~]# cat /etc/security/limits.conf
root soft nofile1610496
root hard nofile1610496
四?0?2 总结一下
|
|
来自: liang1234_ > 《linux》