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

    文章

    10990

    浏览量

    206734
  • 进程
    +关注

    关注

    0

    文章

    193

    浏览量

    13876

原文标题:Linux中Limit相关内容设置大全(值得收藏)

文章出处:【微信号:magedu-Linux,微信公众号:马哥Linux运维】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    Linux LED子系统详解

    Linux LED子系统详解
    的头像 发表于 06-10 10:37 985次阅读
    <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版。《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系统命令及其使用详解 _120页

    电子发烧友网站提供《Linux系统命令及其使用详解 _120页.doc》资料免费下载
    发表于 04-18 13:23 3次下载

    linux命令大全

    linux命令大全,又需要的朋友可以下来看看。
    发表于 12-11 16:57 0次下载

    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次下载