How can I increase my open file limit in Ubuntu?

Linode Staff

I'm trying to run a script but it keeps hitting the open file limit. When I run ulimit -n it says 1024. How can I change this, and make it so the change lasts through my next reboot?

3 Replies

This is is a great question.
In order to change these settings permanently you have two options. The first is to make the change in your sysctl.conf file. You can do this by running:
sudo nano /etc/sysctl.conf
from the command line. This will open up the sysctl.conf file in the nano text editor. While there are some really useful networking settings in there we only need to add one line.
fs.file-max = $VARIABLE
where $VARIABLE is the limit of max files that you need to have open. I would recommend starting with a lowest possible value and working your way up to prevent any unexpected behavior.

You might receive an error to the effect of "sysctl: permission denied on key ‘fs.file-max.'" This isn't the end of the world, it just means we are going to have to move on to the second option.

In this case we are going to run:
sudo nano /etc/security/limits.conf
and add the following lines to the file:

* soft     nproc          $VARIABLE    
* hard     nproc          $VARIABLE   
* soft     nofile         $VARIABLE   
* hard     nofile         $VARIABLE
$USER soft     nproc          $VARIABLE    
$USER hard     nproc          $VARIABLE   
$USER soft     nofile         $VARIABLE   
$USER hard     nofile         $VARIABLE 

You can add this to the bottom of the file, where $VARIABLE is going to be your minimum limit and $USER is the user that is running the script. Next we are going to run:
sudo nano /etc/pam.d/common-session
We are looking for the line
session required
Which we are going to edit to:
session required

Then you can go ahead and run to try ulimit -n which should return $VARIABLE.

I hope this helps you get set up. If you have another way of accomplishing this don't hesitate to let us know!

Thanks. How would I make this apply to the www-data user running nginx?

As per the /etc/security/limits.conf example that @tbaka provided, you would replace $USER with www-data and $VARIABLE with your open files limit.

*           soft     nproc          1024
*           hard     nproc          2048
*           soft     nofile         1024
*           hard     nofile         2048
www-data    soft     nproc          1024
www-data    hard     nproc          2048
www-data    soft     nofile         1024
www-data    hard     nofile         2048

Then you can give the www-data user a quick shell to verify with the ulimit command.

su - www-data -c 'ulimit -Sn' -s '/bin/bash'  # soft limit

su - www-data -c 'ulimit -Hn' -s '/bin/bash'  # hard limit


Please enter an answer

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct