Bookmark and Share
Linux maximum number of open files or file descriptors
(Publish Date: 2009-11-11 10:18pm, Total Visits: 29483, Today: 2, This Week: 45, This Month: 410)

There are two levels of control on Linux maximum number of open files (or file descriptors), system limit and per-user/group/process limit. /etc/sysctl.conf is to set system-wide amount for kernel, /etc/security/limits.conf is to assign specific limits on per-user/group or process.

You may make changes by either issuing commands online or modifying the configuration files. Please bear in mind, online modification is usually temporary (the settings will be gone after reboot of system), change by modifying the configuration files is permanent.


1. System file descriptor limit


System file descriptor limit belongs to the scope of system performance and scalability tuning. The value should be set based on the system RAM and processes to run.
1.1 Check the system-wide maximum number of open files:
# cat /proc/sys/fs/file-max
Output:
206009
1.2 Change the system-wide maximum number of open files
Many applications such as MySQL database server needs this range quite high. You can increase the maximum number of open files by the following ways:
1) Online change by setting new value in kernel variable /proc/sys/fs/file-max,  e.g. the following commands force the limit to 300000 files (takes effect immediately):
# sysctl -w fs.file-max=300000
OR:
# echo "300000" > /proc/sys/fs/file-max
2) Permanent change by editing the configuration file /etc/sysctl.conf:
 # vi /etc/sysctl.conf
Append a config directive (line) as follows:
 fs.file-max = 300000
You will have to either reboot the system or run the following command to make it effective:
 # sysctl -p
3) Verify your settings with command
 # cat /proc/sys/fs/file-max
OR
 # sysctl fs.file-max


2. Per-User/Group/Process file descriptor limits


Each user has per-user file descriptor limits. It includes soft and hard limits. hard limit is the absolute limit, user can set its soft limit freely to any value up to hard limit (use command: ulimit -n xxxx). Per-user limit defaults to 1024. Only root can increase the hard limit.
2.1 Check the per-user file descriptor limit (login to the user)
 $ulimit -n
2.2 Change the per-user file descriptor limit online (login to the user)
 $ ulimit -n 20480           (this command changes the user limit to 20480)
The change takes effect immediately, but will be gone after logout of user or reboot of system.
2.3 Change the default per-user file descriptor limit permanently by editing the configuration file /etc/security/limits.conf (login to root)
 #vi /etc/security/limits.conf
Add appropriate entries, for example:
 mysql      -     nofile  40960        (remark: '-' enforcing both soft & hard limits)
 ftp        -     nofile  512
 @students  soft  nofile  4096
 @students  hard  nofile  4096         (remark: students is a user group)
You may run command 'man limits.conf' for detail of format of limits.conf:
 $man limits.conf