1) ulimit command

The ulimit command can be used to increase the number of files which can be opened in a shell. This command is a bash built-in command, so it affects only bash and programs launched from it. The ulimit syntax is as follows:

ulimit [options [limit]]

The options define what is being limited. You can see some options as below

  • -a (Current Settings Passing): causes ulimit to report its current settings.
  • -f (File Limits) limits the size of files that may be created by the shell
  • -n limits the number of open file descriptors.
  • -H and -S (Hard and Soft Limits) options modify other options, causing them to be set as hard or soft limits, respectively. Hard limits may not be subsequently increased, but soft limits may be. If neither option is provided, ulimit sets both the hard and soft limits for the feature specified.

So, to see the current limitation, you can do as below:

$ ulimit -a | grep open
open files (-n) 1024

You can check the hard limit as below:

$ ulimit -Hn

and the soft limit as below:

$ ulimit -Sn

You can edit the limit as below

$ ulimit -n 3000

and you can check below:

$ ulimit -n 

The problem now is, if you log out and log in or restart your computer, the value will be reset. Remember that making these limits more permanent requires editing one of the user’s configuration files (.bashrc or .profile) or the systemwide configuration files (/etc/bashrc or /etc/profile) by adding the ulimit command line to the end of the file as below

# vim .bash_profile
ulimit -n 3000

Now even if you restart, the limit set by the user will be permanent. You can choose to increase only the hard limit (-Hn) or the soft limit (-Sn) but you should notice that soft limits could be set by any user while hard limits are changeable only by root once it is set.

2) Pluggable Authentication Modules (PAM) module

Imposing such limits is best done through a Pluggable Authentication Modules (PAM) module called pam_limits. Most major Linux distributions use this module as part of their standard PAM configuration so it is already present on some Linux systems but you will need to configure it by editing the /etc/security/limits.conf file. This file contains consist of four essential fields:

  • domain: it describes the entity to which the limit applies. It can be a username, a group name (with the form @groupname) or an asterisk ( * ) wildcard which matches everybody. However, you should notice that wildcard does not apply to root user
  • type: thisfield specifies the limit as hard or soft. A hard limit is imposed by the system administrator and cannot be exceeded under any circumstances, whereas a user may temporarily exceed a soft limit. You can also use a dash ( – ) to signify that a limit is both hard and soft. Notice that a soft limit may be increased up to the value of the hard limit
  • item: it specifies what type of item is being limited. This could be nofile (the number of open data files), fsize (the size of files created by the user), core (the size of core files), etc
  • value: it specifies the value that’s to be applied to the limit

You can see for example the content of this file as below:

# tail -f -n 12 /etc/security/limits.conf  
#<domain> <type> <item> <value>
#* soft core 0
#* hard rss 10000
#@student hard nproc 20
#@faculty soft nproc 20
#@faculty hard nproc 50
#ftp hard nproc 0
#@student - maxlogins 4 # End of file

To edit the maximum number of opened files for all users, you can add, for example, add to the end of the file the lines below:

# vim /etc/security/limits.conf 
* hard nofile 20000
* soft nofile 15000

After this, you need to edit the file /etc/pam.d/login

# vim /etc/pam.d/login
session required pam_limits.so

Then save the file. You can check the result as below:

$ ulimit -Hn

3) System wide limit

On Linux system we have file-max which is the maximum File Descriptors (FD) and the default settings for ulimit and file-max assume that several users would share the system.  This is why these settings limit the number of resources used by each user. You can increase the limit of opened files in Linux by editing the  /etc/sysctl.conf or by editing the directive fs.file-max

You can see the current value for opened files with the command

# cat /proc/sys/fs/file-max

And you can edit the default value as below:

# sysctl -w fs.file-max=250000
fs.file-max = 250000

You can check the result as below

# cat /proc/sys/fs/file-max

By using the sysctl command, the modifications are applied until the next reboot. To make the configuration persistent, you can directly edit the /etc/sysctl.conf file as below:

# vim /etc/sysctl.conf

If you list the content of /proc/sys/fs/file-max , you will see that the change didn’t directly take effect. Now you need to directly apply the changes with the -p option of the sysctl command as below:

# sysctl -p
fs.file-max = 250000

Now the configuration is permanent

Setting up File Limits in Ubuntu / Linux

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.