Linux users often come across the error, ‘Too Many Open Files’ due to high load in the server, leaving it hard for us to open multiple files.
We, at Bobcares have experienced similar error and our Server Administration Team have come up with effective solutions.
Today, let’s check how to find the limit of maximum number of open files set by Linux and how we alter it for an entire host, individual service or a current session.
Where can we find error, ‘Too Many Open Files’?
Since Linux has set a maximum open file limit by default, the system has a method for restricting the number of various resources a process can consume.
Usually the ‘Too Many Open Files’ error is found on servers with an installed NGINX/httpd web server or a database server (MySQL/MariaDB/PostgreSQL).
For example, when an Nginx web server exceeds the open file limit, we come across an error:
To find the maximum number of file descriptors a system can open, run the following command:
The open file limit for a current user is 1024. We can check it as follows:
There are two limit types: Hard and Soft. Any user can change a soft limit value but only a privileged or root user can modify a hard limit value.
However, the soft limit value cannot exceed the hard limit value.
To display the soft limit value, run the command:
To display the hard limit value:
‘Too Many Open Files’ error & Open File Limits in Linux
Now we know that these titles mean that a process has opened too many files (file descriptors) and cannot open new ones. In Linux, the maximum open file limits are set by default for each process or user and the values are rather small.
We, at Bobcares have monitored this closely and have come up with a few solutions:
Increase the Max Open File Limit in Linux
A large number of files can be opened if we change the limits in our Linux OS. In order to make new settings permanent and prevent their reset after a server or session restart, make changes to /etc/security/limits.conf. by adding these lines:
- hard nofile 97816
- soft nofile 97816
If it is using Ubuntu, add this line as well:
These parameters allow to set open file limits after user authentication.
After making the changes, reload the terminal and check the max_open_files value:
Increase the Open File Descriptor Limit per service
A change in the limit of open file descriptors for a specific service, rather than for an entire operating system is possible.
For example, if we take Apache, to change the limits, open the service settings using systemctl:
Once the service settings is open, add the limits required. For example;
After making the changes, update the service configuration and restart it:
To ensure the values have changed, get the service PID:
For example, if the service PID is 3724:
Thus, we can change the values for the maximum number of open files for a specific service.
Set Max Open Files Limit for Nginx & Apache
In addition to changing the limit on the number of open files to a web server, we should change the service configuration file.
For example, specify/change the following directive value in the Nginx configuration file /etc/nginx/nginx.conf:
While configuring Nginx on a highly loaded 8-core server with worker_connections 8192, we need to specify
Then restart Nginx.
For apache, create a directory:
Then create the limit_nofile.conf file:
Add to it:
Do not forget to restart httpd.
Alter the Open File Limit for Current Session
To begin with, run the command:
Once the terminal is closed and a new session is created, the limits will get back to the original values specified in /etc/security/limits.conf.
To change the general value for the system /proc/sys/fs/file-max, change the fs.file-max value in /etc/sysctl.conf:
Finally, apply:
[Despite all the above solutions if the issue still prevail, don’t panic. We have our Experienced Tech Team working round the clock to help you fix it for you.]
Conclusion
To conclude, today, we figured how our Support Engineers solve the error, ‘Too Many Open Files’ and discussed options to change the default limits set by Linux.
We saw that the default value of open file descriptor limit in Linux is too small, and discussed a few options for changing these limits of the server.
0 Comments