If you are an active Linux user, who has to work with many files on Linux at a time then you might have definitely faced a problem regarding “Too many open files” on a Linux system. When you have reached the maximum open file limit you will get an error message displaying “Too many open files (24)” error on your screen.
Why do we face such a thing? Well, Linux operating system set a limit of “open files” that a user can open at a time, Linux operating system uses this way to restrict the user from opening too many files at a time.
But luckily we can modify the number of Open File Limits in Linux according to our wish using some different methods that we are going to discuss today in this article.
Types of open file Limit
Why does Linux Limit the number of opened files? The reason is first due to security purpose so that no software creates files endlessly till the Linux server crashes and also that the Linux Operating System needs memory to manage each open file and memory is kind of limited especially on embedded systems so there is some limitation for a number of open files in a system by a user.
There are 2 types of open file Limit, are as follows:
- Hard Values of File Descriptors.
- Soft Values of File Descriptors.
Hard Values of File Descriptors: Hard value limits are those file limits that can only be modified by the root user. Non-root users cannot change the value of a hard limit.
We can check the hard value limit using the following command:-
$ ulimit -Hn
Soft Values of File Descriptors: A soft value limits are those limit that shows the current effective value for the user that can be modified by a user process at any time. Core dumps can be disabled by soft values.
We can check the soft value limit using the following command:-
$ ulimit -Sn
Methods to Change the Number of Open File Limit in Linux
ulimit is a bash built-in shell command(so you might not get the desirable result from it on other types of shell) which can be used to increase the number of open files descriptors limit for each process in Linux shell.
Syntax : ulimit [options [limit]]
a.) -a (Current Settings Passing):- argument that causes ulimit to display its current settings
To show the current limitation use the following command:
ulimit -a | grep open
b.) -f (File Limits): argument limits the size of files that may be created by the shell.
c.) -H and -S (Hard and Soft Limits) is already discussed above
Now For editing the Limit use the following command:-
ulimit -n 3000
But this value will be reset if you restart your computer or logout the user.
For changing the value permanently we have to edit one of the user’s configuration files (.bashrc or .profile) or the system-wide configuration files (/etc/bashrc or /etc/profile) by adding the following command at the end of the file:-
# vim .bash_profile
ulimit -n 3000
Now the changes are permanent, even you restart your computer, it won’t change.
Another best way to modify the open file limit is done through this PAM module called pam_limits. We have to configure it by editing the /etc/security/limits.conf file.
There are 4 essential fields present in this configuration file. They are:-
- domain: Domain describes a specific unit to which the limit applies that can be a username, a group name (with the form @groupname syntax), or an asterisk ( * ) wildcard (wildcard limits are not applied to root).
- type: Type specifies whether the limit is hard or soft.
- item: it specifies the type of item that is being limited. This could be core (limits the size of core files), fsize (maximum file size), data (maximum data size), sizeetc.
- value: values that will be applied to the limit.
Now you can edit this configuration file to change the limit of opened files for all users, For instance, you can add the following lines below the end of the file lines:
# vim /etc/security/limits.conf
* hard nofile 21000
* soft nofile 16000
Now edit the file /etc/pam.d/login
# vim /etc/pam.d/login
session required pam_limits.so
And you are done changing the open file limit.