How to Change Open File Limit in Linux?

How To Change Open File Limit In Linux

Hello folks, In this article, We will discuss how to change the open file limit in Linux.

What Is The Open File Limit?

The open file limit is the maximum number of files a user can open in the current active session. Mostly, sysadmins, developers, or users working with MySQL or other database management systems (DBMS). For Example, In MySQL, The default open file limit is 1024, but it automatically sets its limit to the system’s open file limit. There are two types of open file limits:

  • Hard limit: It is the maximum value a soft limit can be altered. It can only be changed by the root user.
  • Soft limit: It is the open file limit that can be altered by any user or process. It is changed temporarily at each active session.

Check Open File Limit in Linux

We can check the hard limit and soft limit using the ulimit command. To check the hard limit, We will use the -H parameter for the hard limit. Execute the following command:

ulimit -Hn
Screenshot From 2022 02 27 13 29 08

To check the soft file limit, We will use the -S parameter for soft limit. Execute the following command:

ulimit -Sn
Screenshot From 2022 02 27 13 30 53

To check the maximum number of open files that can be opened in the current active session, Execute the following command:

cat /proc/sys/fs/file-max
Screenshot From 2022 02 27 13 37 30

Here, The maximum open file limit is set to 100000. The hard limit is 1048576 and soft limit is 1024. Let’s see how to change these limits.

Change Open File Limit in Linux

To change the system-wide maximum open file limit, Execute the following command and set the value as required:

sysctl -w fs.file-max=500000
Screenshot From 2022 02 27 13 46 04

To change the hard limit, Execute the following command:

ulimit -Hn 10000
Screenshot From 2022 02 27 13 50 58

To change the soft limit, Execute the following command:

ulimit -Sn 5000
Screenshot From 2022 02 27 13 52 50

However, The hard limit and soft limit values changed using the ulimit command remain temporary till the active session. To change these values permanently can be done as follows:

Execute the following command in a terminal window to open the /etc/security/limits.conf file:

sudo vi /etc/security/limits.conf
Screenshot From 2022 02 27 14 13 30
Screenshot From 2022 02 27 14 00 04

Here, You can change the values for a particular user or all users by adding the following lines:

* hard nofile 10000
* soft nofile 5000
Screenshot From 2022 02 27 14 10 02

Here, The * means all users, To set the limits for a particular user, Replace the * by the username. The nofile means number of files that can be opened in a particular session. To save the file, press the Esc key followed by :wq and press Enter. So, The hard limit and soft limit have been set permanently.

Conclusion

So, We learned how to change the open file limit temporarily and permanently. Whenever you open the system files, It requires root permission, Use the sudo command and enter the password when prompted as we did while opening the /etc/security/limits.conf.