Linux systemd limits


 limits

關於Centos 7 / RHEL 7 中的limits要了解以下幾點:

  1. CentOS 7 / RHEL 7的系統中,使用Systemd替代了之前系統版本中的SysV。
  2. Systemd 中的 /etc/security/limits.conf 文件的配置作用域縮小了,只適用於通過PAM認證登錄用戶的資源限制,對Systemd的service的資源限制不生效。
  3. 登錄用戶的限制,通過 /etc/security/limits.conf/etc/security/limits.d/*.conf 來配置。
  4. 系統全局的配置,在文件 /etc/systemd/system.conf/etc/systemd/system.conf.d/*.conf 來設定。
  5. 用戶全局的配置,在文件 /etc/systemd/user.conf /etc/systemd/user.conf.d/*.conf 來設定。
  6. *.conf.d/*.conf 文件會覆蓋  *.conf ,一般的sevice,使用system.conf中的配置即可。

 

 例:編輯 limits.conf 配置文件在文件尾部添加以下內容

  vim /etc/security/limits.conf

* - nofile 1024000
* - nproc 1024000

  等於

* soft nofile 1024000
* hard nofile 1024000
* soft nproc 1024000
* hard nproc 1024000

兩段配置參數為什么相等,在下面《添加格式》中有詳細的參數說明。

參數生效:

  1. working下執行 source /etc/profile 
  2. 退出當前shell重新登錄
  3. 臨時生效可用ulimit -n number

查看當前:

  1. 進程:cat /proc/<process_pid>/limits
  2. lsof -p $process_pid 每個文件描述符的具體屬性
  3. lsof -p $process_pid | wc -l  當前進程file descriptor table中FD的總量
  4. 用戶:ulimit -n -u

    ulimit(選項):

      -a:顯示目前資源限制的設定;
      -c <core文件上限>:設定core文件的最大值,單位為區塊;
      -d <數據節區大小>:程序數據節區的最大值,單位為KB;
      -f <文件大小>:shell所能建立的最大文件,單位為區塊;
      -H:設定資源的硬性限制,也就是管理員所設下的限制;
      -m <內存大小>:指定可使用內存的上限,單位為KB;
      -n <文件數目>:指定同一時間最多可開啟的文件數;
      -p <緩沖區大小>:指定管道緩沖區的大小,單位512字節;
      -s <堆疊大小>:指定堆疊的上限,單位為KB;
      -S:設定資源的彈性限制;
      -t <CPU時間>:指定CPU使用時間的上限,單位為秒;
      -u <程序數目>:用戶最多可開啟的程序數目;
      -v <虛擬內存大小>:指定可使用的虛擬內存上限,單位為KB。

 

ulimits文件中的添加格式:

[username | @groupname]  type  resource [ limit | unlimited ]

 

  [username | @groupname]:

   username:設置需要被限制的用戶名,

   @groupname:組名前面加@和用戶名區別。

   也可以用通配符*來做所有用戶的限制。

 

  type:有 soft,hard 和 -,
    soft :指的是當前系統生效的設置值。
    hard:表明系統中所能設定的最大值。
    -      :表明同時設置了 soft 和 hard 的值。
    注意:soft 的限制不能比hard 限制高

 

  resource:
    core - 限制內核文件的大小(kb)
    date - 最大數據大小(kb)
    fsize - 最大文件大小(kb)
    memlock - 最大鎖定內存地址空間(kb)
    nofile - 打開文件的最大數目
    rss - 最大持久設置大小(kb)
    stack - 最大棧大小(kb)
    cpu - 以分鍾為單位的最多 CPU 時間
    noproc - 進程的最大數目
    as - 地址空間限制
    maxlogins - 此用戶允許登錄的最大數目

 

  [  limit | unlimited ]:

    limit   :限制所用的數字,具體可根據服務器的硬件配置和服務所需自定義。
    unlimited:無限制

 


/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=
#DefaultLimitRSS=
#DefaultLimitNOFILE=
#DefaultLimitAS=
#DefaultLimitNPROC=
#DefaultLimitMEMLOCK=
#DefaultLimitLOCKS=
#DefaultLimitSIGPENDING=
#DefaultLimitMSGQUEUE=
#DefaultLimitNICE=
#DefaultLimitRTPRIO=
#DefaultLimitRTTIME=
CentOS Linux release 7.4.1708 (Core) 

默認 system.conf  文件中給很多可限制的參數

文件和進程的參數如下:

DefaultLimitNOFILE=1024000
DefaultLimitNPROC=1024000

參數生效:

systemctl daemon-reload
systemctl restart service_name.service

 

 

File Descriptors的設置

 /proc/sys/fs/file-max決定了當前內核可以打開的最大的文件句柄數

使用shell計算

grep -r MemTotal /proc/meminfo | awk '{printf("%d",$2/10)}'

 

設置:

  臨時生效

sysctl -w fs.file-max=100000 

  永久生效

vim /etc/sysctl.conf + fs.file-max = 100000

  查看

cat /proc/sys/fs/file-max

or

sysctl fs.file-max

 

/proc/sys/fs/file-nr中的值由三部分組成,分別為:

  • 已經分配的文件句柄數
  • 已經分配單沒有使用的文件句柄數
  • 最大文件句柄數

第二項的值總為0,這並不是一個錯誤,它實際上意味着已經分配的文件句柄無一浪費的都已經被使用了
查找文件句柄問題的時候,還有一個很實用的程序lsof,可以很方便看到某個進程開了那些句柄,也可以看到某個文件/目錄被什么進程占用了

lsof -n |awk '{print $2}'|sort|uniq -c |sort -nr|more

 

  • file-max是內核級別的,所有的進程總和不能超過這個數
  • ulimit是進程級別的

 

 

 
       


免責聲明!

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



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