3 Methods to Change the Number of Open File Limit in Linux

open file limit linuxThe operating system assigns temporarily a number called file handle to a file when it is opened for access. A special area of main memory is reserved for file handles, and the size of this area determines how many files can be opened at once. The processes on Linux are restricted by a number of constraints that also prevent them from executing properly and each process has several limits associated with it. The shell restricts the number of files handles that programs can open simultaneously. In this article, I will show you different methods that you can use to change the number of Open File Limit in Linux.

Why limiting the number of opened files

Because the operating system needs memory to manage each file, you can face a limitation of the number of files which can be opened. As a program can also close file handlers, it could create many files as big as it wants, until all the available disk space is full. In this case, one aspect of security is preventing the resource exhaustion by imposing limitations.

Under Linux, there are two kinds of limits:

  • soft limit is the value which can be changed by the process at any time.
  • hard limit marks the maximum value which cannot be exceeded by setting a soft limit

You can see the maximum number of opened file descriptors on your Linux system as below:

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

The value shows the number of files that a user can have opened per login session but you should notice that the result might be different depending on your system. For some reasons, you can need to increase the value of the limitation set. This is why your Linux system offers the possibility (increasing or decreasing) to modify these limitations by changing the maximum of the open files count per process and per system.

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
4096

and the soft limit as below:

$ ulimit -Sn
1024

You can edit the limit as below

$ ulimit -n 3000

and you can check below:

$ ulimit -n 
3000

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: this field 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
20000

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
100576

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
250000

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
fs.file-max=250000

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

About Alain Francois

IT Linux administrator passionate of free and open source software, I work on Linux Systems since some years on installations and deployments of certain solutions. I like to share my experiences with a wider audience by training and conferences.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.