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
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.
[wxx@br162 etc]$
-bash: ulimit: open files: cannot modify limit: Operation not permitted
-bash: ulimit: open files: cannot modify limit: Operation not permitted
[root@br162 ~]#
1000000
2000
Two /etc/security/limits.conf
[root@zk203 ~]# ulimit -n
5000
[root@zk203 ~]#
三 /proc/sys/fs/file-max
1610495
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