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
- How to set a permanent limit on open files by editing
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Fixing the ‘Too Many Open Files’ Error on Linux – Troubleshooting Methods
Too Many Open Fileserror 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
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.
- First, let’s use the
ulimitcommand to check how many open files are system is permitted to have open simultaneously. We can use the
-aoption to see this information along with other pertinent restrictions, or use the
-noption to just see the restriction on the number of open files.
$ ulimit -a OR $ ulimit -n
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.
- To change the limit on the number of files a user can have opened at once (file descriptors), use the
-noption 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
ulimitcommand will apply to the current Bash shell only. To make a permanent change to system wide use, you must make changes to the
- Next, let’s see how to permanently change the number of allowed open files for system wide use in the
/etc/security/limits.conffile. 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
- 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
nofileto 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.
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.
- 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.conffile to put limits on users, groups, or everyone on the entire system.
- When you have finished editing the file, save changes and exit for the new settings to take effect.
In this tutorial, we saw how to fix the
Too Many Open Fileserror 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!