<acronym id="s8ci2"><small id="s8ci2"></small></acronym>
<rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
<acronym id="s8ci2"></acronym>
<acronym id="s8ci2"><center id="s8ci2"></center></acronym>
0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

Linux系統中Limit設置詳解大全

馬哥Linux運維 ? 來源:馬哥Linux運維 ? 2024-03-22 11:02 ? 次閱讀

一、/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:
#每一行描述一個用戶配置
#   

#Where:
# 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 
#
# can have the two values:
# - "soft" for enforcing the soft limits 
# - "hard" for enforcing hard limits
有soft,hard和-,soft指的是當前系統生效的設置值,軟限制也可以理解為警告值。
hard表名系統中所能設定的最大值。soft的限制不能比hard限制高,用-表名同時設置了soft和hard的值。
# 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 最大打開的文件數(以文件描敘符,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
#
#   
#

#* 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不能設置unlimited,noproc可以.
當我們設置了nofile不能設置unlimited后,我們進行 ssh 登錄,是登錄不了的,并且報錯下面的內容。

Dec  1 1457 localhost sshd[1543]: pam_limits(sshd 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里面的值。

配置,只能被特定覆蓋。

/etc/security/limits.d/下文件的相同配置可以覆蓋/etc/security/limits.conf

soft和hard需要都進行設置,才能生效。

nofile不能設置unlimited

nofile可以設置的最大值為 1048576(2**20),設置的值大于該數,就會進行登錄不了。

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_config將UsePAM no更改為UsePAM yes, 然后重啟 sshd 服務。

四、ulimit 常用命令

      -Suse the `soft' resource limit # 設置軟限制
      -Huse the `hard' resource limit # 設置硬限制
      -aall current limits are reported# 顯示所有的配置。
      -bthe socket buffer size # 設置socket buffer 的最大值。
      -cthe maximum size of core files created # 設置core文件的最大值.
      -dthe maximum size of a process's data segment  # 設置線程數據段的最大值
      -ethe maximum scheduling priority (`nice') # 設置最大調度優先級
      -fthe maximum size of files written by the shell and its children # 創建文件的最大值。
      -ithe maximum number of pending signals # 設置最大的等待信號
      -lthe maximum size a process may lock into memory #設置在內存中鎖定進程的最大值
      -mthe maximum resident set size 
      -nthe maximum number of open file descriptors # 設置最大可以的打開文件描述符。
      -pthe pipe buffer size
      -qthe maximum number of bytes in POSIX message queues
      -rthe maximum real-time scheduling priority
      -sthe maximum stack size
      -tthe maximum amount of cpu time in seconds
      -uthe maximum number of user processes  # 設置用戶可以創建的最大進程數。
      -vthe size of virtual memory  # 設置虛擬內存的最大值
      -xthe 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 配置取決于以下三個位置:

系統服務的默認全局配置/etc/systemd/system.conf

用戶服務默認全局配置/etc/systemd/user.conf

單個系統服務的配置/usr/lib/systemd/system/*.conf

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

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

如果是已經運行的服務,可以通過基于進程進行查看,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 限制

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

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

方法一:直接重啟主機

方法二:執行systemctl daemon-reexec, 然后重啟服務即可。

daemon-reexec 和 daemon-reload 相比。https://serverfault.com/questions/805745/reboot-or-systemctl-daemon-reload-for-changes-to-etc-systemd-system-conf/805751

方法三:使用prlimit調整。

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

更改單服務配置(推薦)

增加LimitNOFILE配置

vim  /usr/lib/systemd/system/mariadb.service
[Service]
LimitNOFILE=32768

重新加載配置

systemctl daemon-reload

重啟服務

systemctl restart mariadb

六、 擴展

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

cat /proc/進程號/limits

如何更改當前的進程的最大文件數限制

getrlimit, setrlimit, prlimit - get/set resource limits

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

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

審核編輯:黃飛

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • Linux
    +關注

    關注

    87

    文章

    11025

    瀏覽量

    207141
  • 進程
    +關注

    關注

    0

    文章

    196

    瀏覽量

    13905

原文標題:Linux中Limit相關內容設置大全(值得收藏)

文章出處:【微信號:magedu-Linux,微信公眾號:馬哥Linux運維】歡迎添加關注!文章轉載請注明出處。

收藏 人收藏

    評論

    相關推薦

    Linux LED子系統詳解

    Linux LED子系統詳解
    的頭像 發表于 06-10 10:37 ?1107次閱讀
    <b class='flag-5'>Linux</b> LED子<b class='flag-5'>系統</b><b class='flag-5'>詳解</b>

    PCB加工流程詳解大全

    PCB加工流程詳解大全PCB的功能為提供完成第一層級構裝的元件與其它必須的電子電路零件接合的基地,以組成一個具特定功能的模組或成品。所以PCB在整個電子產品,扮演了整合連結總其成所有功能的角色,也
    發表于 11-30 17:29

    Linux系統命令及其使用詳解

    Linux系統命令及其使用詳解
    發表于 08-20 13:38

    ARM嵌入式Linux系統開發詳解

    大量讀者好評的“Linux典藏大系”的《ARM嵌入式Linux系統開發詳解》的第2版?!?b class='flag-5'>Linux
    發表于 09-14 08:57

    詳解linux的各種limit設置

    linuxlimit參數設定一般可以通過ulimit命令或編輯/etc/security/limits.conf重新加載的方式使之生效
    發表于 07-22 06:25

    Linux啟動過程詳解

    1、Linux 基礎安裝Linux操作系統 Linux文件系統 Linux常用命令
    發表于 11-02 07:01

    Linux命令大全(修改版)

    Linux命令大全(修改版) ftp(file transfer protocol) 功能說明:設置文件系統相關功能。 語  法:ftp [-dignv][主機名稱或IP地址]
    發表于 03-26 16:44 ?0次下載

    Linux系統命令及其使用詳解 _120頁

    電子發燒友網站提供《Linux系統命令及其使用詳解 _120頁.doc》資料免費下載
    發表于 04-18 13:23 ?3次下載

    PCB加工流程詳解大全

    PCB加工流程詳解大全
    發表于 02-14 16:07 ?0次下載

    linux /Android 基礎知識總結大全

    本文檔介紹了linux /Android 基礎知識總結大全,包含了源代碼以及詳解,供網友參考。
    發表于 09-11 17:46 ?7次下載

    Linux設備驅動開發詳解》第5章、Linux文件系統與設備文件系統

    Linux設備驅動開發詳解》第5章、Linux文件系統與設備文件系統
    發表于 10-27 14:13 ?0次下載
    《<b class='flag-5'>Linux</b>設備驅動開發<b class='flag-5'>詳解</b>》第5章、<b class='flag-5'>Linux</b>文件<b class='flag-5'>系統</b>與設備文件<b class='flag-5'>系統</b>

    Linux的命令大全

    Linux的命令大全
    發表于 10-30 09:49 ?8次下載
    <b class='flag-5'>Linux</b>的命令<b class='flag-5'>大全</b>

    DSP指令詳解大全(詳細)

    DSP指令詳解大全(詳細)
    發表于 05-22 10:27 ?9次下載

    Linux教程之Linux系統指令大全資料免費下載

    本文檔的主要內容詳細介紹的是Linux教程之Linux系統指令大全資料免費下載
    發表于 12-29 16:48 ?30次下載
    <b class='flag-5'>Linux</b>教程之<b class='flag-5'>Linux</b>的<b class='flag-5'>系統</b>指令<b class='flag-5'>大全</b>資料免費下載

    Linux系統中EXP命令詳解質量匯總

    Linux系統中EXP命令詳解質量匯總
    發表于 05-14 09:35 ?1次下載
    亚洲欧美日韩精品久久_久久精品AⅤ无码中文_日本中文字幕有码在线播放_亚洲视频高清不卡在线观看
    <acronym id="s8ci2"><small id="s8ci2"></small></acronym>
    <rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
    <acronym id="s8ci2"></acronym>
    <acronym id="s8ci2"><center id="s8ci2"></center></acronym>