You may receive a message on a Linux system like "Too many open files"; this means the server has reached the limit of max open files.

In Linux, there are two types of limits

(A) Hard limit: The maximum limit allowed to a user that the superuser/ root can set.

(B) Soft limit: The current limit allowed to a user that the user can increase up to the hard limit.

Check for Current Limits

You can check the currently allowed limit for a user with the Ulimit command. The following are the commands to check the Hard and Soft limits: 

# ulimit -Sn  (Check soft limit)
# ulimit -Hn  (Check hard limit)

Increase Limit for Current Session

If you want to increase the limit of any session, you can use the command given below – 

# ulimit -n 250000

Increase Per-User Limit 

Step 1: We can provide a per-user limit for an open file. Edit the limits.conf using the below command.

# sudo nano /etc/security/limits.conf

Step 2: Modify the value for your user as per your requirement and save the files.

Step 3: Once you complete the limit modification, enable the pam_limits using the command given below – 

# sudo nano /etc/pam.d/common-session

Step 4: Then add the following line:

session required pam_limits.so

Increase System-Wide Limit

Increasing the system-wide limit will allow you to open files system-wide. Please note that a specific user limit can’t be higher than the system-wide limit.

Step 1: We can also increase the system-wide limit by editing the sysctl configuration file

 nano /etc/sysctl.conf 

Step 2: Now, add this line:

fs.file-max = 3000000 

Step 3: Run the following command to save the changes:

# sysctl -p

Conclusion:

To resolve the "Too many open files" error:

               Check current limits using ulimit.

               Use ulimit -n to raise the limit temporarily.

               Modify limits.conf and enable PAM for persistent per-user limits.

               Update sysctl.conf to increase system-wide limits.

This ensures your Linux VPS or dedicated server can handle large-scale workloads

Was this answer helpful? 0 Users Found This Useful (4 Votes)