When running database engines, web servers, microservices, or complex build pipelines on Linux, you may suddenly encounter the operating system error: “socket: too many open files” or “java.io.IOException: Too many open files (Errno 24)”. In Unix-like architectures, everything is treated as a file, including disk files, network sockets, named pipes, and directory handles. This error signals that a process or user has exhausted its allocated file descriptor limit. Here is how to diagnose file descriptor consumption and raise system limits permanently.
Inspect Current File Descriptor Limits
Linux enforces two distinct limits: a soft limit (the threshold processes are warned at or capped by default) and a hard limit (the absolute ceiling non-root users cannot exceed).
- Open your Linux terminal.
- Check the active user soft limit for open files:
ulimit -n - Check the current hard limit:
ulimit -Hn - On many distributions (such as Ubuntu, Debian, and CentOS), the default soft limit is set conservatively to 1024, which high-concurrency applications like Nginx, Node.js, or Elasticsearch rapidly exhaust.
Identify Which Process Is Exhausting File Descriptors
Before blindly increasing system thresholds, verify whether an application has an unclosed socket leak or anomalous file handle accumulation.
- Inspect system-wide file descriptor allocations by checking the kernel parameter:
(The three output values represent: allocated descriptors, unused allocated descriptors, and maximum allowed descriptors).cat /proc/sys/fs/file-nr - Identify which specific process ID (PID) holds the most open handles:
lsof | awk '{print $1, $2}' | sort | uniq -c | sort -rn | head -15 - Inspect the specific files and sockets held by a suspected process PID:
ls -l /proc/[PID]/fd | wc -l
Increase User and Process Limits Permanently via limits.conf
To ensure updated file descriptor caps survive user logouts and system reboots, modify PAM limits configuration.
- Open the limits configuration file in your preferred text editor:
sudo nano /etc/security/limits.conf - Scroll to the end of the file and append the following directives (adjusting thresholds to 65535 or higher according to server demands):
* soft nofile 65535 * hard nofile 65535 root soft nofile 65535 root hard nofile 65535 - Save the file by pressing
Ctrl + O, then Enter, and exit withCtrl + X. - Ensure PAM limits are loaded across session logins by verifying that
/etc/pam.d/common-sessioncontains:session required pam_limits.so - Log out and log back in, then verify the change with
ulimit -n.
Configure Systemd Service Limits for Daemon Applications
Modern Linux distributions run system daemons under systemd, which ignores limits.conf and applies its own service-level defaults.
- Create an override configuration for your specific service (for example, Nginx or Docker):
sudo systemctl edit nginx - In the override editor, add the
LimitNOFILEparameter under the Service block:[Service] LimitNOFILE=65535 - Save and close the override file.
- Reload the systemd daemon and restart the affected service:
sudo systemctl daemon-reload sudo systemctl restart nginx - Confirm that the live daemon has received the new limit:
cat /proc/$(pgrep -f nginx | head -1)/limits | grep "open files"