CentOS7-文件句柄

CentOS7-文件句柄

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)

/proc/sys/fs/file-max

3、執行如下命令,使修改生效

sysctl -p

4、驗證修改是否生效

cat /proc/sys/fs/nr_open

如果結果爲10240000,說明修改生效

二、修改/etc/systemd/system.conf,主要用於systemd service的資源限制

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文件

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

* - nofile 100000

四、修改/etc/security/limits.d/20-nproc.conf文件

vi /etc/security/limits.d/20-nproc.conf

修改

*          soft    nproc     4096

*          soft    nproc     10240000

CentOS7修改單個進程可打開的最大文件句柄數爲10240000完成

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章