linux 最大文件句柄(file handle)相关参数 ulimit、nofile、nr_open、file-max

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.
  • ulimit -n 设置的单shell的最大文件数的限制,如果在一个shell中开启了多个进程,则会共享最大文件数。
  • ulimit只影响shell进程及其子进程,用户登出后失效。
  • ulimit -Sn 是软限制,超过即给出警告
  • ulimit -Hn 是硬限制,不可超过
  • 软限制不可超过硬限制

查看

查看软限制

ulimit -Sn

等同于

ulimit -n

aliyun的CentOS7.7默认值为65535
查看硬限制

ulimit -Hn

aliyun的CentOS7.7默认值为65535

修改

临时生效

ulimit -n 1048576

相当于同时设置了

ulimit -Hn 1048576
ulimit -Sn 1048576

永久生效

echo "ulimit -n 1048576" >> /etc/profile

或者

echo "ulimit -n 1048576" >> ~/.bashrc

不加参数相当于软限制
等同于

生效

临时生效则立即生效
永久生效则需要重新开启会话,或者

source /etc/profile

其他

  • 非root用户只能越设置越小,不能越设置越大
  • root用户不受限制
  • 如果/etc/security/limits.conf没有设置,默认为1024,如果limits.conf有设置默认以limits.conf为准

nofile

含义

The pam_limits.so module applies ulimit limits, nice priority and number of simultaneous login 
sessions limit to user login sessions. This description of the configuration file syntax applies to 
the /etc/security/limits.conf file and *.conf files in the /etc/security/limits.d directory.
  • limits.conf文件实际是Linux PAM中 pam_limits.so 的配置文件,pam_limits模块对 用户的会话 进行 资源限制。
  • 一个shell的初始limits就是由pam_limits设定的,用户登录后,pam_limits会给用户的shell设定在limits.conf定义的值。
  • pam_limits的原理就是在登录时应用ulimit limits(applies ulimit limits)
  • pam_limits的设置是 永久生效 的。
root soft nofile 65535 
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
  • 第一行设置root用户下的最大文件句柄数的软限制,超过限制出现警告
  • 第二行设置root用户下的最大文件句柄数硬限制,不可超过限制
  • 第三行设置所有用户下的最大文件句柄数的软限制,超过限制出现警告
  • 第四行设置所有用户下的最大文件句柄数硬限制,不可超过限制
  • 软限制不可超过硬限制

查看

cat /etc/security/limits.conf

aliyun的CentOS7.7 下默认值为65535

root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535

修改

vi /etc/security/limits.conf

直接修改文件

root soft nofile 1048576
root hard nofile 1048576
* soft nofile 1048576
* hard nofile 1048576

生效

因为是对用户生效,重新登录用户即可生效

其他

  • /proc/sys/fs/file-max限制不了/etc/security/limits.conf

sysctl

  • sysctl -n 展示单个sysctl配置值
  • sysctl -w 设置单个sysctl配置值,通过-w设置的值会写入配置文件
  • sysctl -p 从配置文件加载sysctl配置值

nr_open

含义

/proc/sys/fs/nr_open (since Linux 2.6.25)
This  file  imposes  ceiling  on  the  value to which the RLIMIT_NOFILE resource limit can be 
raised (see getrlimit(2)).  This ceiling is enforced for both unprivileged and privileged process.
The default value in this file is 1048576.  (Before Linux 2.6.25, the ceiling for RLIMIT_NOFILE 
was hard-coded to the same value.)
  • /proc/sys/fs/nr_open 设置一个进程能够使用的最大文件数
  • 对特权进程和非特权进程(both unprivileged and privileged process)都强制生效。

查看

sysctl -n fs.nr_open

或者

sysctl fs.nr_open

或者

cat /proc/sys/fs/nr_open

aliyun的CentOS7.7下默认值为1048576

修改

sysctl -w fs.nr_open=1048576

或者

echo "1048576" >> /proc/sys/fs/nr_open
sysctl -p

file-max

含义

