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:
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:
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:
Similarly, use the command below for soft values:
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:
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:
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:
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.
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.
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:
$ 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.