Running high-concurrency network servers, databases, or file-intensive compilation scripts on Linux can suddenly trigger the terminal error: -bash: ...: Too many open files, accompanied in software logs by error codes EMFILE or ENFILE. In Unix-like operating systems, “everything is a file” – meaning network sockets, database connections, disk files, pipes, and event loops each consume an allocated integer handle called a file descriptor. When a user or daemon exhausts its assigned file descriptor allowance, the kernel rejects all new socket and file allocation requests.
Inspect Current File Descriptor Limits via ulimit
To verify the active file descriptor limits enforced on your current shell session:
- Check the soft file descriptor limit (the practical limit enforced on active processes):
ulimit -n - Check the hard limit (the maximum ceiling the user can elevate to without root privileges):
ulimit -Hn - On many default Linux server installations,
ulimit -nis set conservatively to1024. For modern web applications, reverse proxies, and microservices, 1024 file descriptors can be exhausted in seconds during traffic spikes. - To raise the limit temporarily in your current shell session:
ulimit -n 65535
Identify Which Process Is Exhausting File Descriptors
Before expanding system limits, verify whether an application suffers from an unclosed socket or unclosed file handle memory leak:
- Count open file descriptors across all running processes:
sudo lsof | wc -l - Find the top 10 processes holding the highest number of open file descriptors:
sudo lsof | awk '{print $1, $2}' | sort | uniq -c | sort -nr | head -n 10 - Inspect specific file paths held open by a suspicious process ID (PID):
sudo ls -l /proc/PID/fd | wc -l - If an application has tens of thousands of dangling sockets pointing to closed remote connections, restart the service to clear the leak.
Permanently Increase User Limits in /etc/security/limits.conf
To ensure updated file descriptor thresholds persist across user logins and system reboots, configure PAM security limits:
- Open
/etc/security/limits.confin an editor with administrative privileges:sudo nano /etc/security/limits.conf - Add the following directives at the bottom of the file (replace
usernamewith your active Linux user, or use*for all standard users):* soft nofile 65535 * hard nofile 65535 root soft nofile 65535 root hard nofile 65535 - Save and exit the file.
- Ensure PAM session limits are enabled in
/etc/pam.d/common-sessionand/etc/pam.d/common-session-noninteractiveby verifying the presence of:session required pam_limits.so - Log out and log back in to apply the updated limits. Verify with
ulimit -n.
Configure File Descriptor Limits for systemd Services
Modern Linux distributions manage background daemons (such as Nginx, Apache, MySQL, or Redis) via systemd, which completely ignores limits.conf:
- To raise limits for a specific service (for example, Nginx), edit its systemd override configuration:
sudo systemctl edit nginx - Add the following block into the override editor:
[Service] LimitNOFILE=65535 - Save and exit. Reload systemd and restart the service:
sudo systemctl daemon-reload sudo systemctl restart nginx - Verify the live limits of the running daemon:
cat /proc/$(pgrep -f nginx | head -n 1)/limits | grep "Max open files"
Raise Global System-Wide File Limits via sysctl
On high-throughput cluster nodes, ensure the kernel-wide total open file allocation table (fs.file-max) is configured for enterprise scale:
- Check the current kernel maximum open file allowance:
cat /proc/sys/fs/file-max - If the value is low, elevate it permanently:
echo "fs.file-max = 2097152" | sudo tee -a /etc/sysctl.conf - Apply kernel changes immediately without rebooting:
sudo sysctl -p