How Can I Increase the Operating System File Descriptor Limit?

“Too many open files” errors happen when a process needs to open more files than it is allowed by the operating system. This number is controlled by the maximum number of file descriptors the process has. If you experience such issue, you should increase the operating system file descriptor limit.

File descriptors are an abstract method for your system to store information about open files and thus facilitate communication. Each open file is assigned a file descriptor on file descriptor tables for the whole system and a certain process. The system puts limits on how big these tables can get and, as requests require the opening of files and thus the generating of file descriptors, these limits can constrain how many concurrent requests the server can handle. In UNIX-like operating systems, there are system-level and process-level file descriptor limits. For obvious reasons, the system-level file descriptor limit can restrict how much your server can handle, but, because LiteSpeed Web Server only uses a small number of server processes to serve all clients, LSWS can also require a higher process-level file descriptor limit. Therefore, to use your server to its maximum potential, it is important to set both these limits to a high enough value.

If the server is started by the root user, when this limit is too low, the system will automatically try to adjust the limit based on server configurations. If the server was not started by the root user, this limit has to be manually adjusted with root privilege. You may want to put the following scripts into your startup scripts in order to automatically set the limit after rebooting the machine.

Process-level file descriptor limits

Use the command ulimit -n to check current process-level file descriptor limit. You may find:

ulimit -n
32768

You can reset these limits by adding a number after the command, such as:

ulimit -n 3276800

Note: In Linux, non-root users can also use ulimit -n xxxx to change the process-level limit (at least in Kernel 2.4.x), but you need to add the following lines in /etc/security/limits.conf to give these users permission:

soft nofile 2048
hard nofile 8192

System-level file descriptor limits

Setting system-level file descriptor limits differs in different systems.

Linux kernels

Check the system-level limit for open files:

more /proc/sys/fs/file-max

If it looks low, increase the limit with:

echo 40000 > /proc/sys/fs/file-max

You may also need to increase fs.nr_open

echo "10000" >  /proc/sys/fs/nr_open

For kernel 2.2.x, you may also need to adjust the inode limit (the maximum number of files that can be stored in the file system):

echo 65535 > /proc/sys/fs/inode-max

Solaris 2.4+

Add the following lines in /etc/system:

To reset the hard file descriptor limit:

set rlim_fd_max = XXXX

To reset the soft file descriptor limit:

set rlim_fd_cur = XXXX

FreeBSD

Add the following line in /boot/loader.conf:

set kern.maxfiles=XXXX