How to Fix “Too Many Open Files” in Ubuntu by Increasing systemd LimitNOFILE

High-concurrency web servers, database clusters, reverse proxies, and microservices in Ubuntu frequently handle thousands of simultaneous TCP connections and storage streams. Under heavy production workloads, daemons like Nginx, Node.js, Redis, or PostgreSQL may suddenly crash or refuse new connections with the fatal operating system error: java.io.IOException: Too many open files or socket() failed (24: Too many open files).

In modern versions of Ubuntu, background services are managed by systemd, which ignores traditional limits defined in /etc/security/limits.conf. To resolve this bottleneck permanently, you must increase the service-specific LimitNOFILE parameter inside systemd unit override drop-ins.

Checking Current File Descriptor Limits for a Service

Before applying changes, inspect the active file descriptor limits imposed on your target process:

  1. Find the main process ID (PID) of your running service:
    systemctl status nginx
  2. Inspect the process limit table in the virtual proc filesystem (replacing 1234 with your actual PID):
    cat /proc/1234/limits | grep "Max open files"

You will typically observe a Soft Limit of 1024 or 4096, which severely restricts high-traffic web infrastructure.

Increasing LimitNOFILE Using systemctl edit

The standard, safe method to modify systemd parameters without altering package-managed files in /lib/systemd/system/ is creating an override drop-in file:

  1. Execute the systemd editor command for your service (for example, Nginx):
    sudo systemctl edit nginx
  2. In the empty text editor buffer that opens, insert the following configuration block:
    [Service]
    LimitNOFILE=65536
  3. Save the file (Ctrl + O, then Enter in nano) and exit (Ctrl + X).

systemd automatically saves the override to /etc/systemd/system/nginx.service.d/override.conf and reloads its daemon configuration.

Restarting the Service and Verifying the New Limit

Restart the service to apply the increased file descriptor allocation:

sudo systemctl restart nginx

Now verify that the service adopted the 65,536 limit directly via systemd inspection:

systemctl show nginx | grep LimitNOFILE

The command will output LimitNOFILE=65536 and LimitNOFILESoft=65536, ensuring your server can sustain massive connection volumes without exhausting file handles.

Applying Global Limits for All systemd User Sessions

If you run developer containers or scripts under standard user accounts that trigger the same error, edit the global systemd manager configuration:

  1. Open /etc/systemd/system.conf:
    sudo nano /etc/systemd/system.conf
  2. Uncomment and update the default file limit directive:
    DefaultLimitNOFILE=65536:524288
  3. Save and execute sudo systemctl daemon-reexec to apply globally.

Get the best tech tips delivered straight to your inbox.

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