天天看点

【原创】如何在 Linux 下调整可打开文件/文件描述符数目

how do i increase the maximum number of open files under centos linux? how do i open more file descriptors under linux? 

the ulimit command provides control over the resources available to the shell and/or to processes started by it, on systems that allow such control. the maximum number of open file descriptors displayed with following command (login as the root user). 

ulimit 命令提供了针对 shell  和/或由该 shell 启动的进程占用资源的控制。 

command to list number of open file descriptors 

use the following command to display maximum number of open file descriptors: 

<a href="http://my.oschina.net/moooofly/blog/199596#">?</a>

1

<code>cat</code> <code>/proc/sys/fs/file-max</code>    <code>-- 显示单个登陆会话允许打开的 fd 数目</code>

output: 

<code>75000</code>

75000 files normal user can have open in single login session . to see the hard and soft values, issue the command as follows: 

2

<code># ulimit -hn</code>

<code># ulimit -sn</code>

to see the hard and soft values for httpd or oracle user, issue the command as follows: 

通过切换登陆用户,可以查看针对特定登陆用户所设置的 fd 限制数目。 

<code># su - username</code>

in this example, su to oracle user, enter: 

3

<code># su - oracle</code>

<code>$</code><code>ulimit</code> <code>-hn</code>

<code>$</code><code>ulimit</code> <code>-sn</code>

system-wide file descriptors (fd) limits 

the number of concurrently open file descriptors throughout the system can be changed via /etc/sysctl.conf file under linux operating systems. 

系统范围级别的 fd 数量控制需要编辑 /etc/sysctl.conf 内核参数配置文件。 

the number of maximum files was reached, how do i fix this problem? 

many application such as oracle database or apache web server needs this range quite higher. so you can increase the maximum number of open files by setting a new value in kernel variable /proc/sys/fs/file-max as follows (login as the root): 

通过调整内核参数  /proc/sys/fs/file-max 来增加可打开 fd 数目。 

<code># sysctl -w fs.file-max=100000   -- 这种方式只能临时修改 fd 数目限制</code>

above command forces the limit to 100000 files. you need to edit /etc/sysctl.conf file and put following line so that after reboot the setting will remain as it is: 

通过修改 /etc/sysctl.conf 文件,可以在令针对 fd 的修改一直生效。 

<code># vi /etc/sysctl.conf</code>

append a config directive as follows: 

<code>fs.</code><code>file</code><code>-max = 100000  -- 系统范围内修改 fd 数目</code>

save and close the file. users need to log out and log back in again to changes take effect or just type the following command: 

<code># sysctl -p   -- 不用重启系统令修改生效的方法</code>

verify your settings with command: 

<code># cat /proc/sys/fs/file-max</code>

or 

<code># sysctl fs.file-max</code>

user level fd limits 

the above procedure sets system-wide file descriptors (fd) limits. however, you can limit httpd (or any other users) user to specific limits by editing /etc/security/limits.conf file, enter: 

通过修改 /etc/security/limits.conf 文件可以在用户级别对 fd 进行限制。 

<code># vi /etc/security/limits.conf</code>

set httpd user soft and hard limits as follows: 

<code>httpd soft nofile 4096   -- 针对 httpd 用户做 fd 限制</code>

<code>httpd hard nofile 10240</code>

save and close the file. to see limits, enter: 

<code># su - httpd</code>