How to Permanently Increase Open Files Limit (nofile) in Linux: Solving the 1024 Default Issue


1 views

Many Linux users encounter this frustrating scenario: you've edited /etc/security/limits.conf, rebooted, yet ulimit -n still shows the default 1024 value. Let's explore why this happens and how to properly set permanent limits.

The system has multiple layers where file limits can be configured:

1. Kernel parameters (fs.file-max)
2. Systemd limits (if using systemd)
3. PAM limits (/etc/security/limits.conf)
4. Shell configuration files

For Debian/Ubuntu systems with systemd, follow this comprehensive approach:

# Step 1: Edit global limits
sudo nano /etc/security/limits.conf
* soft nofile 64000
* hard nofile 64000

# Step 2: Configure systemd (required for services)
sudo nano /etc/systemd/system.conf
DefaultLimitNOFILE=64000

# Step 3: Configure pam_limits
sudo nano /etc/pam.d/common-session
session required pam_limits.so

# Step 4: Apply changes without full reboot
sudo systemctl daemon-reload
sudo sysctl -p

After making these changes, test with:

# Check current session
ulimit -n

# Check system-wide maximum
cat /proc/sys/fs/file-max

# Check for a specific process
cat /proc/PID/limits | grep "Max open files"
  • SSH sessions may need UsePAM yes in /etc/ssh/sshd_config
  • Some applications (like MySQL) have their own ulimit settings
  • Check for conflicting settings in /etc/security/limits.d/

For web servers needing high connection counts:

# Create custom override
sudo systemctl edit nginx.service

[Service]
LimitNOFILE=64000

# Then reload and restart
sudo systemctl daemon-reload
sudo systemctl restart nginx

When working with high-performance servers or applications that require numerous file handles (like web servers or database systems), the default 1024 open files limit can be restrictive. While adding entries to /etc/security/limits.conf is a common solution, many administrators find the changes don't persist after reboot on Debian-based systems.

Here's the full procedure to make the change permanent:

# Step 1: Edit limits.conf
sudo nano /etc/security/limits.conf
# Add these lines:
* soft nofile 64000
* hard nofile 64000
root soft nofile 64000
root hard nofile 64000

# Step 2: Edit sysctl.conf
sudo nano /etc/sysctl.conf
# Add this line:
fs.file-max = 100000

# Step 3: Create systemd override
sudo mkdir -p /etc/systemd/system.conf.d/
sudo nano /etc/systemd/system.conf.d/limits.conf
# Add:
[Manager]
DefaultLimitNOFILE=64000

# Step 4: Apply changes
sudo sysctl -p
sudo systemctl daemon-reed

The solution addresses multiple layers:

  • limits.conf sets per-user limits
  • sysctl.conf increases the system-wide maximum
  • Systemd override ensures the limit applies to services

To verify the new limits:

# Check current session
ulimit -n

# Check system-wide maximum
cat /proc/sys/fs/file-max

# Check hard limit
ulimit -Hn

For services like Nginx or MySQL, you might need additional configuration:

# Nginx example in /etc/nginx/nginx.conf
worker_rlimit_nofile 30000;

# MySQL example in my.cnf
[mysqld]
open_files_limit = 65535