
- What is a file descriptor?
- What is the “too many open files” error message?
- What are hard and soft limits?
- Reviewing system wide limits for open files
- Reviewing maximum number of open files for a user
- How to increase the soft limit temporarily
- Increase the soft limit persistently for all users
- What if the changes do not take effect?
What is a file descriptor?
In Linux everything is considered a file, even directories. The only thing which is not considered a file within the Linux world is a process.
When a process opens/interacts with a file in Linux there is a number or identifier assigned to that interaction on the opened file and the identifier/number which was assigned is called a file descriptor.
With that being said, if you have 1000 files open that means there are 1000 file descriptor entries open that which were created by the operating system.
What is the “too many open files” error message?
System resources are shared between users on Linux servers and there are restrictions in place to prevent heavy load or over-usage of resources from a given user.
In this case we’ll be talking about the file limit restriction which when reached will trigger the “too many open files” error.
The restriction in place for how many files a process can have open at a given time are controlled by an upper (hard limit) and lower (soft limit) and to address the error we would only need to adjust the soft limit.
These limits can be reached due to a variety of reasons such as buggy applications, bad code, or the default limits could be too low for your environment and just require adjustment.
What are hard and soft limits?
For the open files limit there are soft and hard limits in place which can be configured.
Overall the soft value can be thought of as a quota that is allowed to be exceeded temporarily but the hard limit cannot be exceeded.
For example if we want to review what our hard limit is configured to we can do so with the following ulimit command:

$ ulimit -Hn
If you want to review the current soft limit you would just use -Sn instead such as with the following:

$ ulimit -Sn
Reviewing system wide limits for open files
In Ubuntu we can review the system wide limit for open files by checking the following file:

$ cat /proc/sys/fs/file-max
As you can see this number is astronomical in size for the global maximum number of open files allowed.
Reviewing maximum number of open files for a user
To see how many open files in total a user is allowed we need to gather 2 pieces of information and make a calculation.
First we’ll check to see how many files a process is allowed to have open at a time with the following ulimit command:

$ ulimit -n
After we obtain the value of ‘1024’ from above, we’ll then check the number of total processes my user is allowed to have running at a time with the following ulimit command:

$ ulimit -u
Once we have gathered both values we can multiply them (1024 x 3563) and receive a value of 3,648,512 which is the total amount of open files allowed on the user level.
The more important value from above is the number of files a process is allowed to have open at a time as this value sometimes needs to be raised to address the “too many open files” error.
How to increase the soft limit temporarily
If you want to temporarily raise the soft limit for your user the following command can be used to do so.
In this case we’ll increase it from 1024 to 2048:

$ ulimit -n 2048
Now it is important to note that performing the above is only a temporary increase and if we were to open a new terminal window or session it would revert back to 1024 since it only is active for our current session.
Increase the soft limit persistently for all users
If we wanted to increase this as a more permanent solution to prevent it from reverting we would do so by adjusting 2 different files as the root user or sudo privileged user.
1. Edit /etc/systemd/system.conf
The first file we will review is located at /etc/systemd/system.conf and we’ll want to adjust the following:

In the above we’ll change the first set of numbers (soft limit) of 1024 to 2048 as our desired value:

$ vim /etc/systemd/system.conf
Notice how we only adjusted the first set of numbers which was 1024 and we also uncommented the line by removing the #.
The second set of numbers will be left alone as that is the hard limit and we only want to adjust the standard soft limit.
2. Modify /etc/systemd/user.conf
We’ll perform the same task on one more file which is located at /etc/systemd/user.conf as follows:

$ vim /etc/systemd/user.conf
Notice how in the above the directive was not set so we just copied the line from /etc/systemd/system.conf and put it into place in /etc/systemd/user.conf.
3. Put changes into effect
Now we just need to either reboot the system or run the following command so that our changes take effect:

$ systemctl daemon-reexec
As mentioned you can also just reboot the system to have your changes take effect but if we execute the command we’re able to skip that which makes the job faster.
4. Confirm changes
After we’ve either rebooted or used the command in the previous step we’ll want to open a new session on the system as our non root user to check the current value of the soft limit.
In the following example I just logged out and logged back into my server to confirm the new limit:

As we see from above the changes did not take effect but we can resolve this in the next section. However if your output shows the new value you’re all done and do not need to proceed onto the next section.
What if the changes do not take effect?
1. Edit /etc/security/limits.conf
If for some reason this happens to you just make the following edit to /etc/security/limits.conf by adding the below as a new line at the bottom of the file.
This will set the soft limit to be 2048 for all users:
* - nofile 2048
2. Finalize changes
After saving the file run the following once more:
$ systemctl daemon-reexec
3. Confirm changes
Then confirm the changes:

Now that we see it has been updated from 1024 to 2048 we’re all set and confirmed everything has been completed successfully.
Making the edit in this manner will ensure the soft limit we adjusted will survive reboots and remain persistent.
Leave a Reply