Cannot Increase open file limit past 4096 (Ubuntu)

OK, I finally figured this out. The limits I was setting in /etc/security/limits.conf were indeed being applied, but not to the graphical login. This can be verified 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 affecting the soft limit. (Leaving us capped with a hard limit of 4096 still.) In order to affect the hard limit, we must modify /etc/systemd/system.conf with the same changes.

The changes I made in /etc/pam.d were not necessarily; it's already working, at least on Ubuntu. Also, it was not necessary to add the lines root and * in limits.conf; adding the username alone (mkasberg in my case) was sufficient.


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.


No need to change anything in the /etc/security/limits.conf file, it is ignored if you are using systemd.

(reproducing a modified answer to another question on the network...)

An alternative for those who prefer not to edit the default /etc/systemd/system.conf and /etc/systemd/user/conf files:

  1. create a new file /etc/systemd/system.conf.d/limits.conf with these contents:

    [Manager]
    DefaultLimitNOFILE=65535
    
  2. run systemctl daemon-reexec as root

  3. logout and login again

  4. check your new limit with ulimit -n.

Refer to the systemd-system.conf manpage for details.