/etc/security/limits.conf配置文件詳解
這個文件主要是用來限制用戶對資源的使用。是/lib64/security/pam_limits.so模塊對應的/etc/serurity/pam_limits的配置文件。下圖是直接在服務器上復制下來的文件內容。
# /etc/security/limits.conf
#
#This file sets the resource limits for the users logged in via PAM.
該文件為通過PAM登錄的用戶設置資源限制。
#It does not affect resource limits of the system services.
#它不影響系統服務的資源限制。
#Also note that configuration files in /etc/security/limits.d directory,
#which are read in alphabetical order, override the settings in this
#file in case the domain is the same or more specific.
請注意/etc/security/limits.d下按照字母順序排列的配置文件會覆蓋 /etc/security/limits.conf中的
domain相同的的配置
#That means for example that setting a limit for wildcard domain here
#can be overriden with a wildcard setting in a config file in the
#subdirectory, but a user specific setting here can be overriden only
#with a user specific setting in the subdirectory.
這意味着,例如使用通配符的domain會被子目錄中相同的通配符配置所覆蓋,但是某一用戶的特定配置
只能被字母路中用戶的配置所覆蓋。其實就是某一用戶A如果在/etc/security/limits.conf有配置,當
/etc/security/limits.d子目錄下配置文件也有用戶A的配置時,那么A中某些配置會被覆蓋。
#
#Each line describes a limit for a user in the form:
#每一行描述一個用戶配置
#<domain> <type> <item> <value>
#Where:
#<domain> can be:
# - a user name 一個用戶名
# - a group name, with @group syntax 用戶組格式為@GROUP_NAME
# - the wildcard *, for default entry 默認配置為*,代表所有用戶
# - the wildcard %, can be also used with %group syntax,
# for maxlogin limit
#
#<type> can have the two values:
# - "soft" for enforcing the soft limits
# - "hard" for enforcing hard limits
有soft,hard和-,soft指的是當前系統生效的設置值,軟限制也可以理解為警告值。
hard表名系統中所能設定的最大值。soft的限制不能比hard限制高,用-表名同時設置了soft和hard的值。
#<item> can be one of the following: <item>可以使以下選項中的一個
# - core - limits the core file size (KB) 限制內核文件的大小。
# - data - max data size (KB) 最大數據大小
# - fsize - maximum filesize (KB) 最大文件大小
# - memlock - max locked-in-memory address space (KB) 最大鎖定內存地址空間
# - nofile - max number of open file descriptors 最大打開的文件數(以文件描敘符,file descripter計數)
# - rss - max resident set size (KB) 最大持久設置大小
# - stack - max stack size (KB) 最大棧大小
# - cpu - max CPU time (MIN) 最多CPU占用時間,單位為MIN分鍾
# - nproc - max number of processes 進程的最大數目
# - as - address space limit (KB) 地址空間限制
# - maxlogins - max number of logins for this user 此用戶允許登錄的最大數目
# - maxsyslogins - max number of logins on the system 系統最大同時在線用戶數
# - priority - the priority to run user process with 運行用戶進程的優先級
# - locks - max number of file locks the user can hold 用戶可以持有的文件鎖的最大數量
# - sigpending - max number of pending signals
# - msgqueue - max memory used by POSIX message queues (bytes)
# - nice - max nice priority allowed to raise to values: [-20, 19] max nice優先級允許提升到值
# - rtprio - max realtime pr iority
#
#<domain> <type> <item> <value>
#
#* soft core 0
#* hard rss 10000
#@student hard nproc 20
#@faculty soft nproc 20
#@faculty hard nproc 50
#ftp hard nproc 0
#@student - maxlogins 4
# End of file
* soft nofile 131072
* hard nofile 131072
* soft nproc 131072
* hard nproc 131072
* soft core unlimited
* hard core unlimited
* soft memlock 50000000
* hard memlock 50000000
## abase create ##
################補充##############
一、使用數據庫如mysql的時候遇到two many open files的error,此時便可以通過將運行mysqld的用戶的 nofile(最大打開文件數)這個值改打,例如用戶為A
A - nofile VALUE
注意上面的第二列 ‘-’表示hard和soft,可以顯示的為soft和hard寫兩行
二、更改用戶資源限制有不同方法
1、可以通過使用ulimit命令:立即生效,只對本次shell會話生效
2、編輯/etc/security/limits.conf:不立即生效,重新加載的方式來使限制生效,永久有效。
三、ulimit命令
ulimit [-SHacdefilmnpqrstuvx] [limit]
-S 設置軟件資源限制
-H 設置硬件資源限制
-a 顯示當前所有的資源限制
-c size:設置core文件的最大值
-d size:設置數據段的最大值.
-f size:設置創建文件的最大值.
-l size:設置在內存中鎖定進程的最大值
-m size:設置可以使用的常駐內存的最大值
-n size:設置內核可以同時打開的文件最大值
-p size:設置管道緩沖區的最大值
-s size:設置堆棧的最大值
-t size:設置CPU使用時間的最大上限
-v size:設置虛擬內存的最大值.單位
unlimited 是一個特殊值,用於表示不限制