Check the Current Limits
ulimit -a
ulimit -n
ulimit -Hn
ulimit -a lists every limit of the current shell. ulimit -n shows the soft limit for open files, often 1024, and ulimit -Hn the hard limit, which is usually much higher. These values apply to the shell and to every program you start from it.
| Option | Limit |
|---|---|
-n | Open files |
-u | Processes per user |
-s | Stack size |
-c | Core file size |
-v | Virtual memory |
Soft and Hard Limits
The soft limit is the one enforced. A process may raise its own soft limit up to the hard limit, which is the ceiling; only root can raise the hard limit. In a shell:
ulimit -n 4096
This raises the soft limit for this session only, and only up to the hard limit.
What Too Many Open Files Means
Every open file, network connection, and pipe uses one file descriptor. A web server, database, or proxy with many clients reaches 1024 quickly, and new connections then fail with "Too many open files" in its log. The fix has three steps: find the process, read its real limit, and raise the limit where that process gets it.
Check a Running Process
A shell's ulimit tells you nothing about a service started at boot. Read the limits of the process itself:
pgrep -o nginx
cat /proc/1234/limits
sudo ls /proc/1234/fd | wc -l
Replace 1234 with the process ID from the first command. In /proc/PID/limits, the line "Max open files" shows the soft and hard value; the last command counts the descriptors the process holds now. lsof shows what those descriptors are.
Raise the Limit for a systemd Service
Services started by systemd do not read /etc/security/limits.conf. Set the limit in the unit with a drop-in:
sudo systemctl edit nginx.service
Add these lines, save, and restart the service:
[Service]
LimitNOFILE=65535
sudo systemctl restart nginx
systemctl show nginx.service -p LimitNOFILE
Check /proc/PID/limits again for the new process ID. Some programs also cap themselves in their own configuration, so read the application's documentation as well. Drop-ins are explained in systemd unit files. A default for all services can be set with DefaultLimitNOFILE in /etc/systemd/system.conf, but a per-service setting is easier to follow.
Raise the Limit for Login Sessions
For users who log in over SSH or switch with su, limits come from /etc/security/limits.conf or a file in /etc/security/limits.d:
deploy soft nofile 65535
deploy hard nofile 65535
The pam_limits module applies them at login, so log out and back in, then check with ulimit -n. A * in the first column means all users. On Debian and Ubuntu, * and group lines do not apply to root; a limit for root needs a line that names root.
| The process starts from | Set the limit in |
|---|---|
| Your SSH session | limits.conf |
| A systemd service | A drop-in file |
| A Docker container | --ulimit option |
The System-Wide Ceiling
cat /proc/sys/fs/file-max
cat /proc/sys/fs/file-nr
file-max is the number of open files the kernel allows in total; file-nr shows three numbers: the handles allocated now, the allocated but unused ones (0 on current kernels), and the maximum. On a 64-bit server the default is very large and is rarely the limit you hit. If you do need to change it, put fs.file-max in a file under /etc/sysctl.d and load it with sudo sysctl --system.
When Raising Is Not the Fix
If the descriptor count of a process grows steadily for hours and never falls, the application is leaking them, for example by opening files or connections without closing them. A higher limit only delays the error. Restart the service to recover, and report or fix the leak. Memory use is worth watching at the same time; see how to check memory usage.