Linux – Cannot Increase open file limit past 4096 (Ubuntu)

linuxUbuntuubuntu 17.04ulimit

I'm on Ubuntu 17.04. Trying to increase the open file limit, and none of the instructions I've found online are working. I can go up to 4096, but can't go past that.

$ ulimit -n
1024
$ ulimit -n 4096
$ ulimit -n
4096

That works. This doesn't:

$ ulimit -n 4097
bash: ulimit: open files: cannot modify limit: Operation not permitted

It appears to be because of the hard limit:

$ ulimit -Hn
4096

I've tried adding these lines to /etc/security/limits.conf:

*                hard    nofile          65535
*                soft    nofile          65535
root             soft    nofile          65535
root             hard    nofile          65535

Also added this line to /etc/pam.d/common-session and /etc/pam.d/common-session-noninteractive:

session required pam_limits.so

Since doing that, I've rebooted my computer. Changes to limits.conf don't seem to affect anything. The hard limit is still stuck at 4096, preventing me from going any higher. How do I increase my open files limit?


Here's some additional config info:

$ cat /proc/sys/fs/file-max 
1624668

Best Answer

OK, I finally figured this out. The limits I was setting in /etc/security/limits.conf were being applied, but they were not being applied to the graphical login. This can be verified like this from a terminal window:

$ ulimit -n
4096
$ su mkasberg
Password:
$ ulimit -n
65535

More research led me to this bug report, which got me pointed in the right direction. In order to modify the limit that is used by the login shell, we need to add the following line to /etc/systemd/user.conf:

DefaultLimitNOFILE=65535

That change works, but only affects the soft limit. (Leaving us capped with a hard limit of 4096 still.) In order to affect the hard limit also, we must modify /etc/systemd/system.conf with the same change.

The changes I made in /etc/pam.d were not necessary. At least on Ubuntu, this is already working. Also, it was not necessary to change settings for root and * in limits.conf. Changing limits for mkasberg was sufficient, at least for my use case.


In Summary

If you want to increase the limit shown by ulimit -n, you should:

  • Modify /etc/systemd/user.conf and /etc/systemd/system.conf with the following line (this takes care of graphical login):

    DefaultLimitNOFILE=65535
    
  • Modify /etc/security/limits.conf with the following lines (this takes care of non-GUI login):

    mkasberg hard nofile 65535
    mkasberg soft nofile 65535
    
  • Reboot your computer for changes to take effect.