What is ulimit?

A ulimit allows control over resources available to each user via a shell. You can type “ulimit -a” to get a list of all necessary settings. In parentheses you will see one or two items: the units in measurements (e.g. kbytes, blocks, seconds) as well as a letter option (e.g. -s, -t, -u) which will let you view or edit one setting at a time.

Soft Limit vs Hard Limit

A soft limit is the effective value currently for that user. The user can increase the soft limit on their own (on an as needed basis), especially when needing more resources. Please note that the user cannot set the soft limit higher than the hard limit.

A hard limit is the maximum amount allowed to a user and is set by the root or superuser. The hard limit value is set in the file /etc/security/limits.conf. As mentioned above, this is the set ceiling or limit that a value can be.

Error Message

If you keep seeing the error “Too many open files (24)” then your whatever you are running (whether it be an application or command or script) is reaching the max open file limit allowed by Linux. You need to increase open file limit as below:

  1. Increase limit by utilizing per-user limit by opening file: /etc/security/limits.conf and then paste following towards end:

*         hard    nofile      500000

*         soft    nofile      500000

root      hard    nofile      500000

root      soft    nofile      500000

500000 is fair number to use. Once you save the file, you may need to logout and login again.

  1. Pam-limits
    1. This is an extra step that may be needed for the limit to change for daemon processes. If the above methods are not working for you, then you may need to use this method to ensure success.
      1. Open /etc/pam.d/common-session
      2. Add following line: session required pam_limits.so
    2. System-Wide Limit
      1. Set this higher than user-limit set above.
        1. Open /etc/sysctl.conf
        2. Add following: fs.file-max = 2097152
  • Run: sysctl -p
  1. This limit method will increase “total” number of files that can stay open system-wide
  1. Verify New Limits
    1. Use following command to see max limit of file descriptors: cat /proc/sys/fs/file-max
    2. Hard Limit: ulimit -Hn
    3. Soft Limit: ulimit -Sn
  2. Check limit for other user
    1. Just replace the www-data with the Linux username you would like to check limits for: su – www-data -c ‘ulimit -aHS’ -s ‘/bin/bash’
  3. Check limits of a running process:
    1. Find process-id (PID): ps aux | grep process-name

Assume XXX is PID, then run the following command to check limits: cat /proc/XXX/limits