Skip to content

Fix "Too many open files" on Linux: raise the fd limit correctly

The "too many open files" error means a process hit its file descriptor limit. Diagnose the right layer and raise ulimit -n, systemd LimitNOFILE, or fs.file-max without guesswork.

When a program prints Too many open files — or fails an operation with EMFILE in strace — it has hit the per-process ceiling on open file descriptors. Every socket, pipe, and open file counts against that ceiling. The fix depends on two things: which limit is being hit (the process soft limit, the process hard limit, or the kernel-wide ceiling) and how the process was started, because a login shell, a systemd service, and a container each get their limits from a different place. Editing /etc/security/limits.conf and restarting nothing is the most common non-fix: systemd services never read that file. This guide finds the real ceiling first, then raises the right one.

What you’ll accomplish

  • Read the live limit of a running process straight from /proc, with no guessing.
  • Count how many descriptors a process actually holds, and check the system-wide total.
  • Raise the limit correctly for a systemd service, for login sessions, and for the kernel ceiling.
  • Verify the new value took effect after a restart.
  • Recognize when the real problem is a descriptor leak, not a limit that is too low.

Prerequisites

  • A modern Linux running systemd (Debian 10+, Ubuntu 18.04+, RHEL/AlmaLinux/Rocky 8+). Check with systemctl --version.
  • root or sudo. Reading /proc/<pid>/limits for your own processes needs no privilege; changing limits does.
  • Knowing how the affected process is managed. A systemd unit, a login shell, and a container each source limits differently, and that difference is the whole point.

Be honest about the distro split. On Debian and Ubuntu, PAM reads /etc/security/limits.conf and /etc/security/limits.d/ for interactive logins through pam_limits. RHEL-family systems behave the same for logins. But no systemd-managed service reads any of those files — they are governed by LimitNOFILE in the unit, regardless of distro. If your database or web server is started by systemd (almost all are), editing limits.conf changes nothing for it.

How it works

There are three layers, and each one has its own configuration source. A request for a new descriptor must pass all three.

Layered view of Linux open-file limits from the kernel fs.file-max ceiling down to a process soft limit, with each layer's config source labeled

The kernel ceiling is system-wide. fs.file-max caps the total number of open file handles across every process on the host; fs.nr_open caps the largest value any single process limit can be set to. On most modern kernels these default high (millions), so they are rarely the first wall you hit — but a per-process limit can never be raised above fs.nr_open.

The per-process rlimits are two numbers, a soft limit and a hard limit, for the RLIMIT_NOFILE resource. The soft limit is the one that is actually enforced: exceed it and the next open() returns EMFILE. The hard limit is the ceiling the soft limit may be raised to. An unprivileged process can raise its own soft limit up to its hard limit, but only a privileged process can raise the hard limit. The kernel API behind this is getrlimit/setrlimit.

How those rlimits get set depends entirely on how the process launched:

  • A login shell inherits limits from PAM’s pam_limits, driven by /etc/security/limits.conf and /etc/security/limits.d/. Inside that shell, ulimit -n reads or lowers the current soft limit.
  • A systemd service inherits LimitNOFILE from its unit (or DefaultLimitNOFILE from the manager config). PAM limits do not apply.
  • A container inherits whatever the container runtime sets, which may be far lower than the host default.

Diagnose the layer, then edit the matching source. That order avoids the classic wasted afternoon of editing a file the process never reads.

Step-by-step

Find the live limit of the running process. This is the ground truth — it reflects what the process actually inherited, not what any config file claims. Get the PID, then read its limits:

pidof nginx
cat /proc/1874/limits | grep 'open files'
Limit                     Soft Limit           Hard Limit           Units
Max open files            1024                 4096                 files

The soft limit here is 1024. That is the number the process hits when it prints the error. If this already shows a high value, the limit is not your problem — skip to the last section.

Count how many descriptors it currently holds. Compare this against the soft limit above to confirm you are actually near the ceiling:

ls /proc/1874/fd | wc -l
1017

At 1017 of 1024, the next few connections will fail. If the count is nowhere near the limit, the error is coming from a different process or a burst you are not catching live.

Check the system-wide total. Rule out the kernel ceiling before touching per-process limits:

cat /proc/sys/fs/file-nr
9856	0	9223372036854775807

The three fields are: handles currently allocated, allocated-but-unused (always 0 on modern kernels), and the fs.file-max maximum. Here 9,856 against a max in the quintillions — the kernel ceiling is nowhere in play, so this is a per-process fix.

Raise the limit for a systemd service with a drop-in. This is the correct place for any service systemd starts. Do not edit the shipped unit file; create an override:

sudo systemctl edit nginx.service

That opens an editor for a drop-in at /etc/systemd/system/nginx.service.d/override.conf. Add:

