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

/etc/security/limits.conf 文件实际是 Linux PAM(插入式认证模块,Pluggable Authentication Modules)中 pam_limits.so 的配置文件,而且只针对于单个会话。 该设置不会影响系统服务的资源限制。还要注意 /etc/security/limits.d/ 的这个目录,

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

  1. # /etc/security/limits.conf
  2. #
  3. #This file sets the resource limits for the users logged in via PAM.
  4. 该文件为通过PAM登录的用户设置资源限制。
  5. #It does not affect resource limits of the system services.
  6. #它不影响系统服务的资源限制。
  7. #Also note that configuration files in /etc/security/limits.d directory,
  8. #which are read in alphabetical order, override the settings in this
  9. #file in case the domain is the same or more specific.
  10. 请注意/etc/security/limits.d下按照字母顺序排列的配置文件会覆盖 /etc/security/limits.conf中的
  11. domain相同的的配置
  12. #That means for example that setting a limit for wildcard domain here
  13. #can be overriden with a wildcard setting in a config file in the
  14. #subdirectory, but a user specific setting here can be overriden only
  15. #with a user specific setting in the subdirectory.
  16. 这意味着,例如使用通配符的domain会被子目录中相同的通配符配置所覆盖,但是某一用户的特定配置
  17. 只能被字母路中用户的配置所覆盖。其实就是某一用户A如果在/etc/security/limits.conf有配置,当
  18. /etc/security/limits.d子目录下配置文件也有用户A的配置时,那么A中某些配置会被覆盖。最终取的值是 /etc/security/limits.d 下的配置文件的配置。
  19. #
  20. #Each line describes a limit for a user in the form:
  21. #每一行描述一个用户配置
  22. #<domain> <type> <item> <value>
  23. #Where:
  24. #<domain> can be:
  25. # - a user name 一个用户名
  26. # - a group name, with @group syntax 用户组格式为@GROUP_NAME
  27. # - the wildcard *, for default entry 默认配置为*,代表所有用户
  28. # - the wildcard %, can be also used with %group syntax,
  29. # for maxlogin limit
  30. #
  31. #<type> can have the two values:
  32. # - "soft" for enforcing the soft limits
  33. # - "hard" for enforcing hard limits
  34. softhard和-,soft指的是当前系统生效的设置值,软限制也可以理解为警告值。
  35. hard表名系统中所能设定的最大值。soft的限制不能比hard限制高,用-表名同时设置了softhard的值。
  36. #<item> can be one of the following: <item>可以使以下选项中的一个
  37. # - core - limits the core file size (KB) 限制内核文件的大小。
  38. # - data - max data size (KB) 最大数据大小
  39. # - fsize - maximum filesize (KB) 最大文件大小
  40. # - memlock - max locked-in-memory address space (KB) 最大锁定内存地址空间
  41. # - nofile - max number of open file descriptors 最大打开的文件数(以文件描叙符,file descripter计数)
  42. # - rss - max resident set size (KB) 最大持久设置大小
  43. # - stack - max stack size (KB) 最大栈大小
  44. # - cpu - max CPU time (MIN) 最多CPU占用时间,单位为MIN分钟
  45. # - nproc - max number of processes 进程的最大数目
  46. # - as - address space limit (KB) 地址空间限制
  47. # - maxlogins - max number of logins for this user 此用户允许登录的最大数目
  48. # - maxsyslogins - max number of logins on the system 系统最大同时在线用户数
  49. # - priority - the priority to run user process with 运行用户进程的优先级
  50. # - locks - max number of file locks the user can hold 用户可以持有的文件锁的最大数量
  51. # - sigpending - max number of pending signals
  52. # - msgqueue - max memory used by POSIX message queues (bytes)
  53. # - nice - max nice priority allowed to raise to values: [-20, 19] max nice优先级允许提升到值
  54. # - rtprio - max realtime pr iority
  55. #
  56. #<domain> <type> <item> <value>
  57. #
  58. #* soft core 0
  59. #* hard rss 10000
  60. #@student hard nproc 20
  61. #@faculty soft nproc 20
  62. #@faculty hard nproc 50
  63. #ftp hard nproc 0
  64. #@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

      1. # Default limit for number of user's processes to prevent
      2. # accidental fork bombs.
      3. # See rhbz #432903 for reasoning.
      4. * soft nproc 4096 # 所有的用户默认可以打开最大的进程数为 4096
      5. root soft nproc unlimited # root 用户默认可以打开最大的进程数 无限制的。
    • CentOS 6

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

二、 ulimit 如何配置

配置注意事项

注意不能设置 nofile不能设置 unlimitednoproc可以.

当我们设置了 nofile不能设置 unlimited 后,我们进行 ssh 登录,是登录不了的,并且报错下面的内容。

  1. 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 为。

  1. root soft nofile 65535
  2. root hard nofile 65535
  3. * soft nofile 65535
  4. * hard nofile 65535

/etc/security/limits.d/20-nproc.conf 设置为

  1. * - nproc 65535
  2. root soft nproc unlimited
  3. root hard nproc unlimited

注意覆盖点的问题。

