If you are a Linux user and work with many files at a time, then you may often face the error “Too many open files” on Linux. This is because you have reached the maximum open file limit set by Linux operating system. In this case, you can use the ulimit command to change the default limit set by the operating system.

The max open file limit is very useful to prevent your system from sudden crashes. There are two types of open file limits in Linux.

  • Hard Limit: This limit can only be modified by the root user.
  • Soft Limit: This limit can be modified by any normal user. It indicates the current value of the session or user.

In this post, we will show you how to change the open file limit on Linux.

Check Open File Limits in Linux

To check the number of files that a user can have opened per login session, run the following command:

cat /proc/sys/fs/file-max

You will get the following output:

752300

The above result may be different based on the operating system.

To check the hard limit, run the following command:

ulimit -Hn

You will get the following output:

4096

To check the soft limit, run the following command:

ulimit -Sn

You will get the following output:

1024

To check all limits set by the operating system, run the following command:

ulimit -a

You will get the following output:

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 29405
max locked memory       (kbytes, -l) 65536
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 29405
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

Also Read

How to Check Linux CPU Usage or Utilization

Change Open File Limits in Linux Temporarily

In this section, we will show you how to change the open file limits temporarily.

To set the hard limit to 10000 temporarily, run the following command:

ulimit -Hn 10000

To set the soft limit to 10000 temporary, run the following command:

ulimit -Sn 10000

The above limits remain modified temporarily till the current session.

Change Open File Limits in Linux Permanently

You can edit the /etc/security/limits.conf file to set the open file limits permanently.

Edit the /etc/security/limits.conf file as shown below:

nano /etc/security/limits.conf

Change the hard limits to 10000 for all users, add the following line:

* hard nofile 10000

Change the soft limits to 10000 for all users, add the following line:

* soft nofile 10000

If you want to set the limit only for a specific user, add the following line:

user1 hard nofile 8000

Save and close the fil,e then log out and log back again to apply the changes.

You can now check the new limits using the following command:

ulimit

Also Read

How to Set Up an Rsync Daemon on Your Linux Server

Conclusion

In this guide, we explained how to see and set the open file limits on Linux. If you are running the Apache webserver or Oracle database server then you may require a higher open file limit. In this case, you can increase the open file limit to prevent your application from crashing. Try it on VPS hosting from Atlantic.Net!