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

What are the three ways to modify the limit on the number of open files in Linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What are the three ways to modify the limit of the number of open files in Linux? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

When a file is opened for access, the operating system temporarily assigns a number called the file handle. 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 a time. 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 openopen files (- n) 1024 or [root@localhost ~] # ulimit-n1024

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

[root@localhost ~] # ulimit-Hn4096

The following modifies the number of open files:

[root@localhost] # ulimit-n 2048

Then let's take a look:

[root@localhost ~] # ulimit-Hn2048 [root@localhost ~] # ulimit-Sn2048

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 @ student-maxlogins the 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-Sn15000 [root@localhost ~] # ulimit-Hn20000

Method three

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-pfs.file-max = 1000000 this is the answer to the question about what are the three ways to modify the limit on the number of files to be opened in Linux. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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

Development

Wechat

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

12
Report