When discussing system performance and limitations in Linux or Unix-like operating systems, one often stumbles upon a term called NOFILE. Whether you’re a system administrator, developer, or server operator, understanding what NOFILE means and how it impacts applications and services is essential to maintaining healthy and scalable computing environments.
TL;DR (Too Long; Didn’t Read)
NOFILE defines the maximum number of file descriptors a process can open. This includes files, sockets, and other I/O resource references. Modifying the NOFILE value can improve performance and prevent errors like “too many open files.” Developers and sysadmins usually configure this setting based on application needs and hardware limitations.
What Is NOFILE?
In Unix-like systems, the operating system allocates a limited number of file descriptors to each process. These file descriptors act as references to open files, sockets, pipes, and other I/O resources. The term NOFILE represents the upper limit, or maximum number of file descriptors a process can simultaneously have open.
When a process hits this limit, it cannot open new files or sockets until it closes some of the existing ones. This can lead to severe issues such as:
- Application crashes
- Web servers failing to accept new connections
- Database performance degradation
Where NOFILE Settings Appear
The NOFILE limit is commonly found in three places:
- User Limits: Defined using the
ulimitshell command or the/etc/security/limits.conffile. - Systemd Service Files: Often specified for services via the
LimitNOFILEdirective in systemd unit configuration files. - Temporary Sessions: Use the
ulimit -ncommand to view or change it temporarily in a shell session.
The default NOFILE value is typically set low (e.g., 1024) to prevent misbehaving applications from consuming all system resources. However, many modern applications, such as web servers and databases, require a much higher value—often in the tens or hundreds of thousands.
Why Does NOFILE Matter?
Modern applications are high-throughput and connection-intensive. A low NOFILE limit can constrain such applications, leading to performance bottlenecks or outright failures. Here are some examples of how NOFILE comes into play:
- Web Servers: An Apache or Nginx server handling thousands of concurrent connections needs enough file descriptors for each socket.
- Database Systems: Systems like MySQL or PostgreSQL keep open many files, logs, and sockets simultaneously.
- Microservices: Distributed applications use REST or gRPC over HTTP, potentially opening numerous simultaneous network connections.
How to Check the Current NOFILE Value
To check the NOFILE value currently in effect for the active shell session, use:
ulimit -n
If you want more detailed information for a given process, such as a PID-specific check, you can use:
cat /proc/<pid>/limits
The output will show both soft and hard limits. The “soft” limit can be adjusted by the user, up to the “hard” limit, which is generally system-enforced and requires elevated permissions to modify.
How to Increase NOFILE Limits
Increasing the NOFILE limits is a common task in production environments. Here’s how it’s usually done:
1. Modify /etc/security/limits.conf
Edit or append the following lines (as root):
username soft nofile 65535
username hard nofile 65535
Replace username with the actual username of the service or user in question.
2. Modify PAM (Pluggable Authentication Module) Setting
Ensure that the file /etc/pam.d/common-session or /etc/pam.d/su contains:
session required pam_limits.so
3. Update Systemd Configurations
For services managed by systemd, update the unit file or create an override:
[Service]
LimitNOFILE=65535
Then reload and restart the service:
systemctl daemon-reexec
systemctl restart <service-name>
4. Temporary Change Using ulimit
For short-lived scripts or manual sessions:
ulimit -n 65535
This won’t persist after logout, so use it only for quick testing or ad-hoc changes.
Soft vs Hard NOFILE Limits
System limits are split into two categories:
- Soft Limit: The value enforced for a user or process until it’s explicitly increased.
- Hard Limit: The maximum ceiling that can be set for the soft limit. Only root users or specific policy-controlled environments can set a new hard limit.
This dual-tiered setup allows administrators to create a safe upper-bound for regular users while allowing exceptions in controlled environments.
Best Practices for Managing NOFILE
To keep your systems healthy and performant, consider the following best practices:
- Always test increased NOFILE values in staging before applying them in production.
- Monitor system behavior using tools like
lsofto see currently open files and file descriptors. - For Docker containers, use the
--ulimit nofile=...flag to set limits at runtime. - Use system-wide defaults only when necessary, and prefer setting per-service values.
Potential Errors and Troubleshooting
If the NOFILE value is too low, you might encounter runtime errors like:
- Too many open files
- Cannot bind socket
- Permission denied: resource limits exceeded
To troubleshoot such issues:
- Check the application’s file descriptors with
lsof -p <pid>. - Inspect the process’ limits via
/proc/<pid>/limits. - Cross-reference with your configuration settings.
Conclusion
Understanding and managing the NOFILE limit is crucial for operating robust, high-performance applications. From simple scripts to enterprise-grade web systems, knowing how to monitor and configure NOFILE can help avoid unexpected outages and ensure smooth operations. When in doubt, it’s always better to plan for higher limits, test, and monitor usage trends over time.
FAQs
-
What does NOFILE stand for?
NOFILE stands for “Number of Files” and refers to the number of file descriptors a process can open. -
What’s the best NOFILE value to use?
This depends on the application. Web servers usually need 10,000 to 100,000 or more. Always test and monitor usage. -
Does changing NOFILE require a reboot?
No, but you may need to log out and back in, or restart any affected services for changes to take effect. -
Are sockets counted in NOFILE?
Yes, sockets, pipes, and other I/O handles all count toward the NOFILE limit. -
Can I change NOFILE for a Docker container?
Yes, use the--ulimitflag when starting the container to set the NOFILE limit.