Three methods to modify the limit of open files in Linux

Time:2021-12-8

When a file is opened for access, the operating system temporarily assigns a number called file handle. A special area of main memory is reserved for file handles. The size of this area determines how many files can be opened at a time. Processes on Linux are subject to many restrictions, which also prevent them from executing correctly, and each process has several related restrictions. The shell limits the number of file handles that the program can open at the same time.
System environment
Centos7

Why limit the number of open files?
Because the operating system needs memory to manage each file, the number of files that can be opened may be limited. Because the program can also close the file handler, it can create files of any size until all available disk space is full. In this case, one aspect of security is to prevent resource depletion by imposing restrictions.

You can see the maximum number of open file descriptors in the Linux system, as shown below:

[[email protected] ~]# cat /proc/sys/fs/file-max
180965
This value shows the number of files that users can open each login session. You will notice that the results may vary from system to system. For some reason, you may need to increase the value of the limit set. This is why Linux systems provide the possibility to modify these limits (increase or decrease) by changing the maximum number of open files per process and per system.

Method 1: use ulimit command
The ulimit command can be used to increase the number of files open in the shell. This command is a system built-in command, so it only affects Bash and the programs started from it. Ulimit syntax is as follows:

Ulimit [options] [limit value]
The following options determine what is limited:

-A displays a report of all current restrictions
-F (file limit) limits the size of the file that the shell can create
-N limit the number of open file descriptors.
-H and – s, which are set to hard limit and soft limit respectively. Hard limits may not increase, but soft limits may increase. If no options are provided, ulimit will set both hard and soft limits.
To view the soft limit of the currently open file, you can use the following command:

[[email protected] ~]# ulimit -a |grep open
open files (-n) 1024
perhaps
[[email protected] ~]# ulimit -n
1024
If you need to view hard limits, adapt to the following command:

[[email protected] ~]# ulimit -Hn
4096
Modify the number of open files as follows:

[[email protected] ~]# ulimit -n 2048
Then take a look:

[[email protected] ~]# ulimit -Hn
2048
[[email protected] ~]# ulimit -Sn
2048
Three methods to modify the limit of open files in Linux three methods to modify the limit of open files in Linux
It is found that when setting ulimit value, if – H or – s is not specified, both limits will set the same value.

The problem now is that if you log out or restart the computer, the value will be reset. Remember that for to take effect, you need to edit the user’s. Bashrc or. Bash_ Profile configuration file, you can make the parameters take effect by adding the ulimit command line to the end of the file.

[[email protected] ~]# echo “ulimit -n 2048” >> ~/.bashrc
Three methods to modify the limit of open files in Linux three methods to modify the limit of open files in Linux

Method 2: use PAM module
It is best to pass the name PAM_ The PAM module of limits implements this limitation. You need to configure it by editing the / etc / security / limits.conf file. This file contains four basic fields:

Domain: describes the entities in which restrictions are applied. It can be user, group (the group name preceded by @ indicates the group), or match all users, using the (*) wildcard. Wildcards are not suitable for root users.

Type: This indicates whether the restriction is hard or soft? The hard limit is added by the system administrator and cannot be exceeded under any circumstances, while the user can temporarily exceed the soft limit. You can also use dashes – to indicate that a limit is hard and soft. Note that the soft limit can be increased to the value of the hard limit

Type: it specifies the type of restricted items. It can be core (limit core file size (KB)), data (maximum data size (KB)), fsize (maximum file size (KB)), nofile (maximum number of open file descriptors), nproc (maximum number of processes), etc. More types can be viewed using man limits.conf.

Type: fill in the value of application limit here.

The following examples can be compared.

[[email protected] ~]# tail -12 /etc/security/limits.conf

* 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

Three methods to modify the limit of open files in Linux three methods to modify the limit of open files in Linux
To edit the maximum number of files opened by all users, add the following line at the end of the file:

[[email protected] ~]# vim /etc/security/limits.conf

  • hard nofile 20000
  • soft nofile 15000
    Three methods to modify the limit of open files in Linux three methods to modify the limit of open files in Linux
    After that, you need to edit the file / etc / pam.d/login configuration file and add the following item at the end:

[[email protected] ~]# vim /etc/pam.d/login
session required pam_limits.so
Three methods to modify the limit of open files in Linux three methods to modify the limit of open files in Linux
Save the file. Then log out, or restart the system. You can see that the values of soft limit and hard limit have been modified successfully:

[[email protected] ~]# ulimit -Sn
15000
[[email protected] ~]# ulimit -Hn
20000
Method 3
View the maximum number of open file descriptors in the system:

[[email protected] ~]# cat /proc/sys/fs/file-max
180965
Set this value temporarily:

[[email protected] ~]# echo “1000000” > /proc/sys/fs/file-max
Three methods to modify the limit of open files in Linux three methods to modify the limit of open files in Linux
Permanent settings need to be set in / etc / sysctl.conf and make it effective:

[[email protected] ~]# echo “fs.file-max = 1000000” >> /etc/sysctl.conf
[[email protected] ~]# sysctl -p
fs.file-max = 1000000
summary
The number of file descriptors opened by all processes cannot exceed / proc / sys / FS / file max. The number of file descriptors opened by a single process cannot exceed the soft limit of nofile in user limit. The soft limit of nofile cannot exceed its hard limit.

Recommended Today

Heavyweight Tencent cloud open source industry’s first etcd one-stop governance platform kstone

​ Kstone open source At the kubecon China Conference held by CNCF cloud native foundation on December 9, 2021,Tencent cloud container tke team released the open source project of kstone etcd governance platform. KstoneIt was initiated by the TKE team of Tencent cloud containerCloud native one-stop etcd governance project based on kubernetes。 The project originates […]