Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify the limit on the number of open files in Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to modify the limit on the number of open files in Linux", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to modify the limit on the number of open files in Linux" this article.

Processes on Linux are subject to many restrictions, which also prevent them from executing correctly, and each process has several restrictions associated with it. Shell limits the number of file handles a 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 exhaustion by imposing restrictions.

You can see the maximum number of open file descriptors on the Linux system, as follows:

[root@localhost ~] # cat / proc/sys/fs/file-max 180965

This value shows the number of files the user can open per login session, and you will notice that the results may vary from system to system. For some reason, you may need to increase the value of the restriction set. This is why the Linux system offers the possibility to modify these limits (increase or decrease) by changing the maximum number of files opened per process and per system.

Method 1: use the ulimit command

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

Ulimit [option] [limit value]

The following options determine what is limited:

-a report showing all current restrictions

-f (file limit) limits the size of files that shell can create

-n limits the number of open file descriptors.

-H and-S are set to hard limit and soft limit, respectively. The hard limit may not increase, but the soft limit may increase. If no options are provided, ulimit sets both hard and soft limits.

To view the soft limits of the currently open file, you can use the following command:

[root@localhost ~] # ulimit-a | grep open open files (- n) 1024 or [root@localhost ~] # ulimit-n 1024

If you need to view the hard limits, adapt to the following command:

[root@localhost ~] # ulimit-Hn 4096

The following modifies the number of open files:

[root@localhost] # ulimit-n 2048

Then let's take a look:

[root@localhost ~] # ulimit-Hn 2048 [root@localhost ~] # ulimit-Sn 2048

It is found that when setting the ulimit value, no-H or-S is specified, and both limits are set to the same value.

The problem now is that if you log out or restart your computer, the value will be reset. Remember, to take effect, you need to edit the user's .bashrc or .bash _ profile configuration file, and you can make the parameters take effect by adding the ulimit command line to the end of the file.

[root@localhost] # echo "ulimit-n 2048" > > ~ / .bashrc

Method 2: use the PAM module

This limitation is best achieved through a PAM module called pam_limits. 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 (group name preceded by @ for group), or match all users, using (*) wildcards. Wildcards are not suitable for root users.

Type: does this mean a hard limit or a soft limit? 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 hard limit

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

Type: fill in the value of the application limit here.

The following examples can be compared.

[root@localhost ~] # 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

To edit the maximum number of files opened by all users, add the following line at the end of the file:

[root@localhost ~] # vim / etc/security/limits.conf * hard nofile 20000 * soft nofile 15000

After that, you need to edit the file / etc/pam.d/login configuration file, adding the following at the end:

[root@localhost ~] # vim / etc/pam.d/login session required pam_limits.so

Save the file. Then log out or restart the system. You can see that the values of both soft and hard limits have been modified successfully:

[root@localhost ~] # ulimit-Sn 15000 [root@localhost ~] # ulimit-Hn 20000

Method 3:

View the maximum number of open file descriptors on the system:

[root@localhost ~] # cat / proc/sys/fs/file-max 180965

Temporarily set this value:

[root@localhost ~] # echo "1000000" > / proc/sys/fs/file-max

Permanent setting, you need to set it in / etc/sysctl.conf and let it take effect:

[root@localhost ~] # echo "fs.file-max = 1000000" > > / etc/sysctl.conf [root@localhost ~] # sysctl-p fs.file-max = 1000000 is all the contents of this article entitled "how to modify the limit on the number of open files in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report