Fix: Too Many Open Files Linux

Every command opens several process files to perform the specified operation. The Operating System has set a specific limit for the opened files, which can not be exceeded without changing the kernel parameters.

Sometimes, while working, you see the “too many open files” error which indicates that the running process has opened more files than the set limit.

The open files limit is set based on “per process” or “per user”. The set limit can be increased temporarily (for the current session only) or permanently following this guide.

What is Too Many Files Error in Linux?

In Linux, every process opens the related files, and there is a limit for the open files (Your system can have different open files limit according to the operating system). Open file limits are of two types:

  • Soft Limit

It shows the current maximum number of open files allowed for the user.

  • Hard Limit

The hard limit for open files is the maximum limit that can not be increased without changing the Kernel parameters.

The maximum number of files limit can be checked using the following command:

$ cat /proc/sys/fs/file-max

The currently used system maximum open files limit is shown in the output.

To check how many files are currently open, can be found by:

$ cat /proc/sys/fs/file-nr

The output is described as:

  • Column 1 (8128): The total number of open files in the system.
  • Column 2 (0): Open files which are not currently in use.
  • Column 3 (92337…): Maximum open files are allowed in the system.

To check the current limit of open files in the system that can be discovered by executing this command:

$ sysctl fs.file-max

The maximum number of open files a single process can open is found using the “n” flag of the ulimit command as shown below:

$ ulimit -n

The output shows that a single process can open a maximum of “1024” files.

The maximum number of processes allowed to a user (maximum user processes) can be checked using the “u” option, for the system can be checked using this command:

Note: This limit is also called the “soft” limit.

$ ulimit -u

The output shows a user can open a maximum of “11510” processes in the system.

The maximum number of open files limit for the system can be found by multiplying the single process open files limit and the maximum process for a single user, which is = 1024×11510 = 11786240

The maximum number of open files which can be opened by the system is called its hard limit. The hard limit for a system can be found using the “H” option with the ulimit command shown below:

$ ulimit -Hn

The output shows the maximum number of files (1048576) a system can open.

How to Fix Too Many Open Files Error?

This section will discuss the best solutions to fix too many open files error which can be done for the current session or permanently.

Reason: More Open Files than Maximum Limit

 When you try to open more files than the maximum number of open files limit (1048576), the error “too many open files ” occurs. The maximum open files limit can be increased to remove this error. Let’s perform its solution.

Solution: Increase Open Files Limit for the System

Multiple methods are utilized to increase the open file limit. In this action, we will increase the maximum open files limit by the following methods:

  • Increase the Maximum Open Files Limit for the Present Session Only
  • Increase the Maximum Open Files Limit Permanently

If you want to increase the open files limit for the presently logged-in session only which expires on logging out of the system. Let’s check how to increase the open files limit for the currently logged-in session only.

Method 1: Increase the Maximum Open Files Limit for the Present Session Only

Let’s check the current limit for the user (soft limit) by executing the below-mentioned command:

$ ulimit -n

The user’s current soft limit is “1024”.

To increase the soft limit (for the current session), use the new limit (4096) with the ulimit command as shown below:

Note: The second command is to verify the limit is increased.

$ ulimit -n 4096
$ ulimit -n

The output shows the open files limit is increased to “4096”, which is for the current session only (it will revert to 1096 when the user is logged out).

Let’s increase the open files limit permanently.

Method 2: Increase Open Files Limit Permanently

There are two ways to permanently increase the open files limit, which are discussed below:

Method 2.1: Increase the Open Files Limit Manually Using /etc/systemd/system.conf

This section will discuss the steps to permanently increase the open files limit using the /etc/systemd/system.conf file.

Make Changes to the /etc/systemd/system.conf File

Open the system configuration file in any editor (nano used in this case). Search for “DefaultLimitNOFILE” and change its limit (in this case, 1024) to the maximum number of the new soft limit, 4096 remove the “#” sign at the start of that line:

$ sudo nano /etc/systemd/system.conf

Save the file by pressing the “Ctrl + O”, press “Enter”, and close the file using the “Ctrl + X” shortcut keys.

Change the /etc/systemd/user.conf file

Open the “/etc/systemd/user.conf” file and search for “DefaultLimitNOFILE”:

$ sudo nano /etc/systemd/user.conf

Now, remove the “#” sign from the line start and change the value to the new maximum open file limit 4096:524288 as shown below:

Save the file by pressing the “Ctrl + O”, press “Enter”, and close the file using the “Ctrl + X” shortcut keys.

Reboot the system

Now, reboot the system to save the changes with this command:

$ sudo reboot

Verify the New Limit

We can verify the new increased limit for the open file by the following command:

$ ulimit -n

The output indicates the permanent open files limit is increased to “4096”.

Method 2.2: Increase the Open Files Limit Manually Using /etc/sysctl.conf

The open files limit can be increased manually by modifying the “/etc/sysctl.conf” file by performing the below steps:

Check the Current Maximum Open Files Limit

We can check the current maximum open files limit by executing this command:

$ sysctl fs.file-max

The output shows the maximum open file limit.

Increase the Maximum Open Files Limit

To increase the open files limit permanently, the “fs.file-max = 500000” line must be added to the file /etc/sysctl.conf. Let’s open the /etc/sysctl.conf in the nano editor by running the below command and add the fs.file-max = 500000 line at the end of the file:

$ sudo nano /etc/sysctl.conf

Update the System Settings

To update the settings in the system, use the below-written command:

$ sudo sysctl -p

The open files limit is increased, and the error of having too many files open is successfully removed.

Conclusion

In Linux, every process opens the associated files; when these files exceed the maximum limit, the “too many file open” error occurs. This error can be removed for the current session or permanently by increasing the limit using the /etc/systemd/system.conf or /etc/sysctl.conf files, as discussed in this article.