34

I've run ulimit -n 65536, added the following lines to /etc/security/limits.conf:

*    soft nofile 65536
*    hard nofile 65536
alix soft nofile 65536
alix hard nofile 65536
root soft nofile 65536
root hard nofile 65536

And added session required pam_limits.so to both:

  • /etc/pam.d/common-session
  • /etc/pam.d/common-session-noninteractive

And fs.file-max = 65536 to /etc/sysctl.d/60-file-max.conf.

Still, after restarting and running ulimit -n on my session, I only get 4096 (was 1024 before).

What am I missing?

3 Answers 3

28

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 
8
  • 1
    Forgot to mention, I also did that.
    – Alix Axel
    Jun 23, 2018 at 12:03
  • 1
    @AlixAxel : This answer worked for me, in addition to * - nofile 16384 ( I was only attempting 16384) in /etc/security/limits.conf. Jun 23, 2018 at 14:45
  • The 65k is an assumption and it would be expected that there are hard coded limits to the upper side of what is allowed. You might want to start lower as Doug indicated ;)
    – Rinzwind
    Jun 23, 2018 at 14:58
  • 2
    What do you mean by "But this is also only for the graphical user settings"?
    – sinan
    Aug 16, 2018 at 7:12
  • 1
    Note setting DefaultLimitNOFILE=65536 might also be needed in /etc/systemd/user.conf Nov 20, 2019 at 9:17
15

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.

2
  • 3
    Thks, worked for me on 20.04. If someone wants to check the ulimit for the mongodb user: 'sudo su -' (become root first) and then 'su mongodb --shell /bin/bash --command "ulimit -aH".'
    – emvidi
    Jul 3, 2020 at 18:51
  • 1
    sudo -H -u mongodb bash -c "ulimit -aH"
    – Paschalis
    Aug 4, 2021 at 16:54
12

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .