Cannot Increase open file limit past 4096 (Ubuntu)

37

24

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

mkasberg

Posted 2017-04-17T17:54:10.100

Reputation: 1 413

Answers

59

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.

mkasberg

Posted 2017-04-17T17:54:10.100

Reputation: 1 413

2DefaultLimitNOFILE=65535 did the trick. But why /etc/security/limits.conf does'nt work? – Suvitruf says Reinstate Monica – 2018-01-04T07:44:05.160

6The GUI login uses systemd, which apparently has it's own configuration (/etc/systemd/system.conf) that is independent of the normal configuration for terminal sessions (/etc/security/limits.conf). I don't know enough about systemd to know why it was implemented this way. – mkasberg – 2018-01-04T17:22:44.293

Thank you! Hours of searching and trying everything, but this fixed the issue. – Roger Collins – 2018-03-19T23:15:18.790

1@Suvitruf because it's ignored in a systemd system. I'm posting an answer. – Marc.2377 – 2018-05-12T21:54:22.427

Doesn't work for me. Still stuck to 1024. – Mooncrater – 2018-12-29T17:33:52.380

I could change the value by first su <username> and then using ulimit -n 5000. I guess it only changed for the current session. – Mooncrater – 2018-12-29T18:56:42.990

1Just want to point out that limits for root user can't be specified by * or group specifiers. root literal must be specified explicitly. – Petr Javorik – 2018-12-30T16:43:06.533

2This works for me, after a reboot. – Shihe Zhang – 2019-05-18T07:46:33.400

Soon, we'll have a monolithic system, all of which will be systemd. /s – Andrew Beals – 2019-10-04T18:36:27.647

14

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.

Marc.2377

Posted 2017-04-17T17:54:10.100

Reputation: 1 072

On my Ubuntu 18.10 system the file in question is at /etc/systemd/system.conf. Making the change there appears to have done the trick, thank you. – Stephen Kennedy – 2019-03-30T22:44:43.727

2Just logging out for me did not work (Ubuntu 18.04) but restarting did the job. Very elegant solution, thanks. – stann1 – 2019-04-11T09:39:49.277

0

Using Ubuntu 17.04 I got the described hard limit:

user@paresh.com:~$ ulimit -Hn
4096

I could lower it using ulimit, but not increase it, just as the question describes it. ulimit manual describes:

only root can increase the hard limit.

So I tried to set a higher limit in /etc/security/limits.conf like this:

user hard nofile 9999

and a fresh login like ssh localhost -l user gave me the new limit:

user@paresh.com:~$ ulimit -Hn
9999

I hope this works for you too.

Paresh Chauhan

Posted 2017-04-17T17:54:10.100

Reputation: 144

0

TL;DR I felt the need to concentrate the answers, so they're easier to find. Took me ages to get all the pieces together to make it work correctly ...

There are 2 locations to be considered.

  1. GUI session

    $ grep DefaultLimitNOFILE /etc/systemd/system.conf
    DefaultLimitNOFILE=65535
    

    or better here:

    $ grep NOFILE /etc/systemd/system.conf.d/limits.conf
    DefaultLimitNOFILE=65535
    
  2. shell environment

    $ grep nofile /etc/security/limits.conf
    user soft nofile 65535
    user hard nofile 65535`
    

    or better here:

    $ grep nofile /etc/security/limits.d/user.conf
    user soft nofile 65535
    user hard nofile 65535
    
  3. After altering the settings in the above files, reboot and then check the limits with: ulimit -n -Hn -Sn

U.V.

Posted 2017-04-17T17:54:10.100

Reputation: 71

0

  1. edit /etc/systemd/system/sonar.service

  2. add these two line under Service

[Service]

LimitMEMLOCK=infinity

LimitNOFILE=65535

  1. systemctl daemon-reload
  2. systemctl restart sonar

this works for me.

Matthew Ma

Posted 2017-04-17T17:54:10.100

Reputation: 1