soft nofile

本文深入解析Linux系统中文件句柄的限制机制,包括ulimit、limits.conf及/proc/sys/fs/file-max的作用与交互,阐述不同用户级别的限制差异。

原创文章,转载请注明出处:http://jameswxx.iteye.com/blog/2096461

 

写这个文章是为了以正视听,网上的文章人云亦云到简直令人发指。到底最大文件数被什么限制了?too many open files错误到底可以通过什么参数控制?网上的很多文章说的大致步骤是没有错的,大致如下:

shell级限制 
通过ulimit -n修改,如执行命令ulimit -n 1000,则表示将当前shell的当前用户所有进程能打开的最大文件数量设置为1000.

用户级限制  
ulimit -n是设置当前shell的当前用户所有进程能打开的最大文件数量,但是一个用户可能会同时通过多个shell连接到系统,所以还有一个针对用户的限制,通过修改 /etc/security/limits.conf实现,例如,往limits.conf输入以下内容:
root soft nofile 1000 
root hard nofile 1200
soft nofile表示软限制,hard nofile表示硬限制,软限制要小于等于硬限制。上面两行语句表示,root用户的软限制为1000,硬限制为1200,即表示root用户能打开的最大文件数量为1000,不管它开启多少个shell。

系统级限制
修改cat /proc/sys/fs/file-max

 
 
但是呢,有很多很重要的细节,有很多错误的描述,一塌糊涂,因此特的在这里做一个说明。

一  ulimit -n

     网上很多人说,ulimit -n限制用户单个进程的问价打开最大数量。严格来说,这个说法其实是错误的。看看ulimit官方描述:
Provides control over the resources available to the shell and to processes started by  it,  on  systems that allow such control. The -H and -S options specify that the hard or soft limit is set for the given resource. A hard limit cannot be increased once it is set; a soft limit may  be  increased  up  to  the value of the hard limit. If neither -H nor -S is specified, both the soft and hard limits are set. The value of limit can be a number in the unit specified for the resource or one of the special values hard, soft,  or  unlimited,  which  stand  for  the  current hard limit, the current soft limit, and no limit,  respectively.
If limit is omitted, the current value of the soft limit  of  the  resource  is  printed,  unless  the  -H  option is given.  When more than one resource is specified, the limit name and unit are  printed before the value.
 
人家从来就没说过是限制用户的单个进程的最大文件打开数量,看看红色部分,是限制当前shell以及该shell启动的进程打开的文件数量。为什么会给人限制单个线程的最大文件数量的错觉,因为很多情况下,在一个shell环境里,虽然可能会有多个进程,但是非常耗费文件句柄的进程不会很多,只是其中某个进程非常耗费文件句柄,比如服务器上运行着一个tomcat,那么就是java进程要占用大多数文件句柄。此时ulimit设置的最大文件数和java进程耗费的最大文件数基本是对应的,所以会给人这样的一个错觉。 
   
还有,很多文章称ulimit -n 只允许设置得越来越小,比如先执行了ulimit -n 1000,在执行ulimit -n 1001,就会报"cannot modify limit: Operation not permitted"错误。这个其实也是不准确的说法。首先要搞清楚,任何用户都可以执行ulimit,但root用户和非root用户是非常不一样的。
非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
[wxx@br162 etc]$ 
 
root用户不受限制
首先切换到root:
[wxx@br162 etc]$ sudo su -
[root@br162 ~]#
查看下当前限制:
[root@br162 ~]# ulimit -n
1000000
[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 2000
root 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
[wxx@zk203 etc]$ 
所以网上的说法是错误的,即使非root用户的最大文件数设置不能超过limits.conf的设置,这也只是一个表象,实际上是因为,每个用户登录进来,ulimit -n的默认值是limits.conf的 soft 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
 
 

四  总结一下

  • /proc/sys/fs/file-max限制不了/etc/security/limits.conf
  • 只有root用户才有权限修改/etc/security/limits.conf
  • 对于非root用户, /etc/security/limits.conf会限制ulimit -n,但是限制不了root用户
  • 对于非root用户,ulimit -n只能越设置越小,root用户则无限制
  • 任何用户对ulimit -n的修改只在当前环境有效,退出后失效,重新登录新来后,ulimit -n由limits.conf决定
  • 如果limits.conf没有做设定,则默认值是1024
  • 当前环境的用户所有进程能打开的最大问价数量由ulimit -n决定

转载于:https://www.cnblogs.com/youngerger/p/9128245.html

为了设置系统文件描述符的软限制(`soft nofile`)和硬限制(`hard nofile`)分别为 65536 和 131072,需要对 Linux 系统的相关配置文件进行修改。以下是详细的配置步骤和建议: ### 修改系统范围的文件描述符限制 1. **编辑 `/etc/sysctl.conf` 文件** 添加或修改以下行以设置系统范围的文件描述符限制: ```bash fs.file-max = 131072 ``` 这个参数定义了系统范围内可以打开的最大文件描述符数量。保存文件后,执行以下命令以应用更改: ```bash sysctl -p ``` 2. **设置用户级别的软限制和硬限制** 编辑 `/etc/security/limits.conf` 文件,添加以下行以设置特定用户的软限制和硬限制: ```bash <username> soft nofile 65536 <username> hard nofile 131072 ``` 其中 `<username>` 是需要设置限制的用户名。如果希望为所有用户设置相同的限制,可以使用通配符 `*`: ```bash * soft nofile 65536 * hard nofile 131072 ``` 3. **确保 PAM 模块加载了 limits.conf** 检查 `/etc/pam.d/common-session` 文件是否包含以下行: ```bash session required pam_limits.so ``` 如果没有,添加该行以确保在用户登录时应用这些限制。 4. **调整 ulimit 设置** 如果需要临时调整当前会话的文件描述符限制,可以使用 `ulimit` 命令: ```bash ulimit -n 131072 ``` 但需要注意的是,`ulimit` 的设置仅在当前会话中有效,并且不能超过 `/etc/security/limits.conf` 中定义的硬限制。 ### 验证设置 1. **检查当前用户的文件描述符限制** 使用以下命令查看当前用户的软限制和硬限制: ```bash ulimit -n ulimit -Hn ``` 第一个命令显示软限制,第二个命令显示硬限制。 2. **检查系统范围的文件描述符使用情况** 使用以下命令查看系统当前的文件描述符使用情况: ```bash cat /proc/sys/fs/file-nr ``` 输出的三个数字分别表示当前已分配的文件描述符数量、已分配但未使用的文件描述符数量以及系统范围内可以分配的最大文件描述符数量。 ### 注意事项 - **重启后生效**:某些设置需要在系统重启后才能生效,特别是 `/etc/security/limits.conf` 中的更改。 - **影响范围**:修改系统范围的文件描述符限制可能会影响所有用户和进程,因此需要谨慎操作。 - **性能考量**:增加文件描述符限制可能会占用更多的系统资源,因此需要根据系统的实际需求进行调整。 ### 示例代码 以下是一个简单的 Python 脚本,用于测试当前进程可以打开的最大文件描述符数量: ```python import resource # 获取当前软限制和硬限制 soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) print(f"Current soft limit: {soft}") print(f"Current hard limit: {hard}") # 尝试调整软限制到硬限制 try: resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard)) print("Successfully increased soft limit to hard limit.") except Exception as e: print(f"Failed to increase soft limit: {e}") ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值