Home Linux Commands How to Change Open File Limit in Linux

How to Change Open File Limit in Linux

In Linux, there are limits defined by the system for anything that consumes resources. For example, there are limits on how many arguments can be passed to a certain command, how many threads can run at the same time, etc.

Similarly, there is a limit on the number of open files. As you might know, an open file is actively being used in some of the other programs and hence consumes memory.
You can view and modify the open file limit with the command ‘ulimit‘.

Now, there are two types of limits defined: Hard limit and Soft limit.

  • The hard limit for open files is a statically set value, and can only be altered by the ‘root‘ user of Linux.
  • The soft limit is one that can be altered by processes dynamically, i.e., in the runtime, if the process is in need of more open files than the number allowed by the soft limit.

Needless to say, a soft limit is something that could lead to security issues.

Check Open File Limits in Linux

To view or list the hard limit of files, run:

$ ulimit -Hn

Similarly, for viewing soft limit, run:

$ ulimit -Sn

Here the 'S' and 'H' stand for soft and hard limits respectively, whereas the 'n' denotes the number of open files.

Change Open File Limits in Linux

Let’s now try to modify these limits by simply adding a third argument for the new value of the limit after 'Sn' or 'Hn'.

$ ulimit -Sn 5000
$ ulimit -Hn 5000
Change Open File Limit in Linux
Change Open File Limit in Linux

However, the limits altered in this way remain modified temporarily till the session (i.e. the terminal) is open. Follow the steps below to permanently modify the limits.

Permanently Set Open File Limits in Linux

Open the file ‘/etc/security/limits.conf‘ in ‘vim‘ or any editor of your choice. Note that this is a write-protected file and should be opened with ‘sudo‘ or as an administrator.

$ sudo vim /etc/security/limits.conf

Note that the limits can be set for all users, or for individual users. The syntax to set a hard limit is:

* hard nofile 5000

The '*' is signifying that the limit is for all users. The second string on the line is a type of limit, which is ‘hard’ in this case.

The third string is the limit that we want to modify. In our case, it is ‘nofile’, i.e. number of files. Like this other limits can also be set here, Eg. ‘noproc’ for a number of processes. Finally, the fourth string is the new limit to be set for the number of files.

If you want to set the limit only for a particular user, you can do so like the following:

abhi soft nofile 5000

Here ‘abhi’ is the name of the user.

Now, we will add these lines to the file and then see if the limit has been altered.

Set Open File Limit in Linux
Set Open File Limit in Linux

Save and exit the file.

These limits will require the user to logout and log back in, as the older limits are still being considered by running programs. Run ulimit again to verify if the limits are indeed changed.

$ ulimit

In this article, we have seen how to change the limit on the number of open files in Linux. Run 'ulimit --help' if you want to know the command in more detail.

Thanks for reading, and let us know your thoughts or questions in the comments below!

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.