How to Fix Bash Too Many Open Files Error in Linux

Running high-concurrency network servers, databases, or file-intensive compilation scripts on Linux can suddenly trigger the terminal error: -bash: ...: Too many open files, accompanied in software logs by error codes EMFILE or ENFILE. In Unix-like operating systems, “everything is a file” – meaning network sockets, database connections, disk files, pipes, and event loops each consume an allocated integer handle called a file descriptor. When a user or daemon exhausts its assigned file descriptor allowance, the kernel rejects all new socket and file allocation requests.

Inspect Current File Descriptor Limits via ulimit

To verify the active file descriptor limits enforced on your current shell session:

  1. Check the soft file descriptor limit (the practical limit enforced on active processes):
    ulimit -n
  2. Check the hard limit (the maximum ceiling the user can elevate to without root privileges):
    ulimit -Hn
  3. On many default Linux server installations, ulimit -n is set conservatively to 1024. For modern web applications, reverse proxies, and microservices, 1024 file descriptors can be exhausted in seconds during traffic spikes.
  4. To raise the limit temporarily in your current shell session:
    ulimit -n 65535

Identify Which Process Is Exhausting File Descriptors

Before expanding system limits, verify whether an application suffers from an unclosed socket or unclosed file handle memory leak:

  1. Count open file descriptors across all running processes:
    sudo lsof | wc -l
  2. Find the top 10 processes holding the highest number of open file descriptors:
    sudo lsof | awk '{print $1, $2}' | sort | uniq -c | sort -nr | head -n 10
  3. Inspect specific file paths held open by a suspicious process ID (PID):
    sudo ls -l /proc/PID/fd | wc -l
  4. If an application has tens of thousands of dangling sockets pointing to closed remote connections, restart the service to clear the leak.

Permanently Increase User Limits in /etc/security/limits.conf

To ensure updated file descriptor thresholds persist across user logins and system reboots, configure PAM security limits:

  1. Open /etc/security/limits.conf in an editor with administrative privileges:
    sudo nano /etc/security/limits.conf
  2. Add the following directives at the bottom of the file (replace username with your active Linux user, or use * for all standard users):
    *          soft    nofile         65535
    *          hard    nofile         65535
    root       soft    nofile         65535
    root       hard    nofile         65535
  3. Save and exit the file.
  4. Ensure PAM session limits are enabled in /etc/pam.d/common-session and /etc/pam.d/common-session-noninteractive by verifying the presence of:
    session required pam_limits.so
  5. Log out and log back in to apply the updated limits. Verify with ulimit -n.

Configure File Descriptor Limits for systemd Services

Modern Linux distributions manage background daemons (such as Nginx, Apache, MySQL, or Redis) via systemd, which completely ignores limits.conf:

  1. To raise limits for a specific service (for example, Nginx), edit its systemd override configuration:
    sudo systemctl edit nginx
  2. Add the following block into the override editor:
    [Service]
    LimitNOFILE=65535
  3. Save and exit. Reload systemd and restart the service:
    sudo systemctl daemon-reload
    sudo systemctl restart nginx
  4. Verify the live limits of the running daemon:
    cat /proc/$(pgrep -f nginx | head -n 1)/limits | grep "Max open files"

Raise Global System-Wide File Limits via sysctl

On high-throughput cluster nodes, ensure the kernel-wide total open file allocation table (fs.file-max) is configured for enterprise scale:

  1. Check the current kernel maximum open file allowance:
    cat /proc/sys/fs/file-max
  2. If the value is low, elevate it permanently:
    echo "fs.file-max = 2097152" | sudo tee -a /etc/sysctl.conf
  3. Apply kernel changes immediately without rebooting:
    sudo sysctl -p

Get the best tech tips delivered straight to your inbox.

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