How to Increase Number of Open Files Limit (ulimit) in Linux

Photo of author
By Jay
— 3 min read
Photo of author
Written by
Photo of author
Verified by
Published On
— 3 min read

Number of Open Files Limit ulimit

Managing the number of open files limit in a Linux system is essential, especially for servers and applications that require handling many simultaneous files. This guide will explain how to find and increase the open files limit in Linux using the ulimit command, including checking and setting soft and hard limits, and configuring system-wide and user-level limits.

How to Find Linux Open File Limit

To find the current open file limits for your session, you can use the ulimit command:

ulimit -n

This command will display the soft limit for the number of open files.


Check Hard Limit in Linux

The hard limit is the maximum value that a soft limit can be set to. It can only be increased by the root user. To check the hard limit for open files, use:

ulimit -Hn

Check Soft Limits in Linux

The soft limit is the current value that can be modified by the user up to the hard limit. To check the soft limit for open files, use:

ulimit -Sn

Checking User-Specific Limits

To view the hard and soft limits for a specific user, simply use the “su” command to switch to that user account. This will allow you to check the limits applicable to that particular user.

For example:

# su sarah
$ ulimit -Sn

1024
$ ulimit -Hn

4096

How to Check System-wide File Descriptors Limits in Linux

System-wide limits on file descriptors can be found in the /proc/sys/fs/file-max file. This value defines the maximum number of file descriptors that can be allocated by the kernel:

cat /proc/sys/fs/file-max

To display system-wide limits using sysctl:

sysctl fs.file-max

How to Increase Number of Open Files Limit

Temporarily Increase Open Files Limit

To temporarily increase the open files limit for your current session, use the ulimit command:

ulimit -n <new-limit>

For example, to set the limit to 100000:

ulimit -n 100000

This change will only apply to the current session and will be reset when you log out.

Read Also | Using systemctl list services to View Running Services in Linux

Permanently Increase Open Files Limit

To permanently increase the open files limit, you need to modify system configuration files.

Modify /etc/security/limits.conf

Edit the /etc/security/limits.conf file to set user-level limits. Add the following lines:

* soft nofile 100000
* hard nofile 100000

This sets both soft and hard limits to 100000 for all users. You can also specify a particular user by replacing * with the username.

Modify /etc/pam.d/common-session

If you’re using PAM (Pluggable Authentication Modules), you also need to ensure that the limits are applied. Add the following line to /etc/pam.d/common-session and /etc/pam.d/common-session-noninteractive:

session required pam_limits.so
Modify /etc/sysctl.conf

To change the system-wide limit, add the following line to /etc/sysctl.conf:

fs.file-max = 200000

Apply the changes with:

sysctl -p
Modify Systemd Configuration (For Systemd Systems)

For services managed by systemd, create or edit a drop-in configuration file for the service. For example, to change the limit for a service called example.service, create or edit the file /etc/systemd/system/example.service.d/limits.conf:

[Service]
LimitNOFILE=100000

Reload systemd and restart the service:

systemctl daemon-reload
systemctl restart example.service

Conclusion

In conclusion, managing the number of open files in a Linux system is crucial for maintaining optimal performance, especially for applications that handle numerous simultaneous connections. By understanding and utilizing the ulimit command, you can effectively check and modify both soft and hard limits to meet your system’s needs. Whether you are adjusting user-level settings or configuring system-wide limits, careful management of these parameters ensures that your applications run smoothly without hitting file descriptor limits. Regular monitoring and adjustments as your workload changes will help maintain system stability and performance.

Read Also | How to Manage Partitions and File Systems in Linux: A Comprehensive Guide

Check Man Pages of ulimit command

Related Posts


About Author

Photo of author

Jay

I specialize in web development, hosting solutions, and technical support, offering a unique blend of expertise in crafting websites, troubleshooting complex server issues, and optimizing web performance. With a passion for empowering businesses and individuals online, I provide in-depth reviews, tech tutorials, and practical guides to simplify the digital landscape. My goal is to deliver clear, reliable, and insightful content that helps readers make informed decisions and enhance their online presence.