How to increase max open files limit on Ubuntu 18.04?

Solution 1:

From the manual:

 -n     The maximum number of open file descriptors (most
        systems do not allow this value to be set)

systemd has an option for this:

$ more /etc/systemd/system.conf | grep NOFILE
#DefaultLimitNOFILE=

But this is also only for the graphical user settings. Remove the # and you can set it to 65536.

Comment from Daniel Fernández:

DefaultLimitNOFILE=65536 

might also be needed in

/etc/systemd/user.conf 

Solution 2:

Ubuntu 18.04 Step by Step

Hope all this helps, quite long but it works (with proof)

I came across this thread whilst installing MongoDb on Ubuntu 18.04

https://posidev.com/blog/2009/06/04/set-ulimit-parameters-on-ubuntu/

As you can see it's quite old however it works for me.

Here is what I did.

  1. MongoDb Recommended Settings (https://docs.mongodb.com/manual/reference/ulimit/#review-and-set-resource-limits)

    • -f (file size): unlimited
    • -t (cpu time): unlimited
    • -v (virtual memory): unlimited [1]
    • -l (locked-in-memory size): unlimited
    • -n (open files): 64000
    • -m (memory size): unlimited [1] [2]
    • -u (processes/threads): 64000
  2. Check my current Limits

ubuntu@isdb-stage:~$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7873
max locked memory       (kbytes, -l) 16384
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7873
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
  1. Make a note of what requires changing

    • locked-in-memory size, needs to be set to unlimited.
    • open files, needs to be set to 64000
    • processes/threads*, needs to be set to 64000
  2. What does ubuntu say about how to change these limits?

$ man limits.conf

NAME
   limits.conf - configuration file for the pam_limits module

DESCRIPTION
   The pam_limits.so module applies ulimit limits, nice priority and number of simultaneous login sessions limit to user login sessions. This description of the configuration file syntax applies to the

       /etc/security/limits.conf file and *.conf files in the 
       /etc/security/limits.d directory.
  1. Seems pretty clear enough, edit the /etc/security/limits.conf file. Ok lets figure it out....

Here is the start of that file, and look it even has instructions within of course, remember this is open source written by the good guys! :)

    vi /etc/security/limits.conf

    # /etc/security/limits.conf 
    #Each line describes a limit for a user in the form:                                                                   
    #                                                                                
    #Where:                                                               
    # can be:
    #        - a user name

(Take a look at the file yourself if you want more details)
  1. Finally make the changes. As MongoDb runs as mongodb group and user, it would be a good idea to raise that users limits only. I have seen '*' used here and there, to me that's a security risk. It gives all and sundry on the server these limits, which could be used to max your server out. So lets make them for mongodb user only.

Here are my changes: -

mongodb          soft    memlock         unlimited
mongodb          hard    memlock         unlimited
mongodb          soft    nofile          64000
mongodb          hard    nofile          64000
mongodb          soft    nproc           64000
mongodb          hard    nproc           64000
  1. Finally, finally, remember this in the man limits.conf entry.

    limits.conf - configuration file for the pam_limits module

    Better make sure that the pam_limits module is loaded so all this limits stuff works. To do this you edit /etc/pam.d/common-session. Of course this is also in the man page.

sudo vi /etc/pam.d/common-session

# Add this to ensure pam_limits.so is loaded.
session required        pam_limits.so

A reboot will show the limits (for user mongodb) have been applied. If you applied them using '*' instead, you can check without having to switch to the mongodb user. Just execute this command.

$ ulimit -a

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7873
max locked memory       (kbytes, -l) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) 64000
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 64000
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

As you can see, lubbly jubbly all set to the limits we wanted.

Solution 3:

I had the same problem on Ubuntu 20.04.

The only file that needed to be changed was /etc/security/limits.conf. Appending the line * - nofile 100000 to that file and re-login in did the trick:

$ ulimit -Sn
100000