The “Too many open files” error is a pervasive issue in Linux environments, often causing applications to crash unexpectedly or fail to start. This occurs when a process attempts to open more file descriptors than the system limit allows, either globally for all users or specifically for that user. For IT students and junior engineers, understanding the distinction between the ulimit (per-process limit) and /proc/sys/fs/file-max (system-wide limit) is crucial. This guide provides a concrete lab scenario to diagnose and fix this issue using standard shell commands, configuration files, and safe testing procedures.
Understanding the File Descriptor Limits
Before diving into solutions, it is essential to understand the three layers of file descriptor limits in Linux:
1. fs.file-max: The global maximum number of open file handles the kernel can support for the entire system. This is defined in /proc/sys/fs/file-max.
2. fs.nr-open-files (per-user): The limit for a specific user, found in /proc/sys/fs/nr-open.
3. ulimit -n: The soft and hard limits enforced by the shell for individual processes. This is what most applications hit first.
When an application like a web server (Nginx/Apache) or a database (PostgreSQL/MariaDB) fails, it is almost always because the per-user ulimit has been reached, not the system-wide limit.
Diagnosing the Issue in a Lab Environment
Imagine you are working on a development server running Ubuntu 22.04 LTS. You have deployed a Node.js application that handles high-concurrency requests. Suddenly, the application logs show repeated EMFILE (Error: Too many open files) exceptions, and new connections start being dropped.
To diagnose this without restarting services immediately, follow these steps:
Step 1: Check Current Limits
Run the following command to see the current limits for your user session:
ulimit -n
Expected Output:
1024
This indicates that any process started by your user can open a maximum of 1,024 files. If the application is handling many sockets or temporary files, this number may be insufficient.
You can also inspect the system-wide limits:
cat /proc/sys/fs/file-max
cat /proc/sys/fs/nr-open
Step 2: Identify Resource Hogs
To see which processes are consuming the most file descriptors, use lsof (List Open Files). If lsof is not installed, install it using your package manager:
sudo apt update
sudo apt install lsof -y
Now, identify the top consumers:
lsof +c 0 | awk '{print $1}' | sort | uniq -c | sort -n | tail -5
Illustrative Output:
980 node
12 nginx
5 mysql
In this scenario, the node process is holding nearly 1,000 open files. This confirms that the application has hit its soft limit.
Resolving the Error: Adjusting User Limits
The most common fix involves increasing the ulimit value for your user account. This change affects new sessions but not existing ones.
Step 3: Modify Shell Configuration
Edit your current shell’s configuration file. For Bash users, this is typically ~/.bashrc. Add the following line to increase the limit to 65,536 (a common safe default for high-load servers):
echo "ulimit -n 65536" >> ~/.bashrc
source ~/.bashrc
Note: If you are using Zsh, edit ~/.zshrc instead. The command ulimit -n sets the soft limit. You should also set the hard limit to ensure the soft limit can be raised above 1024.
ulimit -n 65536
ulimit -Hn 65536
Step 4: System-Wide Adjustment (Optional)
If you are managing a dedicated server where all users need higher limits, or if the application runs as a specific service user (like www-data), you may need to adjust system files. Edit /etc/security/limits.conf and add lines for your username or globally:
* soft nofile 65536
* hard nofile 65536
Note: Changes here require a reboot or a logout/login cycle to take effect.
Application-Specific Configuration
Increasing the OS limit is not enough; the application itself must be configured to respect the new limits and manage file handles efficiently.
Step 5: Configure Node.js Applications
If you are running a Node.js app, ensure it isn’t leaking file descriptors. You can also explicitly set the handle size in the environment variables if your runtime supports it, though fixing the code is preferred. However, for services managed by systemd, you might need to adjust the service unit file:
sudo systemctl edit <your-service-name>
Add the following lines inside the [Service] section:
LimitNOFILE=65536
Step 6: Configure Nginx or Apache
Web servers often have their own worker process limits. In Nginx, check nginx.conf:
worker_rlimit_nofile 65536;
Restart the service after making changes:
sudo systemctl restart nginx
Verification and Common Pitfalls
After applying the fixes, verification is critical to ensure stability.
Step 7: Verify the Changes
Open a new terminal session (or source your config file again) and run:
ulimit -n
ulimit -Hn
Expected Output:
65536
65536
Now, simulate high load. You can create many temporary files to test the limit:
for i in {1..70000}; do touch /tmp/test_$i; done
echo "Success: Created 70000 files"
If this completes without error, your system is ready for high-concurrency workloads.
Common Mistakes to Avoid
- Editing the wrong file: Modifying
/etc/security/limits.confrequires a reboot or session restart. If you expect immediate changes, edit~/.bashrcinstead. - Ignoring hard limits: Setting only the soft limit (
ulimit -n) without adjusting the hard limit (ulimit -Hn) can cause issues if the process tries to exceed the soft limit temporarily. - Forgetting service restarts: Changes to
systemdunit files or Nginx configs do not take effect until the service is restarted. - Assuming global limits apply to all users: Always check if the application runs as a specific user (e.g.,
postgres,www-data) and ensure that user’s limits are configured correctly in/etc/security/limits.conf.
Student Practice Task
To reinforce these concepts, try the following exercise in your lab environment:
1. Create a new user named testuser: sudo adduser testuser.
2. Switch to that user: sudo su - testuser.
3. Run ulimit -n and confirm it is low (usually 1024).
4. Edit ~/.bashrc for testuser, adding ulimit -n 65536.
5. Source the file: source ~/.bashrc.
6. Verify the new limit.
7. Start a simple Python script that opens many files and observe if it crashes before and after the fix.
By mastering these steps, you will be able to troubleshoot file descriptor exhaustion issues effectively in both academic labs and production environments.
Educational-use notice: This tutorial is for learning and controlled lab use only. Test carefully, use backups, and obtain authorization before making changes. Do not run unfamiliar commands on production systems. We accept no responsibility for misuse, damage, data loss, or production issues.