[Service]
LimitNOFILE=65536

Then reload the manager and restart the service so it re-reads the limit:

sudo systemctl daemon-reload
sudo systemctl restart nginx.service

A single LimitNOFILE=65536 sets both soft and hard to 65536. To set them independently, use LimitNOFILE=soft:hard, for example LimitNOFILE=65536:1048576.

Raise the limit for login sessions. This covers interactive shells and anything you launch by hand — not systemd services. Create a file in limits.d rather than editing limits.conf:

echo '* soft nofile 65536' | sudo tee /etc/security/limits.d/90-nofile.conf
echo '* hard nofile 65536' | sudo tee -a /etc/security/limits.d/90-nofile.conf

The change applies to new login sessions only. Log out and back in — an existing shell keeps its old limit. Confirm pam_limits is active for your login path; on most distros it already is via /etc/pam.d/common-session or the system-auth stack.

Raise the kernel ceiling only if file-nr proved you need to. Most hosts never need this. When you do, set it persistently:

echo 'fs.file-max = 2097152' | sudo tee /etc/sysctl.d/90-file-max.conf
sudo sysctl --system
fs.file-max = 2097152

If you also need per-process limits higher than the current fs.nr_open, raise that in the same file with fs.nr_open = 2097152. A per-process LimitNOFILE above fs.nr_open is silently capped.

Verify with ulimit -n. In a fresh login shell, confirm the soft limit for interactive processes:

ulimit -n
65536

Note that ulimit -n reflects your shell’s limit, which comes from PAM — it says nothing about a systemd service. Verify services separately, below.

Validate it works

After restarting the service, read its live limit again from /proc — this is the only check that proves the running process picked up the new value:

cat /proc/$(pidof nginx)/limits | grep 'open files'
Max open files            65536                65536                files

Cross-check what systemd believes it applied:

systemctl show nginx.service -p LimitNOFILE -p LimitNOFILESoft
LimitNOFILE=65536
LimitNOFILESoft=65536

If /proc and systemctl show agree on the higher number, the change is live. If systemctl show reports the new value but /proc still shows the old one, the process was not restarted — a reload is not enough for LimitNOFILE.

Troubleshooting

You edited limits.conf but the service is unchanged. This is the single most common failure. systemd services never read limits.conf or limits.d. Set LimitNOFILE in a unit drop-in instead, daemon-reload, and restart. Confirm with systemctl show <svc> -p LimitNOFILE.

You raised the soft limit but still hit a wall. A process can raise its soft limit only up to its hard limit. If the hard limit is 4096, ulimit -n 65536 fails with cannot modify limit: Operation not permitted. Raise the hard limit first (via limits.d for logins, or LimitNOFILE for services, which sets both), then the soft limit can follow.

ulimit -n in a script does nothing for a running daemon. ulimit changes only the current shell and its future children. A daemon that is already running, or one started by systemd rather than by your script, is unaffected. Set the limit where that process is actually launched, then restart it.

You cannot raise the limit past a certain point. fs.nr_open caps any single per-process limit. If LimitNOFILE=2000000 refuses to take effect and the process caps out lower, raise fs.nr_open in /etc/sysctl.d/ first, then re-apply.

The process runs in a container and ignores host limits. Container runtimes set their own RLIMIT_NOFILE inside the namespace. For Docker use --ulimit nofile=65536:65536 (or the Compose ulimits: key); for Kubernetes the node runtime and its systemd unit govern the default. Check the live limit from inside the container with the same /proc/<pid>/limits read.

Every service needs a higher default. Rather than a drop-in per unit, raise the manager-wide default. Set DefaultLimitNOFILE=65536 in /etc/systemd/system.conf (and user.conf for user services), then reboot or systemctl daemon-reexec. Per-unit LimitNOFILE still overrides it where set.

When this is the wrong approach

If the descriptor count climbs steadily and never falls, you have a leak, not a limit that is too low. Raising LimitNOFILE just moves the crash from Tuesday to Friday. A healthy service opens and closes descriptors in a stable band; a leaking one only ever grows. Watch it over time with the /proc/<pid>/fd count, and when it is high, find what is held open:

lsof -p $(pidof myapp) | awk '{print $5, $9}' | sort | uniq -c | sort -rn | head

If the top entries are hundreds of identical sockets in CLOSE_WAIT, or the same log file opened thousands of times, the application is not closing what it opens. That is a code or configuration bug — fix the leak. A higher limit is the right move only when a correct, non-leaking process genuinely needs more concurrent descriptors, such as a busy reverse proxy or database handling tens of thousands of connections.

For related failure modes, see why the OOM killer took the wrong process and reading systemd logs with journalctl. The kernel API is documented in getrlimit(2), and the systemd directives in systemd.exec(5).

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.