Maintain the File Descriptor Limit(ulimit)
  • 2 Minutes to read
  • Contributors
  • Dark
    Light
  • PDF

Maintain the File Descriptor Limit(ulimit)

  • Dark
    Light
  • PDF

As we are working on Linux, then we must have encountered many-a-time with “Too many open files (24)” error. Linux operating system provides a way of limiting the number of files that can be used by every user. This document provides insights on how to increase the number of open files or file descriptors in the Linux operating system.

To ensure good server performance, the total number of client connections, database files, and log files must not exceed the maximum file descriptor limit(open files limit) on the operating system (ulimit -n). So we will increase the open files limit to a maximum number.
This configuration will ensure the system has the capability to handle a huge amount of files. The datastores contain the data in an indexed format, so it will help in managing those data without any errors of file descriptors

Procedure

Step 1:

  • First, Let’s see how we can find out the maximum number of opened file descriptors on your Linux system.
$ cat /proc/sys/fs/file-max
1024
  • The number which is seen, shows the number of files that a user can have opened per login session. The result might be different depending on your system. If you are running a server, some of your applications may require higher limits for opened file descriptors.

Step 2:

  • You can increase the limit of opened files in Linux by editing the kernel directive fs.file-max. For that purpose, you can use the sysctl utility.

For example, to increase the open file limit to 500000, you can use the following command as root:

$ sysctl -w fs.file-max=500000

Step 3:

  • After that check the current value for opened files with the following command:
$ cat /proc/sys/fs/file-max
500000
  • With the above command, the changes you have made will only remain active until the next reboot.

Step 4:

  • If you wish to apply them permanently, you will have to edit the following file:
$ vim /etc/sysctl.conf
  • Add the following line:
fs.file-max=500000
Note

You can change the number as per your needs.

  • Users will need to logout and login again for the changes to take effect. If you want to apply the limit immediately, you can use the following command:
$ sysctl -p

Step 5:

  • The above examples show how to set global limits, we want to apply limits per user basis. For that purpose, as user root, you will need to edit the following file:
$ vim /etc/security/limits.conf
  • Add the following lines at the end of the file;
* soft nproc 500000 
* hard nproc 500000 
* soft nofile 500000 
* hard nofile 500000 
root soft nproc 500000 
root hard nproc 500000 
root soft nofile 500000 
root hard nofile 500000
  • Save and close the file.

Step 6:

  • Edit the following file;
$ vim /etc/pam.d/common-session
  • Add the following line and save the file.
$ session required pam_limits.so
  • logout and login and try the following command.
$ ulimit -n
500000
  • Make the above changes on the host machine and inside the docker container. Also, take the container commit and add that committed image-ID to the docker-compose.yml file so that even after docker restart/server reboot the changes remain active.

Was this article helpful?