示例一:

/etc/security/limits.conf 配置了:

  1. root soft nofile 65538
  2. root hard nofile 65538
  3. * soft nofile 65539
  4. * hard nofile 65539

这个root 用户的 默认取值是 65538 ,* 统配符虽然在 root 配置后面,但是 root 的配置只能被 root 进行覆盖。

我们看下这个配置,当这样配置的时候

  1. root soft nofile 65538
  2. root hard nofile 65538
  3. * soft nofile 65539
  4. * hard nofile 65539
  5. root soft nofile 65539

这个的 root 用户的取值还是 65538 ,因为虽然 root soft nofile 65539 会覆盖我们之前的配置,但是这个配置是不生效的。因为 root soft nofile 65539 配置的值大于root hard nofile 65538 , soft 配置的值不能大于 hard.

示例二:

当我们在 /etc/security/limits.conf 配置了:

  1. root soft nofile 65538
  2. root hard nofile 65538
  3. * soft nofile 65539
  4. * hard nofile 65539

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

  1. root soft nofile 65536
  2. root hard nofile 65536
  3. * soft nofile 65540
  4. * 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

  1. ulimit -SHn 65536

重启后失效。

永久配置

配置到配置文件/etc/security/limits.conf或者 /etc/security/limits.d/ 中。

然后退出当前会话,重新登录。 即可生效,重启配置也会保留。

三、ulimit 配置后生效

临时配置

