Understanding the Error
Linux limits how many files (including sockets and pipes) a process can open simultaneously. When this limit is reached, you see:
Too many open files
socket: Too many open files
accept: Too many open files (24)
Check Current Limits
# System-wide limit
cat /proc/sys/fs/file-max
# Current user's soft/hard limits
ulimit -Sn # Soft limit (usually 1024)
ulimit -Hn # Hard limit
# For a running process
cat /proc/PID/limits | grep "Max open files"
# How many files a process has open
ls /proc/PID/fd | wc -l
Find Which Process Hit the Limit
# List processes by open file count
for pid in /proc/[0-9]*; do
count=$(ls "$pid/fd" 2>/dev/null | wc -l)
name=$(cat "$pid/comm" 2>/dev/null)
[ "$count" -gt 100 ] && echo "$count $name (PID: $(basename $pid))"
done | sort -rn | head -10
Fix: Increase Limits
For the Current Session
ulimit -n 65535
Permanent — System-Wide
# /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
root soft nofile 65535
root hard nofile 65535
For Systemd Services
# In the service file's [Service] section
LimitNOFILE=65535
Or globally for all services:
# /etc/systemd/system.conf
DefaultLimitNOFILE=65535
sudo systemctl daemon-reload
sudo systemctl restart your-service
System-Wide Maximum
# /etc/sysctl.conf
fs.file-max = 2097152
fs.nr_open = 1048576
sudo sysctl -p
Verify the Fix
# Check new limits for running service
cat /proc/$(pgrep -f nginx)/limits | grep "Max open files"
# Max open files 65535 65535 files
Common Services That Need Higher Limits
| Service | Default | Recommended |
|---|---|---|
| Nginx | 1024 | 65535 |
| MySQL/MariaDB | 1024 | 65535 |
| Redis | 10240 | 65535 |
| PostgreSQL | 1024 | 65535 |
| Node.js (PM2) | 1024 | 65535 |
Warning Increasing file limits without investigating why so many files are open can mask connection leaks. Always check for leaked sockets or unclosed file handles in your application.