Changing the maximum number of open file descriptors

On Linux, there is a global and per-user limit of open file descriptors (read: maximum number of open files). The global limit is distribution and kernel specific, the per-user limit is set to 1024 by default. However, some applications, like Lotus Domino, Oracle, ... require to have more than 1024 open files.

Global limit

The current global limit can be checked with:
cat /proc/sys/fs/file-max

To change the global limit, you can do:
echo 65536 > /proc/sys/fs/file-max

Unfortunately, if you set the limit with the above command, it will not be saved after a reboot. To change this parameter at every boot, you have to add this to /etc/sysctl.conf:
fs.file-max = 65536

To make sure the value(s) configured in /etc/sysctl.conf become effective, you will have to run the following command:
sysctl -p

Per-user limit

To check the current per-user limit, run:
ulimit -n

To change the per-user limit, you have to add these lines to /etc/securitty/limits.conf:

username soft nofile 4096
username hard nofile 16384

The soft limit defines the maximum number of open file descriptors the user will have after logging in. If that is not enough, the user can increase the limit up to the hard limit set above, with this command:
ulimit -n 8192

If the user is only used to run a specific application, you should set the soft limit to the number that the application needs.

To make sure the settings from /etc/security/limits.conf are applied, you have to enable the PAM module that reads this file. This can be done by adding these lines to /etc/pam.d/system-auth:

session required /lib/security/pam_limits.so
session required /lib/security/pam_unix.so

To verify that the limits configured in /etc/security/limits.conf are effective, you can execute this command:

su - username
ulimit -n

Another thing you have to keep in mind is that you shouldn't set the limit for a user equal to (or higher than) the global limit, or your system could run out of open file descriptors, which would cause random problems :-)

Tags: 

Topics: