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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值