CentOS7修改单个进程可打开的最大文件句柄数

来源:互联网 发布:视频编辑软件 知乎 编辑:程序博客网 时间:2024/04/28 12:59

对于“linux修改单个进程可打开的最大文件句柄数”,在网上搜索了很多教程,基本都说得不全或者已经过时了,经过亲自试验,修改成功,现总结如下

1./etc/security/limits.conf中指定的nofile的值,nofile有上限,不是无穷大。nofile由内核参数nr_open定义的.
“在2.6.25内核之前有个宏定义,定义了这个值的最大值,为1024*1024,正好是100万,而在2.6.25内核及其之后,这个值是可以通过/proc/sys/fs/nr_open来设置。”


2.使用cat /proc/sys/fs/nr_open 查看nr_open值,可通过修改/ect/sysct.conf 修改fs.nr_open值,sysctl -p生效


3.fix_max linux内核定义的最大file handles(文件句柄数).nr_open定义是单进程最大file-handles,file-handles(即文件句柄)
file-max:
The value in file-max denotes the maximum number of file-
handles that the Linux kernel will allocate. When you get lots
of error messages about running out of file handles, you might
want to increase this limit

nr_open:
This denotes the maximum number of file-handles a process can
allocate. Default value is 1024*1024 (1048576) which should be
enough for most machines. Actual limit depends on RLIMIT_NOFILE
resource limit.

注:
在centos 5/6 等版本中,资源限制的配置可以在 /etc/security/limits.conf 设置,针对root/user等各个用户或者*代表所有用户来设置。 当然,/etc/security/limits.d/ 中可以配置,系统是先加载limits.conf然后按照英文字母顺序加载limits.d目录下的配置文件,后加载配置覆盖之前的配置。

不过,在CentOS 7 / RHEL 7的系统中,使用Systemd替代了之前的SysV,因此 /etc/security/limits.conf 文件的配置作用域缩小了一些。limits.conf这里的配置,只适用于通过PAM认证登录用户的资源限制,它对systemd的service的资源限制不生效。登录用户的限制,与上面讲的一样,通过 /etc/security/limits.conf 和 limits.d 来配置即可。
对于systemd service的资源限制,如何配置呢?

全局的配置,放在文件 /etc/systemd/system.conf 和 /etc/systemd/user.conf。 同时,也会加载两个对应的目录中的所有.conf文件 /etc/systemd/system.conf.d/*.conf 和 /etc/systemd/user.conf.d/*.conf
其中,system.conf 是系统实例使用的,user.conf用户实例使用的。一般的sevice,使用system.conf中的配置即可。systemd.conf.d/*.conf中配置会覆盖system.conf。

注意:修改了system.conf后,需要重启系统才会生效。

一、如果需要修改 单个进程打开的文件句柄数 即nofile大于1024*1024,需要修改nr_open,方法如下:
1、使用cat /proc/sys/fs/nr_open查看nr_open值;
[root@localhost ~]# cat /proc/sys/fs/nr_open 
1048576
2、修改nr_open的值为10240000
打开/etc/sysctl.conf,在文件末尾添加fs.nr_open=10240000

# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).

fs.nr_open=10240000

保存
(注:nr_open的值要小于file—max)

3、执行如下命令,使修改生效
sysctl -p

4、验证修改是否生效
cat /proc/sys/fs/nr_open
如果结果为10240000,说明修改生效

二、修改/etc/systemd/system.conf,主要用于systemd service的资源限制
[root@localhost ~]# vi /etc/systemd/system.conf
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See systemd-system.conf(5) for details.



[Manager]
#LogLevel=info
#LogTarget=journal-or-kmsg
#LogColor=yes
#LogLocation=no
#DumpCore=yes
#CrashShell=no
#ShowStatus=yes
#CrashChVT=1
#CtrlAltDelBurstAction=reboot-force
#CPUAffinity=1 2
#JoinControllers=cpu,cpuacct net_cls,net_prio
#RuntimeWatchdogSec=0
#ShutdownWatchdogSec=10min
#CapabilityBoundingSet=
#SystemCallArchitectures=
#TimerSlackNSec=
#DefaultTimerAccuracySec=1min
#DefaultStandardOutput=journal
#DefaultStandardError=inherit
#DefaultTimeoutStartSec=90s
#DefaultTimeoutStopSec=90s
#DefaultRestartSec=100ms
#DefaultStartLimitInterval=10s
#DefaultStartLimitBurst=5
#DefaultEnvironment=
#DefaultCPUAccounting=no
#DefaultBlockIOAccounting=no
#DefaultMemoryAccounting=no
#DefaultTasksAccounting=no
#DefaultTasksMax=
#DefaultLimitCPU=
#DefaultLimitFSIZE=
#DefaultLimitDATA=
#DefaultLimitSTACK=
#DefaultLimitCORE=
DefaultLimitCORE=infinity
#DefaultLimitRSS=
#DefaultLimitNOFILE=
DefaultLimitNOFILE=10240000
#DefaultLimitAS=
#DefaultLimitNPROC=
DefaultLimitNPROC=10240000
#DefaultLimitMEMLOCK=
#DefaultLimitLOCKS=
#DefaultLimitSIGPENDING=
#DefaultLimitMSGQUEUE=
#DefaultLimitNICE=
#DefaultLimitRTPRIO=
#DefaultLimitRTTIME=

保存

三、修改/etc/security/limits.conf文件
[root@localhost ~]# vi /etc/security/limits.conf
在文件末尾添加
root            soft    fsize           unlimited
root            hard    fsize           unlimited
root            soft    cpu             unlimited
root            hard    cpu             unlimited
root            soft    as              unlimited
root            hard    as              unlimited
root            soft    nofile          10240000
root            hard    nofile          10240000
root            soft    nproc           10240000
root            hard    nproc           10240000

保存退出

四、修改/etc/security/limits.d/20-nproc.conf文件
[root@localhost ~]# vi /etc/security/limits.d/20-nproc.conf 
修改
*          soft    nproc     4096

*          soft    nproc     10240000

保存。
CentOS7修改单个进程可打开的最大文件句柄数为10240000完成。