Often times you will need to increase the maximum number of file descriptors in order to achieve proper functionality of your application. Perhaps you have a database or have run into the “Too many open files” error. In this tutorial, we will go over increasing this value and other ulimits. But first, what is a Linux ulimit?
What is a Linux ulimit?
A ulimit or user limit in Linux is a restriction placed on a user’s access to certain resources. You can use the command
ulimit -a to view them, this will give you a list of the ulimit values you can set, as well as their value. It is important to note that there are two types of limits, soft and hard.
A hard limit is the maximum allowed to the user. It is set by the root user and acts as the hard ceiling for that value. The user will be unable to have a limit higher than this unless it is changed by the root user. There are a few ways to update this value, but the most common way is by updating the /etc/security/limits.conf file. In most situations, it is best to not update this value.
A soft limit is the current maximum for the user. The user is able to change this at any time, either through setrlimit(2) or the ulimit command. Often times, you will only need to set this value, as very few situations call for resources above the hard limit.
Permanently Updating Limits
The best method for permanently updating soft limits and hard limits is via the /etc/security/limits.conf file. In this file, you can include the new soft limit defaults for a user, or update the hard limits. The format in which you insert your updates into this file is simple, it is just
<domain> <type> <item> <value>. So, if you wanted to update the default file limit for the user apache, it would look like this.
apache soft nofile 5000
Now, all you would have to do is reboot the system, and the user apache’s file descriptor limit would default to 5000.
Temporarily Updating Limits
The easiest way to temporarily update a soft limit is with the ulimit command. If you wanted to set your file descriptor limit to 5000 for the duration of your shell session, you can use this command.
ulimit -S -n 5000
The -S flag signals that you are temporarily updating the soft limit value, and the -n flag states you wish to change the value for file-descriptors. You can also update hard limits using this method, just swap out the -S with -H. However, you can only set a lower value.