1. 程式人生 > >Linux系統 /etc/security/limits.conf 配置

Linux系統 /etc/security/limits.conf 配置

val locks 信息 gre 保護 owin 工作 ica entry

1. limits.conf 配置文件描述

limits.conf文件實際是Linux PAM(插入式認證模塊,Pluggable Authentication Modules)中 pam_limits.so 的配置文件,突破系統的默認限制,對系統訪問資源有一定保護作用。 limits.conf 和sysctl.conf區別在於limits.conf是針對用戶,而sysctl.conf是針對整個系統參數配置。

2. limits.conf工作原理

limits.conf是 pam_limits.so的 配置文件,然後/etc/pam.d/下的應用程序調用pam_***.so模塊。譬如說,當用戶訪問服務器,服務程序將請求發送到PAM模塊,PAM模塊根據服務名稱在/etc/pam.d目 錄下選擇一個對應的服務文件,然後根據服務文件的內容選擇具體的PAM模塊進行處理。

3. limit.conf文件格式

# /etc/security/limits.conf
#
#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
#        - 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 # #<item> can be one of the following: # - 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 # - rss - max resident set size (KB) # - stack - max stack size (KB) # - cpu - max CPU time (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] # - rtprio - max realtime priority # #<domain> <type> <item> <value> # #* soft core 0 #* hard rss 10000 [email protected] hard nproc 20 [email protected] soft nproc 20 [email protected] hard nproc 50 #ftp hard nproc 0 [email protected] - maxlogins 4 # End of file * soft core 0 * hard core 0 * soft nofile 32768 * hard nofile 65536

[email protected] type resource limit

resource 表示要限制的資源

core - 限制內核文件的大小
何謂core文件,當一個程序崩潰時,在進程當前工作目錄的core文件中復制了該進程的存儲圖像。core文件僅僅是一個內存映象(同時加上調試信息),主要是用來調試的。 core文件是個二進制文件,需要用相應的工具來分析程序崩潰時的內存映像,系統默認core文件的大小為0,所以沒有被創建。可以用ulimit命令查看和修改core文件的大小,例如:
#ulimit –c
0
#ulimit -c 1000
-c 指定修改core文件的大小,1000指定了core文件大小。也可以對core文件的大小不做限制,如: ulimit -c unlimited
註意:如果想讓修改永久生效,則需要修改配置文件,如 .bash_profile、/etc/profile或/etc/security/limits.conf

nofile - 打開文件的最大數目

對於需要做許多套接字連接並使它們處於打開狀態的應用程序而言,最好通過使用ulimit -n,或者通過設置nofile參數,為用戶把文件描述符的數量設置得比默認值高一些

maxlogins - 此用戶允許登錄的最大數目
註意:要使 limits.conf 文件配置生效,必須要確保 pam_limits.so 文件被加入到啟動文件中。查看 /etc/pam.d/login 文件中有:session required /lib/security/pam_limits.so

4. limits.conf 設置使用

示例,若機器上部署了ORACLE數據庫,我們需要對oracle用戶的資源做下調整,如下:

# oracle limits resource
oracle  soft  nproc   2047
oracle  hard  nproc   16384
oracle  soft  nofile  1024
oracle  hard  nofile  65536
oracle  soft  stack   10240

要使 limits.conf 文件配置生效,必須要確保 pam_limits.so 文件被加入到啟動文件中。查看 /etc/pam.d/login 文件中有:

session required /lib/security/pam_limits.so

--查看當前系統配置文件ulimit的全局配置

ulimit -a

如果未進行具體設置的話,會使用默認配置,如下查看:

grep ^* /etc/security/limits.conf

批註:當使用*號讓全局用戶生效的時候,生效的nproc的值大小是受文件/etc/security/limits.d/90-nproc.conf中nproc值大小制約的,而如果僅僅是針對某個用戶,那麽就不受該文件nproc值大小的影響。

Linux系統 /etc/security/limits.conf 配置