36

On Ubuntu 12.04, I ran ulimit -n, it is showing 1024. I want to increase my open file limit from 1024 to 65535, so I tried the following command:

sudo ulimit -n 65535

but I get the following error:

sudo: ulimit: command not found

How do I increase the file limit from 1024 to 65535?

Nathan
  • 143
sunpy
  • 461
  • 2
    Hai thare! Am seeing in my ulimit manpage adviced to use the bash builtin ulimit instead. Am not sure sudo can be used with builtins, can be it only available for external commands? – 41754 Jul 05 '13 at 07:59

1 Answers1

43

Limits are process-specific. ulimit is a shell bult-in and can only change limits for this shell and the processes started from this shell. sudo ulimit doesn't make any sense. Even if it won't fail it would only influence the processes started by sudo at the same time, and there aren't any. It can't change the limit for the current session since it creates a new session (for user root) and terminates it immediately afterwards.

In order to raise your limit above the hard limit you have to either raise the global hard limit for all users or set a different limit for your specific user. Those limits are defined in /etc/security/limits.conf (or /etc/security/limits.d/). Edit this file and create a new entry for your user:

sunpy             hard    nofile           65535

Save the file and open a new session. Afterwards calling ulimit -Hn inside the new session should show you the new hard limit for the number of open files. In order to use the hard limit, you have to raise your current soft limit to the hard limit by calling ulimit -n 65535.

Of course you can also change your soft limit permanently by specifying soft instead of hard in your /etc/security/limits.conf. Then you don't have to raise your soft limit manually, but the new limit will apply for each of your sessions.

Oh, and according to this answer it seems like you have to edit /etc/pam.d/common-session* and add:

session required pam_limits.so

if not already done. Otherwise those limits won't apply for your sessions.

scai
  • 10,793
  • 5
    This answer is not helpful. – schily May 29 '18 at 15:44
  • 7
    Please explain why so that it can be improved. – scai May 20 '19 at 09:28
  • @scai I know this is really old but I just hit this problem today so this is still relevant. ulimit -Hn aka Hard limits cannot be changed by non-root users once set. (According to RHEL 7.8 man page). So that implies I need to sudo ulimit -Hn value but surprise I get the OP's error message. And here I am trying to figure out how to fix it . – Dan Aug 21 '20 at 02:26
  • sudo ulimit ... will only change the limit for this particular sudo process, so this call is worthless even if it would succeed. You need to either raise the global limit for all users or the global limit for the current user. Both can be done by editing /etc/security/limits.conf or creating a new file inside /etc/security/limits.d/. – scai Aug 24 '20 at 07:39