设置可以打开文件的最大数为 65536

  1. 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 常用命令

  1. -S use the `soft' resource limit # 设置软限制
  2. -H use the `hard' resource limit # 设置硬限制
  3. -a all current limits are reported# 显示所有的配置。
  4. -b the socket buffer size # 设置socket buffer 的最大值。
  5. -c the maximum size of core files created # 设置core文件的最大值.
  6. -d the maximum size of a process's data segment # 设置线程数据段的最大值
  7. -e the maximum scheduling priority (`nice') # 设置最大调度优先级
  8. -f the maximum size of files written by the shell and its children # 创建文件的最大值。
  9. -i the maximum number of pending signals # 设置最大的等待信号
  10. -l the maximum size a process may lock into memory #设置在内存中锁定进程的最大值
  11. -m the maximum resident set size
  12. -n the maximum number of open file descriptors # 设置最大可以的打开文件描述符。
  13. -p the pipe buffer size
  14. -q the maximum number of bytes in POSIX message queues
  15. -r the maximum real-time scheduling priority
  16. -s the maximum stack size
  17. -t the maximum amount of cpu time in seconds
  18. -u the maximum number of user processes # 设置用户可以创建的最大进程数。
  19. -v the size of virtual memory # 设置虚拟内存的最大值
  20. -x the maximum number of file locks

查看配置

查看所有的配置

  1. ulimit -a

查看配置的最大打开文件数

  1. ulimit -n

更改配置

  1. 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
  1. [root@djx128 ~]# systemctl show sshd |grep '^Limit'
  2. LimitCPU=18446744073709551615
  3. LimitFSIZE=18446744073709551615
  4. LimitDATA=18446744073709551615
  5. LimitSTACK=18446744073709551615
  6. LimitCORE=18446744073709551615
  7. LimitRSS=18446744073709551615
  8. LimitNOFILE=4096
  9. LimitAS=18446744073709551615
  10. LimitNPROC=11222
  11. LimitMEMLOCK=65536
  12. LimitLOCKS=18446744073709551615
  13. LimitSIGPENDING=11222
  14. LimitMSGQUEUE=819200
  15. LimitNICE=0
  16. LimitRTPRIO=0
  17. LimitRTTIME=18446744073709551615
  1. 如果是已经运行的服务,可以通过基于进程进行查看, cat /proc/pid/limits

    1. [root@djx128 ~]# cat /proc/943/limits
    2. Limit Soft Limit Hard Limit Units
    3. Max cpu time unlimited unlimited seconds
    4. Max file size unlimited unlimited bytes
    5. Max data size unlimited unlimited bytes
    6. Max stack size 8388608 unlimited bytes
    7. Max core file size 0 unlimited bytes
    8. Max resident set unlimited unlimited bytes
    9. Max processes 11222 11222 processes
    10. Max open files 1024 4096 files
    11. Max locked memory 65536 65536 bytes
    12. Max address space unlimited unlimited bytes
    13. Max file locks unlimited unlimited locks
    14. Max pending signals 11222 11222 signals
    15. Max msgqueue size 819200 819200 bytes
    16. Max nice priority 0 0
    17. Max realtime priority 0 0
    18. Max realtime timeout unlimited unlimited us

问题二 一个服务,如何调整 Limit 限制

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

    改全局配置如何生效? 这个需要注意。

  2. 更改单服务配置(推荐)

    1. 增加 LimitNOFILE 配置

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

      1. systemctl daemon-reload
    3. 重启服务

      1. systemctl restart mariadb

六、 扩展

  1. 如何查看当前运行进程的limit 限制

    1. cat /proc/进程号/limits
  2. 如何更改当前的进程的最大文件数限制

    getrlimit, setrlimit, prlimit - get/set resource limits

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

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

文章参考:

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

linux ulimit的若干坑 - ulimit真不是乱设的

Linux Limit相关内容设置大全(值得收藏)的更多相关文章

  1. web前端/移动端H5博客专家博客大全--值得收藏的前端技术大牛博客地址

    web前端/移动端H5博客专家博客大全--值得收藏的前端技术大牛博客地址   Huang Jie Blog .Com-前端开发 http://www.huangjieblog.com/?feed=rs ...

  2. 搭建MHA环境【1】规划+linux相关的设置

    [1]规划 MHA这套软件包涵两个部分 1.manager :主要负责对MySQL集群状态的检查&在master 库宕机时对故障进行转移. 2.node      :主要包涵状态检查& ...

  3. linux用户权限相关内容查看

    linux用户权限相关内容查看 1   用户信息 创建用户一个名为 webuser 的账号,并填写相应的信息: root@iZ94fabhqhuZ:~# adduser webuser Adding ...

  4. 在地图中调用显示FeatureLayer并进行render、popupTemplate、添加图例等相关内容的设置

    ArcGIS Server发布完FeatureLayer后,就可以在自己的代码中调用并在地图上显示出来了. 一.代码框架 调用FeatureLayer,要在require开头引入"esri/ ...

  5. [转帖] Linux buffer 和 cache相关内容

    Linux中Buffer/Cache清理 Lentil2018年9月6日 Linux中的buff/cache可以被手动释放,释放缓存的代码如下: https://lentil1016.cn/linux ...

  6. [转帖]超详细的PostgreSQL体系结构总结,值得收藏

    超详细的PostgreSQL体系结构总结,值得收藏 https://www.toutiao.com/i6715390855772897800/ 原创 波波说运维 2019-07-26 00:03:00 ...

  7. 前端学习——css基础知识,选择器与html模板、值得收藏的html标签

    一.css需要的html(采用html5标准) DTD,文档类型声明: <!Doctype html> 文本编码声明: <meta charset="utf-8" ...

  8. linux常用命令加实例大全

    目  录引言    1一.安装和登录    2(一)    login    2(二)    shutdown    2(三)    halt    3(四)    reboot    3(五)    ...

  9. linux云服务器常用设置

    前面的话 由于在云服务器上配置自己的网站,将Linux里的常用设置记录如下 更改shell 默认地, ubuntu系统默认的shell是dash,但更常用的shell是bash 通过下面命令可以将da ...

随机推荐

  1. frida hook_RegisterNatives--使用frida打印so中动态注册的函数

    原文地址:https://github.com/lasting-yang/frida_hook_libartfrida -U --no-pause -f package_name -l hook_Re ...

  2. MySQL的安装及使用

    安装MySQL 这里建议大家使用压缩版,安装快,方便.不复杂. 1.MySQL软件下载 mysql5.7 64位下载地址: https://dev.mysql.com/get/Downloads/My ...

  3. KVM虚拟化配置

    KVM虚拟化 虚拟化概念 KVM虚拟化概念详讲 虚拟化配置 首先开启虚拟化的支持 并且增加一个50g的硬盘 free查看内存 grep -Ei 'vmx|svm' /proc/cpuinfo查看虚拟机 ...

  4. 亲测有效,解决80端口被svchost.exe进程占用的问题,网上的方法不行,可以试试这个

    先说网上无效的方法(个人尝试无效,不具有代表性): 网上第一个说法:把IIS给关了,Windows10系统本身IIS是处于禁用状态的,并且没有额外安装IIS和启动IIS. 网上第二个说法:和SQL S ...

  5. 手摸手教你阅读和调试大型开源项目 ZooKeeper

    本文作者:HelloGitHub-老荀 Hi,这里是 HelloGitHub 推出的 HelloZooKeeper 系列,免费开源.有趣.入门级的 ZooKeeper 教程,面向有编程基础的新手. 项 ...

  6. Android创建自定义Application

    开发目的 创建一个自定义的MainApplication继承Application. 读取AndroidManifest.xml文件中Application节点的META-DATA数据.此处以ApiK ...

  7. 附031.Kubernetes_v1.20.4高可用部署架构二

    kubeadm介绍 kubeadm概述 参考附003.Kubeadm部署Kubernetes. kubeadm功能 参考附003.Kubeadm部署Kubernetes. 本方案描述 本方案采用kub ...

  8. Leedcode算法专题训练(链表)

    1.发现两个链表的交点 160.两个链表的交集(容易) Leetcode /力扣 public class Solution { public ListNode getIntersectionNode ...

  9. kafka listeners和advertised配置

    kafka  listeners和advertised配置 kafka版本:kafka_2.11-2.3.0 kafka配置listeners # The address the socket ser ...

  10. 大数翻倍法求解CRT

    目录 正文 引入 大数翻倍法 复杂度证明 大数翻倍法的优势 最后的最后:上代码! 注:做法和思路是 zhx 在一次讲课中提出的,如有侵权,请联系作者删除 其实别的题解也有提到过暴力做法,但这里将会给出 ...