/proc/sys/fs/file-max
This file defines a system-wide limit on the number of open files for all processes.  System calls 
that fail when encountering this limit fail  with  the error ENFILE.  (See also setrlimit(2), which 
can be used by a process to set the per-process limit, RLIMIT_NOFILE, on the number of files it may
open.)  If you get lots of error messages in the kernel log about running out of  file  handles  
(look  for  "VFS:
              file-max limit <number> reached"), try increasing this value:

                  echo 100000 > /proc/sys/fs/file-max

              Privileged processes (CAP_SYS_ADMIN) can override the file-max limit.

  • /proc/sys/fs/file-max 是系统级的总文件数量限制,当达到此限制之后内核报错 ENFILE;
  • 特权进程( Privileged processes)可以覆盖file-max限制。

查看

sysctl -n fs.file-max

或者

sysctl fs.file-max

或者

cat /proc/sys/fs/file-max

aliyun的CentOS7.7 下(1G内存)默认值为98089

修改

sysctl -w fs.file-max=1048576

或者

echo "1048576" >> /proc/sys/fs/file-max
sysctl -p

file-nr

含义

/proc/sys/fs/file-nr
This (read-only) file contains three numbers: the number of allocated file handles (i.e., the number
of files presently opened); the num‐ber of free file handles; and the maximum number of file handles
(i.e., the same value as /proc/sys/fs/file-max).  If the number of allo‐cated file handles is close
to the maximum, you should consider increasing the maximum.  Before Linux 2.6, the kernel
allocated file han‐dles dynamically, but it didn't free them again.  Instead the free file
handles were kept in a list for reallocation; the "free file han‐dles"  value  indicates  the
size of that list.  A large number of free file handles indicates that there was a past peak in
the usage of open file handles.  Since Linux 2.6, the kernel does deallocate freed file
handles, and the "free file handles" value is always zero.
  • /proc/sys/fs/file-nr为只读文件,包含三个数值,已分配的文件句柄数,空闲文件句柄数,最大文件句柄数。
  • 当文件句柄数不够用了以后,手动增大最大文件句柄数。
  • 2.6内核版本以后,空闲文件句柄数永远为零。

查看

sysctl -n fs.file-nr

或者

sysctl fs.file-nr

或者

cat /proc/sys/fs/file-nr
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: worker_rlimit_nofile参数最大可以填则取决于操作系统的设置,根据所使用的操作系统的不同,worker_rlimit_nofile参数最大值也不同。例如,在Linux操作系统上,最大值可以设置为65535。 ### 回答2: worker_rlimit_nofile参数决定了每个NGINX工作进程能够打开的最大文件描述符数量。文件描述符是操作系统用于跟踪打开的文件或套接字的标识符,因此该参数可以限制NGINX服务器处理同时打开的连接数量。 worker_rlimit_nofile的默认值在不同的系统上可能有所不同,通常是1024。这个值对于一些简单的网站应用来说可能足够,但对于大负载的高流量网站,这个值可能会很快耗尽,导致服务器无法处理更多的连接请求。 在某些情况下,我们可能需要增加这个值以支持更多的连接。实际上,worker_rlimit_nofile可以设置的最大值是操作系统对文件描述符数量的限制。在大多数Linux系统上,这个限制可以通过收件人打开文件句柄最大数量来确定。 要查看系统上文件描述符数量的限制,可以使用以下命令: ulimit -n 然后,可以将worker_rlimit_nofile设置为此值,以便NGINX能够打开的最大文件描述符数量与系统限制相匹配。 然而,需要注意的是,增加worker_rlimit_nofile值也会增加服务器的资源消耗,尤其是内存消耗。因此,不仅需要考虑操作系统的文件描述符限制,还需要评估服务器的硬件资源,以确定适合的worker_rlimit_nofile值。 ### 回答3: worker_rlimit_nofile参数是用来限制工作进程打开的文件描述符数量的。文件描述符是操作系统用来标识和跟踪打开文件的一种机制。工作进程打开的文件描述符的数量取决于系统的硬件资源和配置。 在大多数的Linux系统上,worker_rlimit_nofile参数最大值默认是1024。这个值是操作系统内核给每个用户进程允许打开的文件描述符的最大数量。虽然默认值是1024,但是我们可以通过修改操作系统的相关配置文件来增加这个值,以满足特定应用的需求。 在修改worker_rlimit_nofile最大值时,我们需要权衡系统硬件资源和性能,以及应用的需求。如果系统硬件资源比较充足,并且应用对打开文件描述符的需求较高,我们可以考虑适当增大worker_rlimit_nofile最大值。但是过大的值可能会导致系统负载过高,性能下降。因此,我们需要综合考虑系统资源和应用需求来确定一个合适的值。 需要注意的是,修改worker_rlimit_nofile最大值需要root权限。在修改之前,我们需要仔细评估系统的配置和需求,并确保这个操作不会给系统性能带来过大的负担。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值