linux下最大文件描述符的限制有两个方面,一个是用户级的限制,另一个则是系统级限制。linux
先介绍如何修改系统级的限制
一般咱们经过终端链接到linux系统后执行ulimit -n 命令能够看到本次登陆的session其文件描述符的限制,以下:
$ulimit -n
1024session
固然能够经过ulimit -SHn 102400 命令来修改该限制,但这个变动只对当前的session有效,当断开链接从新链接后更改就失效了。ide
若是想永久变动须要修改/etc/security/limits.conf 文件,以下:
vi /etc/security/limits.conf
* hard nofile 102400
* soft nofile 102400this
保存退出后从新登陆,其最大文件描述符已经被永久更改了。spa
这只是修改用户级的最大文件描述符限制,也就是说每个用户登陆后执行的程序占用文件描述符的总数不能超过这个限制。.net
系统级的限制
它是限制全部用户打开文件描述符的总和,能够经过修改内核参数来更改该限制:
sysctl -w fs.file-max=102400code
使用sysctl命令更改也是临时的,若是想永久更改须要在/etc/sysctl.conf添加
fs.file-max=102400
保存退出后使用sysctl -p 命令使其生效。orm
与file-max参数相对应的还有file-nr,这个参数是只读的,能够查看当前文件描述符的使用状况。three
sysctl -a | grep file-nr内存
下面是摘自kernel document中关于file-max和file-nr参数的说明
file-max & file-nr:
The kernel allocates file handles dynamically, but as yet it doesn't free them again.
内核能够动态的分配文件句柄,但到目前为止是不会释放它们的
The value in file-max denotes the maximum number of file handles that the Linux kernel will allocate. When you get lots of error messages about running out of file handles, you might want to increase this limit.
file-max的值是linux内核能够分配的最大文件句柄数。若是你看到了不少关于打开文件数已经达到了最大值的错误信息,你能够试着增长该值的限制
Historically, the three values in file-nr denoted the number of allocated file handles, the number of allocated but unused file handles, and the maximum number of file handles. Linux 2.6 always reports 0 as the number of free file handles -- this is not an error, it just means that the number of allocated file handles exactly matches the number of used file handles.
在kernel 2.6以前的版本中,file-nr 中的值由三部分组成,分别为:1.已经分配的文件句柄数,2.已经分配单没有使用的文件句柄数,3.最大文件句柄数。但在kernel 2.6版本中第二项的值总为0,这并非一个错误,它实际上意味着已经分配的文件句柄无一浪费的都已经被使用了
file-max的默认值大概是系统内存的10%(系统内存以kb计算),
参考文章:http://www.mjmwired.net/kernel/Documentation/sysctl/fs.txt