Linux Limit相關內容設置大全(值得收藏)


一、 /etc/security/limits.conf 詳解

/etc/security/limits.conf 文件實際是 Linux PAM(插入式認證模塊,Pluggable Authentication Modules)中 pam_limits.so 的配置文件,而且只針對於單個會話。 該設置不會影響系統服務的資源限制。還要注意 /etc/security/limits.d/ 的這個目錄,

/etc/security/limits.conf 配置解析

# /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中某些配置會被覆蓋。最終取的值是 /etc/security/limits.d 下的配置文件的配置。

#
#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
#@st

/etc/security/limits.d/目錄

  • /etc/security/limits.d/ 目錄
    該目錄下默認有 *-nproc.conf 文件,該文件是用於限制用戶的線程限制。我們也可以在該目錄創建配置文件在 /etc/security/limits.d/ 下,以 .conf 結尾。

    • centos 7

      在CentOS 7版本中為/etc/security/limits.d/20-nproc.conf

      # Default limit for number of user's processes to prevent
      # accidental fork bombs.
      # See rhbz #432903 for reasoning.
      
      *          soft    nproc     4096 # 所有的用戶默認可以打開最大的進程數為 4096
      root       soft    nproc     unlimited # root 用戶默認可以打開最大的進程數 無限制的。
      
      
    • CentOS 6

      在CentOS 6版本中為/etc/security/limits.d/90-nproc.conf

二、 ulimit 如何配置

配置注意事項

注意不能設置 nofile不能設置 unlimitednoproc可以.
當我們設置了 nofile不能設置 unlimited 后,我們進行 ssh 登錄,是登錄不了的,並且報錯下面的內容。

Dec  1 14:57:57 localhost sshd[1543]: pam_limits(sshd:session): Could not set limit for 'nofile': Operation not permitted

當我們設置的 nofile 的值可以設置的最大值為 1048576(2**20),設置的值大於該數,就會進行登錄不了。也會顯示上面的登錄錯誤。(親測)

基礎配置

我們不將所有的配置配置在/etc/security/limits.conf 而是將配置放在 /etc/security/limits.d/ 下。
比如我們將 nofile的配置放在 /etc/security/limits.d/20-nofile.conf ,nproc 的配置放在 /etc/security/limits.d/20-nproc.conf.

一般我們需要配置的 /etc/security/limits.d/20-nofile.conf 為。

root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535

/etc/security/limits.d/20-nproc.conf 設置為

*    -     nproc   65535
root soft  nproc  unlimited
root hard  nproc  unlimited

注意覆蓋點的問題。

示例一:
/etc/security/limits.conf 配置了:

root soft nofile 65538
root hard nofile 65538
* soft nofile 65539
* hard nofile 65539

這個root 用戶的 默認取值是 65538 ,* 統配符雖然在 root 配置后面,但是 root 的配置只能被 root 進行覆蓋。

我們看下這個配置,當這樣配置的時候

root soft nofile 65538
root hard nofile 65538
* soft nofile 65539
* hard nofile 65539
root soft nofile 65539

這個的 root 用戶的取值還是 65538 ,因為雖然 root soft nofile 65539 會覆蓋我們之前的配置,但是這個配置是不生效的。因為 root soft nofile 65539 配置的值大於root hard nofile 65538 , soft 配置的值不能大於 hard.

示例二:
當我們在 /etc/security/limits.conf 配置了:

root soft nofile 65538
root hard nofile 65538
* soft nofile 65539
* hard nofile 65539

然后我們在 /etc/security/limits.d/20-nofile.conf 配置了:

root soft nofile 65536
root hard nofile 65536
* soft nofile 65540
* hard nofile 65540

最后的取值是會取 /etc/security/limits.d/20-nofile.conf 里面的值。

  1. 配置,只能被特定覆蓋。
  2. /etc/security/limits.d/ 下文件的相同配置可以覆蓋 /etc/security/limits.conf
  3. softhard需要都進行設置,才能生效。
  4. nofile不能設置 unlimited
  5. nofile可以設置的最大值為 1048576(2**20),設置的值大於該數,就會進行登錄不了。
  6. soft 設置的值 一定要小於或等於 hard 的值。

具體詳細配置根據應用情況進行配置。

三、ulimit 配置后生效

臨時配置

設置可以打開文件的最大數為 65536

ulimit  -SHn  65536

重啟后失效。

永久配置

配置到配置文件/etc/security/limits.conf或者 /etc/security/limits.d/ 中。
然后退出當前會話,重新登錄。 即可生效,重啟配置也會保留。

三、ulimit 配置后生效

臨時配置

設置可以打開文件的最大數為 65536

ulimit  -SHn  65536

重啟后失效。

永久配置

配置到配置文件/etc/security/limits.conf或者 /etc/security/limits.d/ 中。
然后退出當前會話,重新登錄。 即可生效,重啟配置也會保留。

配置不生效的問題

2020年3月6日補充

問題

按照上面的配置好了之后,我們進行設置登錄到服務器,我發現是配置沒有生效的,但是我使用 su - root 之后,發現配置是生效的。 很怪異。
設備環境: Centos6.

問題原因

