Home/Engineer/Troubleshooting/Too many open files — Fixing…
TroubleshootingIntermediateUbuntuDebianCentOSRHELulimitToo many open filesEMFILE

Too many open files — Fixing ulimit File Descriptor Limits

A guide to resolving file descriptor limit errors such as EMFILE: too many open files and accept4() failed (24: Too many open files) using ulimit, limits.conf, and systemd LimitNOFILE.

Contents
  1. Check the current limits
  2. Immediate fix — raise the limit for the current session
  3. Permanent fix 1 — /etc/security/limits.conf
  4. Permanent fix 2 — systemd services (recommended)
  5. Permanent fix 3 — system-wide kernel parameter
  6. Nginx-specific settings
  7. Root cause — check for fd leaks

The Too many open files error occurs when a process hits the limit on how many file descriptors (fds) it can open. Sockets, pipes, and regular files all consume fds.


Check the current limits

Bash
# soft/hard
ulimit -Sn # soft limit
ulimit -Hn # hard limit

# when
cat /proc/sys/fs/file-max
sysctl fs.file-max
Bash
# fd
ls /proc/PID/fd | wc -l

# fd TOP 10
sudo lsof 2>/dev/null | awk '{print $2}' | sort | uniq -c | sort -rn | head -10

Immediate fix — raise the limit for the current session

Bash
# (when when )
ulimit -n 65535

# check
ulimit -n

Permanent fix 1 — /etc/security/limits.conf

Bash
sudo nano /etc/security/limits.conf
INI
#
*         soft    nofile    65535
*         hard    nofile    65535
root      soft    nofile    65535
root      hard    nofile    65535

# User when
deploy    soft    nofile    65535
deploy    hard    nofile    65535
Bash
# PAM limits check
grep "pam_limits" /etc/pam.d/common-session
#
echo "session required pam_limits.so" | sudo tee -a /etc/pam.d/common-session

You must log out and reconnect for the change to take effect.


Services managed by systemd do not pick up limits.conf. Set the limit directly in the service file.

Bash
# override file
sudo systemctl edit nginx # or
INI
[Service]
LimitNOFILE=65535
Bash
sudo systemctl daemon-reload
sudo systemctl restart nginx

# check
sudo cat /proc/$(pgrep nginx | head -1)/limits | grep "open files"

Permanent fix 3 — system-wide kernel parameter

Bash
# when
sudo sysctl -w fs.file-max=2097152

#
echo "fs.file-max = 2097152" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

Nginx-specific settings

Nginx can also be configured directly in nginx.conf.

Nginx
# /etc/nginx/nginx.conf
worker_processes auto;
worker_rlimit_nofile 65535;

events {
  worker_connections 4096;
}
Bash
sudo nginx -t && sudo systemctl reload nginx

Root cause — check for fd leaks

If errors continue after raising the limit, you may have an fd leak.

Bash
# fd list check
sudo lsof -p PID | head -50

# fd (10 )
watch -n 10 "ls /proc/PID/fd | wc -l"

If the fd count keeps growing, the application is not closing files or sockets — a bug in the code.

#ulimit#Too many open files#EMFILE#file descriptor#Troubleshooting#systemd#limits.conf
Editorial note

AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us

Topic-related official docs (auto-linked · not per-claim sources)Linux man pages ↗

Questions & answers

Ask a question about this guide. We'll review and reply.