Ubuntu

How to increase the open file limits on Ubuntu

On a Linux-based system such as Ubuntu, we occasionally face problems such as “too many open files,” especially when you install and work in platforms like Apache web server and Oracle database. The encountered error signifies that our server has opened the files up to the default maximum number. This issue occurs when the system imposes resource constraints on any user or session. For instance, the maximum size that can be locked into memory, the maximum size of created file, the maximum CPU time that can be utilized, the maximum number of processes that are allowed to execute, and the maximum virtual memory size that can be used; these are all examples of resource limitations in an Ubuntu system.

What are the limits types in Ubuntu

Limits in an Ubuntu system are of two types:

  • Soft limit: This type of limit indicates the current value of the session or user.. An Ubuntu user has the option to increase its value to the hard limit.
  • Hard Limit: The superuser/root sets the maximum allowable limit for a user or session, known as the hard limit.

Now, we will explain the procedure to increase the open file limits on Ubuntu. So let’s start!

How to increase the open file limits on Ubuntu

First of all, open your Ubuntu terminal by pressing “CTRL+ALT+T”. Now, we will check out the current limits of the resources. For this, we will execute the “unlimit” command.

What is unlimit command in Ubuntu

The “unlimit” is a Linux shell command utilized to set, view, or limit the current user’s resources. It also extracts information about how many open file descriptors each process has. Many Ubuntu users execute this command to limit the resources which are in the use of a process.

How to check current limits in Ubuntu

To view the current limits set, add the “-a” option in the “unlimit” command:

$ ulimit -a

Execution of the above-given command will show you the following output:

How to check the soft and hard open file limits in Ubuntu

To view the current soft limit, execute the unlimit command with the “-Sn” option:

$ ulimit -Sn

From the below-given output, you can see that “1024” is the soft limits of the open files in our Ubuntu system:

The “-Hn” option is added to the “unlimit” command for retrieving the hard limit of the open files:

$ ulimit -Hn

How to increase the open file limits for the current session in Ubuntu

As mentioned earlier, you can utilize the “unlimit” command for setting the current session limits. If you want to increase the open file limits for your current Ubuntu session, choose a value between the soft and hard limits and add that specific numerical value with the “-n” option. Adding the “-n” option to the “unlimit” command lets you increase the open file limits:

$ ulimit -n 100000

How to increase per-user open file limits in Ubuntu

You can also increase the open file limits for a specific user. To do so, open the limits configuration file “/etc/security/limits.conf” in the “nano” editor:

$ sudo nano /etc/security/limits.conf

In the “nano” editor, the limit configuration file will look like this:

Now, add the following content in the “/etc/security/limits.conf” file:

* soft nproc 65535

* hard nproc 65535

* soft nofile 65535

* hard nofile 65535

linuxhint soft nproc 100000

linuxhint hard nproc 100000

linuxhint soft nofile 100000

linuxhint hard nofile 100000

Here:

  • *” denotes the rest of the system users excluding “linuxhint”
  • linuxhint” is our domain
  • soft” or “hard” is the limit type
  • nofile” item is utilized for limiting the number of the file descriptor
  • nproc” item defines the maximum user processes limit
  • 100000” or “65535” are the limit values

Press “CTRL+O” and save the changes we made in the “/etc/security/limits.conf”:

In the next step, we will restrict the number of system resources a user can get in a single session by enabling the pam_limits. For this, you have to edit the “/etc/pam.d/common-session” file in your “nano” editor:

$ sudo nano /etc/pam.d/common-session

Now, add this line to enable the pam_limits for the current session:

session required pam_limits.so

Press “CTRL+O” to save the added content of “ /etc/pam.d/common-session” file:

How to increase system-wide open file limits in Ubuntu

Ubuntu also provides you the option to increase system-wide open file limits. For this purpose, we will edit the “/etc/sysctl.conf” file:

$ sudo nano /etc/sysctl.conf

You can set the maximum number of file handles that the Linux kernel will allocate by using the “fs.file-max” parameter. Now, add the below-given line in the “sysctl.conf” file:

fs.file-max = 2097152

Setting the value “2097152” for “fs.file-max” parameter will set this value as maximum number of file handles:

Again, press “CTRL+O” to save the “sysctl.conf” file:

The “sysctl” command with the “-p” option will load the kernel settings from the “sysctl.conf” file. For applying the changes we have made, execute the below-given “sysctl” command in your terminal:

$ sudo sysctl -p

The execution of the above-given command will increase the maximum number of open files across the entire system:

Conclusion

In Ubuntu, many applications, such as the Apache web server or the Oracle databases, require a higher open file limit, resulting in an excessive number of open files, file descriptors, etc. If the number of open files exceeds the default limit, file opening difficulties and access control issues may arise. This article showed you how you can increase the open file limits in your Ubuntu system. Moreover, you have also seen how to check and increase the system-wide and per-user limits of opened files.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.