ulimit on Linux: Fix “Too Many Open Files” (nofile, limits.conf, systemd)

ulimit open files linux - custom-ulimit-featured.png

ulimit open files is a classic production error — Nginx, Java, Elasticsearch, and databases hit the default ceiling and start refusing connections. The fix is raising nofile at the right layer: shell, PAM, or systemd unit.

Related: lsof,
systemctl,
Nginx guide.

Read Current Limits

ulimit -a shows the invoking shell; services see their own limits.

bashcheck
ulimit -a | grep -i 'open files'
cat /proc/$$/limits | grep 'Max open files'
ulimit -a
nofile

Soft vs Hard Limits

Users can raise soft up to hard; root can raise hard — know which applies to daemons.

soft
hard

Symptom: Too Many Open Files

Often appears in Nginx error.log or application stack traces under load.

bashcount
sudo lsof -p $(pidof nginx | awk '{print $1}') | wc -l
nginx
EMFILE

/etc/security/limits.conf

Persistent per-user limits when PAM loads pam_limits — typical for login shells and some services.

bashsnippet
# /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
limits.conf
* soft nofile

systemd LimitNOFILE

Most distro services ignore limits.conf unless configured — set overrides on the unit.

bashoverride
sudo systemctl edit nginx
# [Service]
# LimitNOFILE=65535
sudo systemctl daemon-reload && sudo systemctl restart nginx
drop-in
LimitNOFILE

prlimit for One-Off Tests

Raise limits for a single command without editing global config.

bashprlimit
prlimit --nofile=65535:65535 -- ./my-app
prlimit
temporary

Verify Under Load

Confirm the running process inherited the new ceiling after restart.

bashverify
systemctl show nginx -p LimitNOFILE
sudo cat /proc/$(pidof nginx | head -1)/limits | grep 'Max open files'
systemctl show
Max open files

Quick Reference

  • Shell: ulimit -n (session only)
  • Services: LimitNOFILE= in systemd drop-in
  • Debug with lsof and /proc/PID/limits

Related tutorials

Diagrams are original illustrations by Gnome IT Solutions. Tutorial text © Gnome IT Solutions.