Linux Commands

/proc/sys/fs/file-max: Linux Host Maximum Number of Open Files

The file-max contains the maximum file handles for a Linux host, representing the maximum number of files the host can have per session. There are different recommendations for the maximum file handles. You can also temporarily or permanently set a new maximum number from the command line. This guide explains the open files in a Linux host, how to view the maximum number, and set new values either permanently or temporarily.

Maximum File Handles in Linux

The file handles represent the maximum number individual Linux users can have for open files per session. The/proc/sys/fs/file-max defines the file limit. If you need to set a temporary limit before a reboot, that’s the file to edit.

To view the current limit for the number of open files in a Linux host, use the following command:

$ cat /proc/sys/fs/file-max

The maximum value of the open file descriptors listed in this case is for the Linux host. Different users can have different values than the host.

Alternatively, you can use the following command:

$ sysctl fs.file-max

The value in the output above represents the maximum limit for a normal user per login session. You can also get the soft and hard values for the descriptors using the ulimit command, which offers more control over shell resources and processes started by it.

For the hard values, use the following command:

$ ulimit -Hn

Similarly, use the command below for soft values:

$ ulimit -Sn

Modifying the /proc/sys/fs/file-max File

Some applications, such as the Oracle database, require a higher running range for the file descriptors. In that case, you will need to change the maximum current limit for the open files to increase the capacity. Changing this limit means you modify the kernel variable /proc/sys/fs/file-max, and you can achieve that in two ways.

For the first method, directly set the value from the command line. For instance, to set the range to 324567, the command would be the following:

$ sudo sysctl -w fs.file-max=324567

Note that you must have administrator privilege for this to work. Verify the new limit as we did when checking the maximum limit.

For the next method, only use it if you want to permanently set a new maximum limit for the open files on your system. The first method previously shown will reset to the default values after a reboot.

To proceed with the permanent method, you need to use a text editor like nano or vi and edit the /etc/sysctl.conf file. For our case, let’s use the vi edit. The command will be the following:

$ vi /etc/sysctl.conf

Add the fs.file-max=324567 with the new value of choice. Save and exit the file. The new values will persist even after a reboot.

Additionally, you will need to log out of your system and log back in for the changes to affect. Alternatively, run the following command:

$ sysctl -p

Finally, verify that the new limit is set using the previous command or list the contents of the /proc/sys/fs/file-max utilizing the cat command.

$ cat /proc/sys/fs/file-max

Setting User Level FD Limits

The previous commands set the file descriptors (FD) for the entire host system. However, you will not always need to modify the whole system. Sometimes, you may need to specify the specific user, which is possible.

To modify a specific user, you will add the limit to the /etc/security/limits.conf.

Use any editor to open the file and make the edits. For our example, we are using vi and setting the soft FD limits for a user named user1.

$ sudo vi /etc/security/limits.conf

You can verify that the soft limits we added for the specific user have worked by switching to that user account using the following commands:

$ su user1

$ ulimit -Sn

The -Sn is for the soft limits. In the following output, we note the value gets set to the one defined in the configuration file. Implying we successfully changed the file descriptors for a specific user.

Conclusion

That’s it! We’ve seen how to modify the /proc/sys/fs/file-max to view and edit the maximum limit for the open files in a Linux host. Furthermore, we’ve seen how to change the FD for a specific user and set a permanent or temporary limit for the open files in Linux.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.