How to Increase Number of Open Files Limit in Linux

In Linux, you can change the maximum amount of open files. You may modify this number by using the ulimit command. It grants you the ability to control the resources available for the shell or process started by it.

Read Also: Set Linux Running Processes Limits on Per-Userl Level

In this short tutorial we will show you how to check your current limit of open files and files descriptions, but to do so, you will need to have root access to your system.

First, Lets see how we can find out the maximum number of opened file descriptors on your Linux system.

Find Linux Open File Limit

The value is stored in:

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

818354

The number you will see, shows the number of files that a user can have opened per login session. The result might be different depending on your system.

For example on a CentOS server of mine, the limit was set to 818354, while on Ubuntu server that I run at home the default limit was set to 176772.

If you want to see the hard and soft limits, you can use the following commands:

Check Hard Limit in Linux

# ulimit -Hn

4096

Check Soft Limits in Linux

# ulimit -Sn

1024

To see the hard and soft values for different users, you can simply switch user with “su” to the user which limits you want to check.

For example:

# su marin
$ ulimit -Sn

1024
$ ulimit -Hn

4096

How to Check System wide File Descriptors Limits in Linux

If you are running a server, some of your applications may require higher limits for opened file descriptors. A good example for such are MySQL/MariaDB services or Apache web server.

You can increase the limit of opened files in Linux by editing the kernel directive fs.file-max. For that purpose, you can use the sysctl utility.

Sysctl is used to configure kernel parameters at runtime.

For example, to increase open file limit to 500000, you can use the following command as root:

# sysctl -w fs.file-max=500000

You can check the current value for opened files with the following command:

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

With the above command the changes you have made will only remain active until the next reboot. If you wish to apply them permanently, you will have to edit the following file:

# vi /etc/sysctl.conf

Add the following line:

fs.file-max=500000

Of course, you can change the number per your needs. To verify the changes again use:

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

Users will need to logout and login again for the changes to take effect. If you want to apply the limit immediately, you can use the following command:

# sysctl -p

Set User Level Open File limits in Linux

The above examples, showed how to set global limits, but you may want to apply limits per user basis. For that purpose, as user root, you will need to edit the following file:

# vi /etc/security/limits.conf

If you are a Linux administrator, I suggest you that you become very familiar with that file and what you can do to it. Read all of the comments in it as it provides great flexibility in terms of managing system resources by limiting users/groups on different levels.

The lines that you should add take the following parameters:

<domain>        <type>  <item>  <value>

Here is an example of setting a soft and hard limits for user marin:

## Example hard limit for max opened files
marin        hard nofile 4096
## Example soft limit for max opened files
marin        soft nofile 1024

Final thoughts

This brief article showed you a basic example of how you can check and configure global and user level limits for maximum number of opened files.

While we just scratched the surface, I highly encourage you to have a more detailed look and read regarding /etc/sysctl.conf and /etc/security/limits.conf and learn how to use them. They will be of great help for you one day.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Marin Todorov
I am a bachelor in computer science and a Linux Foundation Certified System Administrator. Currently working as a Senior Technical support in the hosting industry. In my free time I like testing new software and inline skating.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

12 thoughts on “How to Increase Number of Open Files Limit in Linux”

  1. I have encountered an error in RHEL 7.8 as “Too many open files“, causing an application issue. We are working side-by on the application code where there was a bug.

    What is that I am getting hard to check if three is a limit on the server-side configuration that a particular file can be concurrently opened for x-time, or if there is an overall max number of files that can be opened by an application/process.

    Where to get these configuration values? Please let me know if anyone has this information.

    Reply
    • @Ram,

      You can list all open files using the lsof command. Basically, it shows the information to look out the files that are opened by which process.

      Reply
    • @Ram,

      You can list all open files using the lsof command. Basically, it shows the information to look out the files that are opened by which process.

      Reply
  2. Thanks for the info.

    After making changes in /etc/security/limits.conf and restarting the process, I see open file count in /proc//limits is not changed!!! What else needs to be changed to get the changes reflected here..?

    Reply
    • logout and login again before restarting the process?

      perhaps add:
      session required pam_limits.so
      to the file /etc/pam.d/common-session

      just guesses

      Reply
    • If that doesn’t work you might try blowing away the limits.conf file and try again.

      (could have some random char there wrong that is breaking it)

      Reply
  3. HI,
    What should be the open file limit if the system RAM is 8GB? Also would like to know how to calculate this?
    we are having Ubuntu 12.04 with 8GB of RAM. I be leave we need to consider system RAM before changing he limit.

    Reply
  4. Hi,

    You are correct, but still I would like to clarify that per systemctl’s manual the following files are read:

    Load settings from all system configuration files.
    /run/sysctl.d/*.conf
    /etc/sysctl.d/*.conf
    /usr/local/lib/sysctl.d/*.conf
    /usr/lib/sysctl.d/*.conf
    /lib/sysctl.d/*.conf
    /etc/sysctl.conf

    So practically /etc/sysctl.conf is still read.

    Reply
  5. This way of increasing the open files limit is not reliable anymore, you need to create or edit a file in /usr/lib/sysctl.d/**.

    Although if you need to optimize a web-server, you are probably going to need to edit :

     kernel.pid_max = 65536
     net.core.netdev_max_backlog = 8192
     net.ipv4.tcp_syncookies = 0
     net.core.somaxconn = 8192
     net.ipv4.tcp_max_syn_backlog = 8192 
     net.ipv4.ip_local_port_range  = 1024 65535
     net.ipv4.tcp_tw_recycle = 1
     net.ipv4.tcp_tw_reuse = 1
    
    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.