Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save ntamvl/78aadc9a8c0b47a974a483cd863c7493 to your computer and use it in GitHub Desktop.
Save ntamvl/78aadc9a8c0b47a974a483cd863c7493 to your computer and use it in GitHub Desktop.
Ubuntu 16 – how to increase maximum file open limit ( ulimit -n )

Ubuntu 16 – how to increase maximum file open limit ( ulimit -n )

If you are setting up nginx,chances are you will discover your worker_connections is at some low number, such as 1024. You can’t increase this number unless you increase kernel limit as well. First of all run cat /proc/sys/fs/file-max to discover your maximum limit.

abc@ubuntu:~$ cat /proc/sys/fs/file-max
1048576
abc@ubuntu:~$ ulimit -n
1024

As you can see there’s plenty of space for improvement. Lets say I want my new ulimit -n to read 131072.

abc@ubuntu:~$ sudo nano /etc/sysctl.conf

add

fs.file-max = 131072

run

sudo sysctl -p

edit

sudo nano /etc/security/limits.conf

add

* soft     nproc          131072    
* hard     nproc          131072   
* soft     nofile         131072   
* hard     nofile         131072
root soft     nproc          131072    
root hard     nproc          131072   
root soft     nofile         131072   
root hard     nofile         131072
sudo nano /etc/pam.d/common-session

add

session required pam_limits.so

And that’s it. Log out and in and try ulimit -n

abc@ubuntu:~$ ulimit -n 131072

Now you can edit nginx as well

events {
    worker_connections 131072;
    use epoll;
    multi_accept on;
}

source: https://codingweb.io/ubuntu-16-increase-maximum-file-open-limit-ulimit-n/

@djramones
Copy link

Note that this probably doesn't work for nginx, which by default in Ubuntu is run by systemd under a different user, i.e. not through the user shell. You'll have to use systemd's LimitNOFILE (or DefaultLimitNOFILE) option instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment