Linux maximum file handle (file descriptor) limit and modification

Reprinted from: http://jameswxx.iteye.com/blog/2096461

 

This article is written in order to face the sound, and the articles on the Internet are simply outrageous. What is the limit on the maximum number of files? What parameters can the too many open files error be controlled by? The general steps mentioned in many articles on the Internet are correct, which are roughly as follows: The

shell-level limit 
is modified by ulimit -n. For example, executing the command ulimit -n 1000 means that the maximum number of files that can be opened by all processes of the current user of the current shell is set. It is 1000. The

user-level limit  
ulimit -n is to set the maximum number of files that can be opened by all processes of the current user of the current shell, but a user may connect to the system through multiple shells at the same time, so there is also a user-specific limit, by modifying To implement /etc/security/limits.conf, for example, enter the following into limits.conf:
root soft nofile 1000 
root hard nofile 1200
soft nofile means soft limit, hard nofile means hard limit, soft limit must be less than or equal to hard limit. The above two lines of statements indicate that the soft limit of the root user is 1000 and the hard limit is 1200, which means that the maximum number of files that the root user can open is 1000, no matter how many shells it opens.

System level limit
modification cat /proc/sys/fs/file-max

 
 
However, there are many very important details, and there are many wrong descriptions, which are messed up, so I will make a special explanation here.

a ulimit -n

     网上很多人说,ulimit -n限制用户单个进程的问价打开最大数量。严格来说,这个说法其实是错误的。看看ulimit官方描述:
Provides control over the resources available to the shell and to processes started by  it,  on  systems that allow such control. The -H and -S options specify that the hard or soft limit is set for the given resource. A hard limit cannot be increased once it is set; a soft limit may  be  increased  up  to  the value of the hard limit. If neither -H nor -S is specified, both the soft and hard limits are set. The value of limit can be a number in the unit specified for the resource or one of the special values hard, soft,  or  unlimited,  which  stand  for  the  current hard limit, the current soft limit, and no limit,  respectively.
If limit is omitted, the current value of the soft limit of the resource is printed, unless the -H option is given. When more than one resource is specified, the limit name and unit are printed before the value.
 
People never say It is to limit the maximum number of files opened by a single process of the user. Look at the red part, which is to limit the number of files opened by the current shell and the process started by the shell. Why does it give the illusion of limiting the maximum number of files for a single thread, because in many cases, in a shell environment, although there may be multiple processes, there are not many processes that consume a lot of file handles, but one of them is very Consume file handles, such as a tomcat running on the server, then the java process will occupy most of the file handles. At this time, the maximum number of files set by ulimit basically corresponds to the maximum number of files consumed by the java process, so it will give people such an illusion. 
   
Also, many articles say that ulimit -n is only allowed to be set smaller and smaller. For example, if ulimit -n 1000 is executed first, and ulimit -n 1001 is executed, the error "cannot modify limit: Operation not permitted" will be reported. This is actually an inaccurate statement. The first thing to understand is that any user can execute ulimit, but root and non-root users are very different.
Non-root users can only set smaller and smaller, not larger
I run as non-root on the machine first:
[wxx@br162 etc]$ ulimit -n 900
[wxx@br162 etc]$
The execution is successful, and then increase:
[wxx@br162 etc]$ ulimit -n 901
-bash: ulimit: open files: cannot modify limit: Operation not permitted
[wxx@br162 etc]$
Increase fails, if decrease is OK:
[wxx@br162 etc]$ ulimit -n 899
[wxx@br162 etc]$
If it is increased to 900, it will not work:
[wxx@br162 etc]$ ulimit -n 900
-bash: ulimit: open files: cannot modify limit: Operation not permitted
[wxx@br162 etc]$ 
 
Unrestricted root user
First switch to root:
[wxx@br162 etc]$ sudo su -
[root@br162 ~]#
Check out the current restrictions below:
[root@br162 ~]# ulimit -n
1000000
[root@br162 ~]#
Increase:
 [root@br162 ~]# ulimit -n 1000001
[root@br162 ~]#
It can be successfully increased and then decreased:
[root@br162 ~]# ulimit -n 999999
[root@br162 ~]#
Decreasing is also successful, and then increasing:
 [root@br162 ~]# ulimit -n 1000002
[root@br162 ~]#
It is also ok, it can be seen that root is not restricted. 
 
The default value of the maximum number of open files in ulimit
If there is no setting in limits.conf, the default value is 1024. If there is a setting in limits.con, the default value is based on limits.conf. For example, I changed a machine, logged in, and ulimit -n showed the following:
[root@zk203 ~]# ulimit -n
2000
This is because the number of open files in my limits.conf is 2000, as follows:
[root@zk203 ~]# cat /etc/security/limits.conf
root soft nofile 2000
root hard nofile 2001
If there is no limit in limits.conf, after logging in again, ulimit -n is displayed as 1024.
 [root@zk203 ~]# ulimit -n
1024
 
Effective period after ulimit is modified
It will take effect immediately after the modification, and will be invalid after logging in again, because it is reset to the set value in limits.conf
 
 
 

Two /etc/security/limits.conf

There is also Miao Chuan on the Internet. The value set by ulimit -n cannot exceed the number of open files set in limits.conf (ie soft nofile)
Well, in fact, there are two cases, the root user can exceed, for example, the current limits.conf settings are as follows:
root soft nofile 2000
root hard nofile 2001
But I succeeded by setting the max number of files to 5000 with root:
[root@zk203 ~]# ulimit -n 5000
[root@zk203 ~]# ulimit -n 
5000
[root@zk203 ~]#
However, non-root users cannot exceed the settings of limits.conf. I switched to wxx and executed the command as follows:
[wxx@zk203 ~]# ulimit -n 5000
-bash: ulimit: open files: cannot modify limit: Operation not permitted
[wxx@zk203 etc]$ 
So the online statement is wrong. Even if the maximum number of files for non-root users cannot exceed the settings in limits.conf, this is only an appearance. In fact, it is because each user logs in, the default value of ulimit -n is limits It is specified by the soft nofile of .conf, but for non-root users, ulimit -n can only get smaller and smaller, not larger. In fact, this is the real reason, but the result is the same.
 
Modify the limits.conf and need to restart the system?
This statement is very funny. Modify limits.conf and log in again to take effect. Just try it on the machine, and many people are really lazy and would rather ask around than spend a minute to actually operate it.
 
 

三  /proc/sys/fs/file-max

It is said on the Internet that the maximum number of files in ulimit -n and limits.conf cannot exceed the value of /proc/sys/fs/file-max, which is also funny, /proc/sys/fs/file-max is given by the system The system will calculate the resource and give a reasonable value, which is generally related to the memory. The larger the memory, the larger the change value, but it is only a recommended value. The setting of limits.conf can completely exceed /proc/sys /fs/file-max.
[root@zk203 ~]# cat /proc/sys/fs/file-max
1610495
I set the maximum number of files in limits.conf to 1610496, and after saving, print out:
[root@zk203 ~]# cat /etc/security/limits.conf
root soft nofile1610496
root hard nofile1610496
 
 

Four to sum up

  • /proc/sys/fs/file-max cannot limit /etc/security/limits.conf
  • Only the root user has permission to modify /etc/security/limits.conf
  • For non-root users, /etc/security/limits.conf will limit ulimit -n, but not root users
  • For non-root users, ulimit -n can only be set smaller and smaller, and root users are unlimited
  • Any user's modification of ulimit -n is only valid in the current environment, and will be invalid after logging out. After logging in again, ulimit -n is determined by limits.conf
  • If limits.conf is not set, the default value is 1024
  • The maximum number of ask prices that can be opened by all processes of the user in the current environment is determined by ulimit -n

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324586376&siteId=291194637