216

When running my application I sometimes get an error about too many files open.

Running ulimit -a reports that the limit is 1024. How do I increase the limit above 1024?

Edit ulimit -n 2048 results in a permission error.

Charles Menguy
  • 40,830
  • 17
  • 95
  • 117
John Meagher
  • 22,808
  • 14
  • 54
  • 57
  • see also:https://unix.stackexchange.com/a/8949/231660 – Guy Avraham Dec 11 '17 at 19:36
  • I just went through this on Centos 7 (same on RHEL) and made a blog post covering it because I had so much trouble even with all these posts: https://coding-stream-of-consciousness.com/2018/12/21/centos7-and-rhel7-increasing-open-file-descriptors-process-limits/. Often along with open files, you need to increase nproc which actually resides in multiple settings files... and if you use systemd/systemctl that has its own separate settings. It's kind of nuts. – John Humphreys Dec 21 '18 at 18:02
  • If you are using VSCode on linux this workaround might help: https://stackoverflow.com/a/55027686/1190948 – Juri Sinitson Mar 06 '19 at 16:37

4 Answers4

168

You could always try doing a ulimit -n 2048. This will only reset the limit for your current shell and the number you specify must not exceed the hard limit

Each operating system has a different hard limit setup in a configuration file. For instance, the hard open file limit on Solaris can be set on boot from /etc/system.

set rlim_fd_max = 166384
set rlim_fd_cur = 8192

On OS X, this same data must be set in /etc/sysctl.conf.

kern.maxfilesperproc=166384
kern.maxfiles=8192

Under Linux, these settings are often in /etc/security/limits.conf.

There are two kinds of limits:

  • soft limits are simply the currently enforced limits
  • hard limits mark the maximum value which cannot be exceeded by setting a soft limit

Soft limits could be set by any user while hard limits are changeable only by root. Limits are a property of a process. They are inherited when a child process is created so system-wide limits should be set during the system initialization in init scripts and user limits should be set during user login for example by using pam_limits.

There are often defaults set when the machine boots. So, even though you may reset your ulimit in an individual shell, you may find that it resets back to the previous value on reboot. You may want to grep your boot scripts for the existence ulimit commands if you want to change the default.

Ganesh Krishnan
  • 7,155
  • 2
  • 44
  • 52
hoyhoy
  • 6,281
  • 7
  • 38
  • 36
111

If you are using Linux and you got the permission error, you will need to raise the allowed limit in the /etc/limits.conf or /etc/security/limits.conf file (where the file is located depends on your specific Linux distribution).

For example to allow anyone on the machine to raise their number of open files up to 10000 add the line to the limits.conf file.

* hard nofile 10000

Then logout and relogin to your system and you should be able to do:

ulimit -n 10000

without a permission error.

Waldemar Wosiński
  • 1,490
  • 17
  • 28
Jonathan Stanton
  • 1,239
  • 1
  • 10
  • 4
  • 9
    NOTE: Wildcard **does not** apply to `root` user. You have to specify `root hard nofile 10000` if you want to adjust the `root` limit. – Joshua Pinter Aug 06 '15 at 17:04
  • 2
    @JoshPinter Haha, I just spent the past 3 hours or so going through every single tutorial on how to change the `ulimit` and finally found out I am logged in as root. That's why I kept seeing the 1024 in `ulimit -a`. I changed the wildcare, and added a `*` inside `limits.conf`. All is good now, (im using ssh keys don't worry :P) -- Thank you!!! – NiCk Newman Jun 13 '16 at 15:24
  • 1
    @NiCkNewman I'm glad you're laughing about it! That's a good trait in a programmer/sysops person. I did the same thing and wasn't so jovial. Glad it helped! :) – Joshua Pinter Jun 14 '16 at 17:46
  • SUPLEMENT: You may find also a config in this dir: `/etc/security/limits.d/`. – Waldemar Wosiński Dec 14 '16 at 14:51
  • 3
    I've opened /etc/security/limits.conf for the first time and noticed that everything is commented out there. What is the default value of "hard nofile" then? – ka3ak May 04 '17 at 16:42
  • I am using Ubuntu-18.04, I found that after making the changes in /etc/security/limits.conf , I had to RESTART the system then it worked. – MSharq Apr 15 '19 at 15:06
41

1) Add the following line to /etc/security/limits.conf

webuser hard nofile 64000

then login as webuser

su - webuser

2) Edit following two files for webuser

append .bashrc and .bash_profile file by running

echo "ulimit -n 64000" >> .bashrc ; echo "ulimit -n 64000" >> .bash_profile

3) Log out, then log back in and verify that the changes have been made correctly:

$ ulimit -a | grep open
open files                      (-n) 64000

Thats it and them boom, boom boom.

Momer
  • 3,158
  • 22
  • 23
Vikrant Telkar
  • 427
  • 4
  • 2
  • 10
    If you get change your line in limits.conf from 'hard' to 'soft' it should become the new default and not require changes to bash profiles. – RishiD Aug 12 '13 at 16:22
  • Seems you are repeating an extra steps. Setting limits alone will do the same as the profile, but across all sessions. – Eddie Nov 11 '13 at 14:47
8

If some of your services are balking into ulimits, it's sometimes easier to put appropriate commands into service's init-script. For example, when Apache is reporting

[alert] (11)Resource temporarily unavailable: apr_thread_create: unable to create worker thread

Try to put ulimit -s unlimited into /etc/init.d/httpd. This does not require a server reboot.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Sysadmin
  • 197
  • 1
  • 2