Linux4 min read· 3 practice problems

Too many open files — causes and fixes (ulimit, systemd LimitNOFILE, fd leaks)

When you see Too many open files, EMFILE or accept4: too many open files: how to read a service's real fd limit, raise it with systemd LimitNOFILE or container ulimits, and find a file descriptor leak.

On this page
  1. Short answer: the process used up its file descriptor limit
  2. Same problem, different message per runtime
  3. Diagnosis: check the process's limit, not your shell's
  4. Cause 1: the systemd service limit — LimitNOFILE
  5. In containers: Docker and Kubernetes
  6. Cause 2: an fd leak
  7. The system-wide limit comes last
  8. What not to do
  9. The order to work in
  10. Quick diagnostic checklist

Short answer: the process used up its file descriptor limit

Too many open files is the kernel error EMFILE (errno 24): one process already holds as many fds as its own limit (RLIMIT_NOFILE) allows. Sockets are fds too, so under load new connections fail first. The cause is usually one of two:

  1. The limit is too low — a systemd service or container runs with the default of 1024
  2. fds are leaking — unclosed files or sockets keep growing over time

Hitting the system-wide limit (fs.file-max) gives a different message: Too many open files in system (ENFILE).

Same problem, different message per runtime

  • Go: accept tcp [::]:8080: accept4: too many open files
  • nginx: accept4() failed (24: Too many open files), socket() failed (24: Too many open files)
  • Java: java.net.SocketException: Too many open files
  • Node.js: Error: EMFILE: too many open files, open '/app/data/report.json'

Diagnosis: check the process's limit, not your shell's

ulimit -n in your shell shows your shell's limit only. The service runs with whatever systemd or the container runtime gave it.

PID=$(systemctl show -p MainPID --value api)
grep "Max open files" /proc/$PID/limits
ls /proc/$PID/fd | wc -l
Max open files            1024                 524288               files
1021
  • Open fds sit right at the soft limit (1024) → it really is hitting the limit
  • After raising the limit, the count keeps climbing over time → a leak (cause 2)

Cause 1: the systemd service limit — LimitNOFILE

/etc/security/limits.conf applies to login sessions (PAM) only, not to systemd services. Raise it in the unit.

# /etc/systemd/system/api.service.d/override.conf  (the same file systemctl edit api creates)
[Service]
LimitNOFILE=65536

systemctl daemon-reload
systemctl restart api
grep "Max open files" /proc/$(systemctl show -p MainPID --value api)/limits

The new limit applies only after a restart. For nginx, raise the worker limit with worker_rlimit_nofile and keep worker_connections within it.

In containers: Docker and Kubernetes

docker run --ulimit nofile=65536:65536 api:1.4.2
docker exec api sh -c 'ulimit -n'

The Kubernetes pod spec has no ulimit field. Containers inherit the defaults of the node's container runtime (containerd, CRI-O), so check the real value with cat /proc/1/limits inside the pod and change it in the node's runtime configuration.

Cause 2: an fd leak

If the same error returns hours or days after raising the limit, it is a leak. See what is piling up.

lsof -p $PID | awk '{print $5}' | sort | uniq -c | sort -rn | head
ss -tanp state close-wait | grep -c "pid=$PID"

Growing REG entries mean unclosed files. Growing IPv4, IPv6 or sock entries with CLOSE_WAIT piling up mean HTTP or DB connections that were read but never closed. Look for a missing close or a connection not returned to the pool.

The system-wide limit comes last

cat /proc/sys/fs/file-nr      # allocated · 0 · maximum (fs.file-max)

Touch fs.file-max only for Too many open files in system. It does nothing for EMFILE.

What not to do

  • Raising ulimit -n in your shell and calling it fixed — the service limit has not changed.
  • Hiding a leak behind a higher limit — it only delays the outage.

The order to work in

  1. Compare the limit and usage of that process: /proc/<pid>/limits and ls /proc/<pid>/fd | wc -l
  2. systemd: LimitNOFILE= → daemon-reload → restart; containers: --ulimit or runtime settings
  3. After the restart, confirm the new value in /proc/<pid>/limits
  4. Record the fd count over time; if it keeps rising, trace the leak with lsof -p

To practise the same situation as a problem, start with the one-minute quiz Too many open files: new connections fail under load.

Quick diagnostic checklist

  • Tell Too many open files (per process) from Too many open files in system (system-wide).
  • Read the service's limit in /proc/<pid>/limits, not ulimit -n in your shell.
  • Compare the open fd count from ls /proc/<pid>/fd | wc -l with the limit.
  • For a systemd service, set LimitNOFILE= in an override.conf, run daemon-reload and restart.
  • Remember that /etc/security/limits.conf does not apply to systemd services.
  • For containers, change the limit with docker run --ulimit nofile= or the node's runtime settings.
  • Record the fd count over time and, if it keeps rising, use lsof -p to see what leaks.
  • Adjust fs.file-max only for Too many open files in system.

Practice with this guide

Reviewed problems about the same failure. Write the cause, recovery, and prevention yourself, then compare with the model answer.

Read next