Home LinuxToo Many Open Files Error in Linux: Causes, Diagnosis, and Fixes

Too Many Open Files Error in Linux: Causes, Diagnosis, and Fixes

by Ardra Shaji
Laptop screen shows overlapping error windows with a bold red ERROR banner; caption reads 'Too Many Open Files Error in Linux'.

Linux systems are designed to handle thousands of processes and file operations efficiently. However, administrators and developers occasionally encounter the frustrating “Too Many Open Files” error. This issue can disrupt applications, database servers, web services, and backup operations if not addressed properly.

In this guide, we’ll explore what the error means, its common causes, how to diagnose it, and practical ways to fix and prevent it in Linux environments.

What Does “Too Many Open Files” Mean?

In Linux, every open file, socket, pipe, or network connection uses something called a file descriptor. Each running process has a limit on how many file descriptors it can open simultaneously.

When a process exceeds this limit, Linux returns errors such as:

Too many open files

or

EMFILE (Too many open files)

This problem is common in:

  • Web servers handling heavy traffic
  • Database servers
  • Backup applications
  • File synchronization tools
  • Applications with file descriptor leaks

Understanding File Descriptors in Linux

A file descriptor is simply an integer that Linux uses to track open files and resources.

Examples include:

  • Regular files
  • Directories
  • Network sockets
  • Pipes
  • Devices

Linux manages two important limits:

1. Soft Limit

The current operational limit for a user or process.

2. Hard Limit

The maximum value the soft limit can be increased to.

You can check these limits using:

ulimit -Sn   # Soft limit
ulimit -Hn   # Hard limit

To view all limits:

ulimit -a

Common Causes of the Error

1. Low Default File Descriptor Limits

Many Linux distributions ship with conservative defaults such as:

1024

This may not be enough for modern applications.

For example:

  • Nginx handling thousands of requests
  • MySQL managing concurrent connections
  • Backup software opening multiple files simultaneously

2. File Descriptor Leaks

Some applications fail to close files or sockets properly. Over time, the number of open descriptors keeps increasing until the limit is reached.

Symptoms include:

  • Gradually increasing descriptor count
  • Application slowdown
  • Sudden crashes after long uptime

3. High Traffic or Concurrent Connections

Web servers and APIs may exceed their limits during periods of high traffic.

Each client connection can consume:

  • One socket descriptor
  • Additional descriptors for logs or cache files

4. Backup and Synchronization Operations

Backup tools often scan and open many files at once. Large hosting environments or servers with millions of files can quickly hit descriptor limits.

5. Misconfigured Services

Some services define their own limits independent of system-wide settings. If these values are too low, the application may fail even when the OS allows more descriptors.

How to Diagnose the Problem

Check System-Wide Limits

Use:

cat /proc/sys/fs/file-max

This shows the maximum number of file handles the kernel can allocate.

Check Current Open File Usage

Run:

cat /proc/sys/fs/file-nr

This displays:

  • Allocated file handles
  • Unused handles
  • Maximum handles

Check Open Files for a Process

Find the process ID:

ps aux | grep nginx

Then check descriptor count:

ls /proc/PID/fd | wc -l

Replace PID with the actual process ID.

Use lsof for Detailed Analysis

The lsof command lists open files:

lsof -p PID

To count total open files system-wide:

lsof | wc -l

Fixing the “Too Many Open Files” Error

1. Increase Temporary Limits

You can increase limits for the current shell session:

ulimit -n 65535

This change is temporary and resets after logout or reboot.

2. Configure Permanent User Limits

Edit:

/etc/security/limits.conf

Add:

* soft nofile 65535

* hard nofile 65535

You can also apply limits to specific users:

nginx soft nofile 65535

nginx hard nofile 65535

3. Update Systemd Service Limits

Modern Linux systems using systemd may override user limits.

Edit the service file:

systemctl edit nginx

Add:

[Service]

LimitNOFILE=65535

Reload systemd:

systemctl daemon-reexec
systemctl restart nginx

4. Increase Kernel File Limits

Edit:

/etc/sysctl.conf

Add:

fs.file-max = 2097152

Apply changes:

sysctl -p

5. Fix File Descriptor Leaks

If an application continuously consumes descriptors:

  • Update the software
  • Check logs for bugs
  • Review application code
  • Restart the affected service temporarily

Monitoring tools can help identify leaking processes early.

Best Practices to Prevent the Issue

Monitor Descriptor Usage

Use monitoring tools such as:

  • Prometheus
  • Grafana
  • Netdata
  • Zabbix

Track open file counts regularly.

Tune Services Properly

Applications like:

  • Nginx
  • Apache
  • MySQL
  • Redis

should be configured according to expected workloads.

Close Unused Connections

Developers should ensure applications properly close:

  • Files
  • Database connections
  • Sockets
  • Pipes

This reduces the risk of leaks.

Perform Load Testing

Simulate high traffic scenarios before deploying to production. This helps determine whether current limits are sufficient.

Conclusion

The “Too Many Open Files” error in Linux is typically caused by exhausted file descriptor limits, high concurrent workloads, or leaking applications. While the issue may appear intimidating at first, Linux provides multiple ways to diagnose and resolve it effectively.

By understanding how file descriptors work, monitoring resource usage, and properly tuning system limits, administrators can prevent outages and maintain stable server performance even under heavy workloads.

For production environments, proactive monitoring and correct limit configuration are essential to ensuring long-term reliability and scalability.

Need Expert Linux Server Support?

From resolving “Too Many Open Files” errors to optimizing system performance and preventing downtime, SupportPRO provides proactive Linux administration and infrastructure support. Get in touch today to keep your servers secure, scalable, and running at peak efficiency.

Facing issues?

Our technical support
engineers can solve it.

Contact Us today!
guy server checkup

You may also like

Leave a Comment