Fixing the ‘Too Many Open Files’ Error on Linux

Every Linux system imposes some upper limit on the number of files that can be opened simultaneously. If you exceed this limit, you will encounter the Too Many Open Files error. This is a security feature and failsafe to keep the system from becoming overwhelmed, since opening thousands of files is ordinarily unnecessary and indicative of a program gone haywire or a malicious user trying to crash the system.

At an administrator’s discretion, there could be circumstances where opening thousands of files should be permitted by the system, such as on a robust system with many users on it that share resources. In this tutorial, we will show you how to fix the Too Many Open Files error on Linux. This involves using the ulimit command to temporarily lift limits, or permanently allowing more files to be open simultaneously by making edits to the /etc/security/limits.conf configuration file. We will go over both methods and everything you need to know below.

In this tutorial you will learn:

  • How to check how many open files are allowed at once
  • How to set a temporary limit of files with ulimit command
  • How to set a permanent limit on open files by editing limits.conf
Fixing the 'Too Many Open Files' Error on Linux
Fixing the ‘Too Many Open Files’ Error on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software ulimit
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Fixing the ‘Too Many Open Files’ Error on Linux – Troubleshooting Methods




The Too Many Open Files error occurs when a user or process tries to access more file descriptors (fancy Linux term for files and other I/O resources, collectively) than the system currently allows. We will run through some steps below to identify the limit of our system, apply a temporary new limit, and a permanent new limit on the number of open files allowed.

When your system’s limit is exceeded, you will receive an error like this:

bash: command: Too many open files
WARNING
As a Linux administrator, you must carefully decide the limits you wish to impose on your system. If possible, you should only temporarily lift restrictions, rather than making a permanent change. Allowing too many files to be open at once will make your system vulnerable to attacks or buggy programs that try to open too many files and end up overwhelming system resources.
  1. First, let’s use the ulimit command to check how many open files are system is permitted to have open simultaneously. We can use the -a option to see this information along with other pertinent restrictions, or use the -n option to just see the restriction on the number of open files.
    $ ulimit -a
    OR
    $ ulimit -n
    
    Output of the ulimit command, showing the maximum number of opened files allowed
    Output of the ulimit command, showing the maximum number of opened files allowed

    Our output shows that our system can have 1024 files open at once. This is a very typical and default configuration, and will suffice for most single user systems. On shared servers, such as those rented out to virtual private server customers, this limit will not be nearly high enough.

  2. To change the limit on the number of files a user can have opened at once (file descriptors), use the -n option followed by the number of files. This command will allow a user to have 2000 files open at once.
    $ ulimit -n 2000
    
    DID YOU KNOW?
    All limits set with the ulimit command will apply to the current Bash shell only. To make a permanent change to system wide use, you must make changes to the /etc/security/limits.conf file.
  3. Next, let’s see how to permanently change the number of allowed open files for system wide use in the /etc/security/limits.conf file. Use root permissions to open the file; we will use nano in this example, but you may use a different text editor if you prefer.


    $ sudo nano /etc/security/limits.conf
    
  4. At the bottom of this file, we will impose some hard limits on our users, which are enforced system wide. The general syntax goes like this:
    [domain] [type] [item] [value]
    

    [domain] can be a user name, group name, or wild card (to apply to everyone). [type] can either be “soft” or “hard” depending on which kind of limit you would like to enforce. [item] is what you would like to limit, which in our case will be nofile to configure the file descriptors limit. [value] is the number you would like to enforce (number of allowed open files).

    All of this info is summarized in the file itself, in case you forget.

    Instructions from inside the limits.conf file
    Instructions from inside the limits.conf file

    Note that there is a difference between ‘soft’ and ‘hard’ limits: hard limits can only be raised further by root level access, whereas any process can lower the limit. This is great for security purposes as non-root processes will not be able to overstep a limit. A soft limit can be changed by a given process at any time.

  5. Here is an example. Let’s impose a limit of 2000 open files for user linuxconfig. We would do so by appending the following line to the end of the file.
    linuxconfig        hard    nofile          2000
    

    If we wanted to put a limit on the number of files for an entire group, like corporate, we would use the following syntax. This allows the group to have 5000 files open at once.

    @corporate        hard    nofile          5000
    

    Use any of the limit types described in the /etc/security/limits.conf file to put limits on users, groups, or everyone on the entire system.

  6. When you have finished editing the file, save changes and exit for the new settings to take effect.

Closing Thoughts




In this tutorial, we saw how to fix the Too Many Open Files error on a Linux system. Linux imposes a limit on the number of files we can have open at once. This is both a security feature and an extra measure to make sure that resources do not get easily overwhelmed by a buggy process or user that executes a risky command. You now know how to lift the restrictions either temporarily or permanently, so use this knowledge carefully!



Comments and Discussions
Linux Forum