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

cat /proc/sys/fs/file-max    -- 顯示單個登錄會話容許打開的 fd 數目

Output:shell

75000

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

# ulimit -Hn
# ulimit -Sn

To see the hard and soft values for httpd or oracle user, issue the command as follows:
經過切換登錄用戶,能夠查看針對特定登錄用戶所設置的 fd 限制數目。oracle

# su - username

In this example, su to oracle user, enter:app

# su - oracle
$ ulimit -Hn
$ ulimit -Sn


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 數目。ide

# sysctl -w fs.file-max=100000   -- 這種方式只能臨時修改 fd 數目限制

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 的修改一直生效。ui

# vi /etc/sysctl.conf

Append a config directive as follows:this

fs.file-max = 100000  -- 系統範圍內修改 fd 數目

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:spa

# sysctl -p   -- 不用重啓系統令修改生效的方法

Verify your settings with command:code

# cat /proc/sys/fs/file-max

OR

# sysctl fs.file-max


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 進行限制。

# vi /etc/security/limits.conf

Set httpd user soft and hard limits as follows:

httpd soft nofile 4096   -- 針對 httpd 用戶作 fd 限制
httpd hard nofile 10240

Save and close the file. To see limits, enter:

# su - httpd
$ ulimit -Hn
$ ulimit -Sn
相關文章
相關標籤/搜索