- How to Increase the Maximum Open File Limit in Linux
- What is the Open File Limit in Linux?
- Understanding the Need for an Increase
- How to Check the Current Limit
- How to Increase the Open File Limit
- Increase Limit In Docker Container
- Conclusion
- How to Increase Number of Open Files Limit in Linux
- Find Linux Open File Limit
- Check Hard Limit in Linux
- Check Soft Limits in Linux
- How to Check System wide File Descriptors Limits in Linux
- Set User Level Open File limits in Linux
- Final thoughts
- How to increase the open file limits on Ubuntu
- What are the limits types in Ubuntu
- How to increase the open file limits on Ubuntu
- What is unlimit command in Ubuntu
- How to check current limits in Ubuntu
- How to check the soft and hard open file limits in Ubuntu
- How to increase the open file limits for the current session in Ubuntu
- How to increase per-user open file limits in Ubuntu
- How to increase system-wide open file limits in Ubuntu
- Conclusion
- About the author
- Sharqa Hameed
How to Increase the Maximum Open File Limit in Linux
Linux systems come with a default configuration that optimizes system performance based on average user workloads. However, specific tasks require more resources than others. For instance, running a large database server or a high-traffic web server necessitates a higher number of open files than usual. If the maximum open file limit isn’t increased to accommodate these demanding tasks, it may result in system errors or inefficiencies. This article will provide a comprehensive guide to increasing the open file limit in Linux, thereby optimizing system performance and maximizing productivity.
What is the Open File Limit in Linux?
In Linux, the ‘open file limit’ refers to the maximum number of file descriptors that a single process can open concurrently. A file descriptor is a unique integer that the system uses to access and manage open files and network sockets.
By default, most Linux systems limit this number to prevent system resources from being overwhelmed. However, this limit may become a bottleneck for certain applications that need to handle many files simultaneously.
Understanding the Need for an Increase
When operating a system with intensive workloads, such as large-scale data processing applications, high-traffic web servers, or extensive database systems, the default open file limit may not suffice. The system might return an error like “Too many open files”, suggesting that you need to increase the open file limit.
How to Check the Current Limit
Before changing anything, it’s prudent to check the current limit settings. In Linux, you can check the limit for the current session by running the following command:
This will return an integer, representing the current limit of open file descriptors for the current shell session.
How to Increase the Open File Limit
Now let’s dive into the steps to increase the open file limit in Linux:
To increase the limit for all users, you need to edit the /etc/sysctl.conf file. Append or modify the following lines:
The above command increases the system-wide limit to 100,000. Save and exit the file. To apply the changes, use the following command:
Edit the /etc/security/limits.conf file and append the following lines to increase the limit for a specific user (replace ‘username’ with the actual username):
The ‘soft’ limit is the actual limit enforced for sessions, while the ‘hard’ limit acts as the ceiling for the soft limit. An unprivileged process may only set its soft limit to a value in the range from 0 up to the hard limit.
Increase Limit In Docker Container
If you are running your application in a containerized environment, you can increase the max open file limit by passing the appropriate flag to the container runtime. For example, when running a container with Docker, you can pass the —ulimit flag to set the limit:
docker run --ulimit nofile=1000000:1000000 my_image
It’s also possible to increase the limit for a specific user by editing the user’s shell profile file. For example, if you are using the bash shell, you can edit the “.bashrc” file located in the user’s home directory. To increase the limit, you can add the following line to the file:
It’s important to note that increasing the maximum open file limit is not a solution for all performance issues. It’s a way to address specific problems caused by running out of file handles, but if your system is experiencing performance issues, it’s important to identify the root cause before increasing the limit.
Conclusion
Increasing the open file limit in Linux is a vital process to maximize system productivity and performance, especially in resource-intensive environments. While the default configuration may work for most users, understanding how to adjust these limits gives you more control over your Linux system, ensuring optimal system performance regardless of the workload. Remember, it’s crucial to monitor your system’s performance and make appropriate adjustments as your needs evolve.
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.
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
# 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.
# 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.
For example, to increase open file limit to 500000, you can use the following command as root:
You can check the current value for opened files with the following command:
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:
Of course, you can change the number per your needs. To verify the changes again use:
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:
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:
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:
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.
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:
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:
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:
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:
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:
In the “nano” editor, the limit configuration file will look like this:
Now, add the following content in the “/etc/security/limits.conf” file:
linuxhint soft nproc 100000
linuxhint hard nproc 100000
linuxhint soft nofile 100000
linuxhint hard nofile 100000
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:
Now, add this line to enable the pam_limits for the current session:
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:
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:
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:
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.