The "Too many open files" error on a CentOS 7 system is a common issue that occurs when an application or process exceeds the limit set for the maximum number of open file descriptors. This article provides a detailed guide on how to identify and subsequently fix this problem.
Causes of the Error
The "Too many open files" error indicates that a process in the operating system attempted to open more files than allowed by the system's limit. Each file opened by a process (including libraries, sockets, and other I/O resources) consumes a file descriptor. The limit on open file descriptors is enforced to protect system resources.
Identifying the Problem
First, it's necessary to identify which process is causing the error. This can be done using the lsof
or ps
commands, which show currently open files or running processes.
Increasing the Open Files Limit
-
Temporary Increase in Limit
To temporarily increase the open files limit for a specific process, you can use the
ulimit
command. For example, to increase the limit to 20480 open files for the current shell, use:ulimit -n 20480
This change applies only to the current shell and will be lost upon its closure.
-
Permanent Increase in Limit
For permanent changes, you need to modify configuration files:
a. Editing /etc/security/limits.conf
Open the file
/etc/security/limits.conf
in a text editor and add the following lines to increase the limit for all users:* soft nofile 20480 * hard nofile 40960
b. Editing /etc/sysctl.conf
To increase the limit at the system level, open
/etc/sysctl.conf
and add:fs.file-max = 2097152
After making changes to the
/etc/sysctl.conf
file, you need to apply the changes using the command:sysctl -p
-
Applying Changes
After editing the configuration files, it is recommended to restart the system or at least the affected services to apply the new limits.
The "Too many open files" error is often caused by inadequately configured limit values for open files in the system. By properly adjusting these limits, the problem can be effectively resolved, thus enhancing the stability and performance of applications running on CentOS 7. However, it is important to note that excessively high limits may have a negative impact on system resources and should, therefore, be set with consideration for available resources and application needs.