How to Check and Increase File Descriptor Limits in Ubuntu Using ulimit -n

In Unix and Linux architectures, “everything is a file”—including local disk files, network sockets, named pipes, and hardware devices. Every time a web server (like Nginx or Apache), database engine (like PostgreSQL or MySQL), or container runtime opens a connection or accesses storage, the Ubuntu kernel assigns it a file descriptor integer. By default, Linux imposes protective limits on the maximum number of concurrent file descriptors a process or user can open. Under high concurrency, reaching this threshold triggers the notorious error: Too many open files.

Understanding Soft vs Hard Limits

The Linux kernel enforces two tiers of file descriptor thresholds via the ulimit subsystem:

  • Soft Limit: The effective operational limit currently enforced on the session or application. An unprivileged user can freely increase their soft limit up to the ceiling defined by the hard limit.
  • Hard Limit: The absolute maximum boundary established by the system administrator. Only the root user (or a process with CAP_SYS_RESOURCE privileges) can increase the hard limit.

Checking Current File Descriptor Limits with ulimit -n

To inspect your current shell session thresholds in the Ubuntu terminal:

  1. Open the Terminal (press Ctrl + Alt + T).
  2. Check the active soft limit:
    ulimit -n
    (Default desktop Ubuntu installations typically return 1024).
  3. Check the active hard limit:
    ulimit -Hn
    (Often configured between 1048576 and 524288).
  4. View all system resource limits simultaneously:
    ulimit -a

Temporarily Increasing Limits for the Current Session

If you are compiling software or executing an intensive script in the current shell, you can temporarily elevate the soft limit up to the hard ceiling:

ulimit -n 65536

This command takes effect immediately for the active terminal window and any child processes spawned from it. However, closing the terminal reverts the threshold back to the default setting.

Permanently Configuring Limits via limits.conf

To permanently increase file descriptor quotas across all user logins, configure the PAM security limits configuration file:

  1. Open /etc/security/limits.conf in your preferred terminal text editor with administrative privileges:
    sudo nano /etc/security/limits.conf
  2. Scroll to the bottom of the file and append the following directives (replacing username with your account name or using * for all users):
    * soft nofile 65536
    * hard nofile 65536
    root soft nofile 65536
    root hard nofile 65536
  3. Save the file (press Ctrl + O, then Enter) and exit (Ctrl + X).
  4. Log out of your session and log back in, or restart Ubuntu for PAM to apply the updated thresholds globally.

Increasing Limits for Systemd Daemons and Services

Modern Ubuntu utilizes systemd to manage system services (such as Docker, Redis, or Nginx). Daemons launched by systemd bypass limits.conf entirely. To increase file descriptors for system services:

  1. To set a global ceiling for all systemd services, edit /etc/systemd/system.conf:
    sudo nano /etc/systemd/system.conf
  2. Uncomment and adjust the default limit directive:
    DefaultLimitNOFILE=65536:524288
  3. Reload the systemd daemon:
    sudo systemctl daemon-reexec

Get the best tech tips delivered straight to your inbox.

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