Increase number of maximum open files in linux



If you are working on Linux, then you must have encountered many-a-time with “Too many open files (24)” error. Linux operating system provides a way of limiting the amount of files that can be used by every user. This article provides insights on how to increase the number of open files or file descriptors in Linux operating system.

The ulimit command has the control over shell or processes and it should require a login as the root user. The ulimit command is used to set the file limits that apply only to your current session.

Use the following command to display the maximum number of open file descriptors-

# cat /proc/sys/fs/file-max
801841

In the above command, the user can have open 801841 files in a single login session. To see the hard and soft values of file descriptors, use the following commands-

Hard Values of File Descriptors

Hard values limits can only be modified by root user. Non-root users cannot overstep a hard limit.

We can check hard values using the following command –

$ ulimit -Hn
4096

In the above command, “H” option indicates about hard value and “n” indicates number of files.

Soft Values of File Descriptors

A soft value limit can be modified by user process at any time. It is used to disable core dumps.

We can check hard values using the following command –

$ ulimit -Sn
1024

In the above command, “S” option indicates about soft value and “n” indicates number of files.

Fixing “Too many open files” error

It is possible to increase the maximum number of open files by setting a new value in kernel variable /proc/sys/fs/file-max as follows-

$ sysctl -w fs.file-max=100000
fs.file-max = 100000

The above command forces the maximum open files limit to 100000 and this is applicable for a particular session. If you want to make this value for a lifetime, then you need to edit /etc/sysctl.conf file and put the following line –

$ vi /etc/sysctl.conf

The sample output should be like this –

........
#
# Do not send ICMP redirects (we are not a router)
#net.ipv4.conf.all.send_redirects = 0
#
# Do not accept IP source route packets (we are not a router)
#net.ipv4.conf.all.accept_source_route = 0
#net.ipv6.conf.all.accept_source_route = 0
#
# Log Martian Packets
#net.ipv4.conf.all.log_martians = 1
#fs.file-max = 50000

Replace the file-max value to 100000 and save it. Users need to log out and sign-in again to make the changes take effect or just type the following command –

$ sysctl -p

To verify it, use the following command –

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

The sample output should be like this –

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

User Level File Descriptor Limits

The above procedure describes the system level file descriptor limits. However, we can set the user level file descriptor limits. To specify the user level file descriptor limits, you can do it by editing /etc/security/limits.conf file.

To edit the file, use the following command –

$ vi /etc/security/limits.conf

The sample output should be like this –

.....
#*        soft       core       0
#root     hard       core       100000
#*        hard       rss        10000
#@student hard       nproc      20
#@faculty soft       nproc      20
#@faculty hard       nproc      50
#ftp      hard       nproc      0
#ftp      -          chroot     /ftp
#@student -          maxlogins 4

Append the following line to limits.conf file –

#tp    soft    nofile    4096
#tp    hard    nofile    10240

Here tp is the user name of the system. Save and close the file. To see limits, use the ulimits commands as shown above.

Congratulations! Now, you know “How to increase the maximum number of open files or file descriptors”. We’ll learn more about these types of commands in our next Linux post. Keep reading!

Samual Sam
Samual Sam

Learning faster. Every day.


Advertisements