主要是 Centos6 的原因,我們排查到 sshd 服務的 PAM 模塊是沒有開啟的,而/etc/security/limits.conf 文件實際是 Linux PAM(插入式認證模塊,Pluggable Authentication Modules)中 pam_limits.so 的配置文件,我們沒有開啟 PAM 模塊,最終也就沒有讀取到 /etc/security/limits.conf 的內容。 而 su 進行切換的時候使用的是 終端tty登陸(默認使用PAM模塊),

解決辦法

/etc/ssh/sshd_configUsePAM no 更改為 UsePAM yes, 然后重啟 sshd 服務。

四、ulimit 常用命令

      -S	use the `soft' resource limit # 設置軟限制
      -H	use the `hard' resource limit # 設置硬限制
      -a	all current limits are reported# 顯示所有的配置。
      -b	the socket buffer size # 設置socket buffer 的最大值。
      -c	the maximum size of core files created # 設置core文件的最大值.
      -d	the maximum size of a process's data segment  # 設置線程數據段的最大值
      -e	the maximum scheduling priority (`nice') # 設置最大調度優先級
      -f	the maximum size of files written by the shell and its children # 創建文件的最大值。
      -i	the maximum number of pending signals # 設置最大的等待信號
      -l	the maximum size a process may lock into memory #設置在內存中鎖定進程的最大值
      -m	the maximum resident set size 
      -n	the maximum number of open file descriptors # 設置最大可以的打開文件描述符。
      -p	the pipe buffer size
      -q	the maximum number of bytes in POSIX message queues
      -r	the maximum real-time scheduling priority
      -s	the maximum stack size
      -t	the maximum amount of cpu time in seconds
      -u	the maximum number of user processes  # 設置用戶可以創建的最大進程數。
      -v	the size of virtual memory  # 設置虛擬內存的最大值
      -x	the maximum number of file locks

查看配置

查看所有的配置

ulimit  -a

查看配置的最大打開文件數

ulimit  -n

更改配置

ulimit  -SHn  65536

五、systemd 相關的limit

最近一次故障中才發現的問題,就是我們通過systemd 管理的服務,文件最大打開數是 1024(軟限制),硬限制是4096. 但是我們的 /etc/security/limits.conf/etc/security/limits.d/20-nofile.conf 設置的值都不是1024。

引申出來,我們的 systemd 管理的服務有單獨的limit 限制

systemd 管理的服務limit 配置取決於以下三個位置:

  1. 系統服務的默認全局配置 /etc/systemd/system.conf
  2. 用戶服務默認全局配置 /etc/systemd/user.conf
  3. 單個系統服務的配置 /usr/lib/systemd/system/*.conf

問題一 如何查看現有系統服務的Limit

  1. systemctl show sshd
[root@djx128 ~]# systemctl show  sshd |grep '^Limit'
LimitCPU=18446744073709551615
LimitFSIZE=18446744073709551615
LimitDATA=18446744073709551615
LimitSTACK=18446744073709551615
LimitCORE=18446744073709551615
LimitRSS=18446744073709551615
LimitNOFILE=4096
LimitAS=18446744073709551615
LimitNPROC=11222
LimitMEMLOCK=65536
LimitLOCKS=18446744073709551615
LimitSIGPENDING=11222
LimitMSGQUEUE=819200
LimitNICE=0
LimitRTPRIO=0
LimitRTTIME=18446744073709551615
  1. 如果是已經運行的服務,可以通過基於進程進行查看, cat /proc/pid/limits

    [root@djx128 ~]#  cat /proc/943/limits
    Limit                     Soft Limit           Hard Limit           Units     
    Max cpu time              unlimited            unlimited            seconds   
    Max file size             unlimited            unlimited            bytes     
    Max data size             unlimited            unlimited            bytes     
    Max stack size            8388608              unlimited            bytes     
    Max core file size        0                    unlimited            bytes     
    Max resident set          unlimited            unlimited            bytes     
    Max processes             11222                11222                processes 
    Max open files            1024                 4096                 files     
    Max locked memory         65536                65536                bytes     
    Max address space         unlimited            unlimited            bytes     
    Max file locks            unlimited            unlimited            locks     
    Max pending signals       11222                11222                signals   
    Max msgqueue size         819200               819200               bytes     
    Max nice priority         0                    0                    
    Max realtime priority     0                    0                    
    Max realtime timeout      unlimited            unlimited            us   
    

問題二 一個服務,如何調整 Limit 限制

  1. 改全局配置 /etc/systemd/system.conf

    改全局配置如何生效? 這個需要注意。

  2. 更改單服務配置(推薦)

    1. 增加 LimitNOFILE 配置

      vim  /usr/lib/systemd/system/mariadb.service
      [Service]
      LimitNOFILE=32768
      
    2. 重新加載配置

      systemctl daemon-reload
      
    3. 重啟服務

      systemctl restart mariadb
      

六、 擴展

  1. 如何查看當前運行進程的limit 限制

    cat /proc/進程號/limits
    
  2. 如何更改當前的進程的最大文件數限制

    getrlimit, setrlimit, prlimit - get/set resource limits

    https://man7.org/linux/man-pages/man1/prlimit.1.html

    prlimit --pid 13134 --nofile=1024:4096
    

文章參考:

/etc/security/limits.conf配置文件詳解

linux ulimit的若干坑 - ulimit真不是亂設的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM