天天看點

【原創】如何在 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>