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真不是乱设的

原文地址:https://www.cnblogs.com/operationhome

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


文章浏览阅读1.8k次,点赞63次,收藏54次。Linux下的目录权限!!!粘滞位!!!超详解!!!
文章浏览阅读1.6k次,点赞44次,收藏38次。关于Qt的安装、Windows、Linux、MacBook_mack book 安装qt
本文介绍了使用shell脚本编写一个 Hello
文章浏览阅读1.5k次,点赞37次,收藏43次。【Linux】初识Linux——了解操作系统的发展历史以及初次体验Linux编程环境
文章浏览阅读3k次,点赞34次,收藏156次。Linux超详细笔记,个人学习时很认真的记录的,觉得好的麻烦点个赞。
文章浏览阅读6.8k次,点赞109次,收藏114次。【Linux】 OpenSSH_9.3p1 升级到 OpenSSH_9.5p1(亲测无问题,建议收藏)_openssh_9.5p1
文章浏览阅读3.5k次,点赞93次,收藏78次。初识Linux中的线程,理解线程的各种概念,理解进程地址空间中的页表转换,介绍pthread线程库并理解线程库!
文章浏览阅读863次。出现此问题为Linux文件权限问题,解决方案为回到引擎目录执行命令。输入用户密码后运行./UnrealEditor。_increasing per-process limit of core file size to infinity.
文章浏览阅读2.9k次。使用文本编辑器:打开CSV文件,并使用文本编辑器(如Notepad++、Sublime Text、Visual Studio Code等)来查看文件的字符编码格式。通常在编辑器的底部状态栏或设置中可以找到当前编码的显示。请注意,上述方法并非绝对准确,特别是当文件没有明确的编码标识时。因此,如果你发现CSV文件在不同的工具或方法中显示不同的编码格式,可能需要进行进一步的分析和判断,或者尝试使用不同的编码转换方法。该命令将输出文件的MIME类型和编码信息。使用命令行工具:在命令行中,你可以使用。_shell读取csv文件逐行处理
本文介绍了如何在Linux系统中升级gcc版本,以便更好地支持C++11及以上版本的新特性。通过升级gcc,可以提升编译器的功能和性能,获得更好的开发体验。详细的步骤和方法请参考原文链接。
文章浏览阅读4.4k次,点赞6次,收藏19次。Mosquitto是一个开源的MQTT消息代理服务器。MQTT是一个轻量级的、基于发布/订阅模式的消息传输协议。 mosquitto的安装使用比较简单,可以方便的来进行一些测试。_linux mosquitto
文章浏览阅读7.2k次,点赞2次,收藏12次。Linux中,用于根目录下有一个.ssh目录,保存了ssh相关的key和一些记录文件。_~/.ssh/
文章浏览阅读4.5k次,点赞5次,收藏18次。首先需要安装 snmp ,使用下面的命令进行安装安装完毕之后,使用下面的命令查看是否安装成功当命令行显示如图即为安装成功。_snmp工具
文章浏览阅读3.5k次,点赞7次,收藏24次。本地部署和使用llama.cpp进行量化Llama2,linux和Windows平台方案,支持CPU和GPU多版本。_llama cpp gpu
文章浏览阅读1.4k次,点赞46次,收藏44次。在vim中,最为常见的有三种模式,分别是:命令模式(command mode)、插 入模式(Insert mode)和底行模式(last line mode)文件保存或退出,也可以进行文件替换,找字符串,列出行号等操作。在命令模式下,shift+: 即可进入该模 式。在进入vim后,使用 i 进入插入模式,插入模式就是vim的编辑模式,可以在vim中进行内容的编辑和修改。vim的核心模式,使用vim进入文件编辑时的最初模式,在该模式中只能移动光标和使用命令对文件内容进行编辑。
Linux常用命令大全,包括目录操作命令和文件操作命令,以及查看登录用户命令和文件内容查看命令等。
文章浏览阅读1.7k次,点赞57次,收藏50次。Yearning 简单, 高效的MYSQL 审计平台 一款MYSQL SQL语句/查询审计工具,为DBA与开发人员使用.本地部署,注重隐私,简单高效的MYSQL审计平台。下面介绍Linux 简单部署Yearning 并结合cpolar 内网穿透工具实现远程访问,破除访问限制,提高工作效率!!
文章浏览阅读1.9w次,点赞7次,收藏18次。Microsoft Edge是一款现代化的浏览器,它拥有众多功能和强大的性能,为用户带来更加流畅的浏览体验。Edge最近推出了分屏功能,支持一个窗口同时显示两个选项卡,这可以大大提高生产力和多任务处理能力。欢迎大家使用分屏及其他新功能后分享自己的使用心得与建议。首先,使用Microsoft Edge的分屏功能确实能够提高生产力,尤其是在需要同时浏览两个不同网页的情况下。分屏功能使得在一个窗口中同时显示两个选项卡,用户可以在两个网页之间快速切换,而无需打开新的窗口或使用多个浏览器窗口。_edge linux
文章浏览阅读1.8k次,点赞83次,收藏71次。C语言实现倒计时和进度条并进行演示。_linux不换行输出倒计时
文章浏览阅读1.5k次,点赞28次,收藏25次。基于嘉立创泰山派开发板,本机使用VMware+Unbuntu,编译泰山派的Linux SDK。详细教程,0基础小白可操作!内涵大量基础操作和linux基本知识。_立创泰山派sdk编译