How to set and check file descriptor limits
Topic: Servers linux
Summary
Check and raise the open file limit (nofile) for the shell with ulimit, and for a systemd service or system-wide with limits.conf or the unit file. Use this when you see 'too many open files' or when running high-connection services.
Intent: How-to
Quick answer
- ulimit -n shows current process limit for open files; ulimit -Sn (soft) and ulimit -Hn (hard). In a shell: ulimit -n 65535 to raise for that session (up to hard limit).
- Permanent for a user: add to /etc/security/limits.conf (e.g. deploy soft nofile 65535, deploy hard nofile 65535). For systemd service: add LimitNOFILE=65535 in the unit [Service] section.
- System-wide default: /etc/security/limits.conf or a file in /etc/security/limits.d/. Verify with ulimit -n in the shell or cat /proc/PID/limits for a running process.
Prerequisites
Steps
-
Check current limits
ulimit -n (current process); ulimit -Sn and ulimit -Hn for soft and hard. For another process: cat /proc/PID/limits | grep open.
-
Raise for shell session
ulimit -n 65535 (cannot exceed hard limit). To raise hard limit, use ulimit -Hn 65535 first (if allowed by system limits), then ulimit -n 65535.
-
Make persistent
User: add deploy soft nofile 65535 and deploy hard nofile 65535 to /etc/security/limits.conf or a file in /etc/security/limits.d/. systemd unit: LimitNOFILE=65535 in [Service]; daemon-reload and restart.
Summary
Check limits with ulimit -n and /proc/PID/limits; raise for a session with ulimit; make persistent with limits.conf for users or LimitNOFILE for systemd units. Use this when you hit “too many open files” or when tuning high-connection services.
Prerequisites
- Understanding users, groups, and permissions.
- How to manage services with systemd.
- Shell and essential commands.
Steps
Step 1: Check current limits
ulimit -n
ulimit -Sn
ulimit -Hn
For a process: cat /proc/PID/limits | grep open.
Step 2: Raise for shell session
ulimit -n 65535
Step 3: Make persistent
Add to /etc/security/limits.d/ for users, or LimitNOFILE= in the systemd unit; reload and restart.
Verification
- ulimit -n (or /proc/PID/limits) shows the new limit; the app no longer reports “too many open files”.
Troubleshooting
Cannot raise above current hard — Edit limits.conf or systemd to set a higher hard limit; re-login or restart the service. systemd service ignores limits.conf — Set LimitNOFILE in the unit; limits.conf applies to PAM logins, not necessarily to services started by systemd.