How to Fix Too Many Open Files Error in Linux

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).

  1. Open your Linux terminal.
  2. Check the active user soft limit for open files:
    ulimit -n
  3. Check the current hard limit:
    ulimit -Hn
  4. 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.

  1. Inspect system-wide file descriptor allocations by checking the kernel parameter:
    cat /proc/sys/fs/file-nr
    (The three output values represent: allocated descriptors, unused allocated descriptors, and maximum allowed descriptors).
  2. Identify which specific process ID (PID) holds the most open handles:
    lsof | awk '{print $1, $2}' | sort | uniq -c | sort -rn | head -15
  3. 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.

  1. Open the limits configuration file in your preferred text editor:
    sudo nano /etc/security/limits.conf
  2. 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
  3. Save the file by pressing Ctrl + O, then Enter, and exit with Ctrl + X.
  4. Ensure PAM limits are loaded across session logins by verifying that /etc/pam.d/common-session contains:
    session required pam_limits.so
  5. 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.

  1. Create an override configuration for your specific service (for example, Nginx or Docker):
    sudo systemctl edit nginx
  2. In the override editor, add the LimitNOFILE parameter under the Service block:
    [Service]
    LimitNOFILE=65535
  3. Save and close the override file.
  4. Reload the systemd daemon and restart the affected service:
    sudo systemctl daemon-reload
    sudo systemctl restart nginx
  5. Confirm that the live daemon has received the new limit:
    cat /proc/$(pgrep -f nginx | head -1)/limits | grep "open files"

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.