暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Linux用户资源限制

IT那活儿 2024-04-19
1101

点击上方“IT那活儿”公众号--专注于企业全栈运维技术分享,不管IT什么活儿,干就完了!!!


  

为了防止因过度使用系统资源而导致系统崩溃或性能下降,我们可以通过合理地设置资源限制来提高系统的性能和稳定性。

更改用户资源限制通常有两种方法:
1. 使用ulimit命令

2. 修改/etc/security/limits.conf文件



ulimit命令
ulimit命令是Linux系统中用于控制shell程序的资源限制的命令。它可以用来设置和查看各种资源限制,包括最大文件大小、最大进程数、最大打开文件数等等。这些设置可以通过ulimit命令来修改,并且可以在启动脚本中设置默认值。
ulimit命令的语法如下:
ulimit [-aHS] 
[-c <core file>][-d <data size>][-f <file size>][-m <memory size>][-n <number of files>]
[-p <buffer size>][-s <stack size>][-t <time>]
[-u username|resource|block|core|data|filesize|process|stack|cputime]

  • -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


/etc/security/limits.conf文件

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

2.1 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

2.2 /etc/security/limits.d/目录

/etc/security/limits.d/ 目录下默认有 *-nproc.conf 文件,该文件是用于限制用户的线程限制。我们也可以在该目录创建配置文件在 /etc/security/limits.d/ 下,以 .conf 结尾。
  • 在CentOS 7版本中为/etc/security/limits.d/20-nproc.conf
  • 在CentOS 6版本中为/etc/security/limits.d/90-nproc.conf

2.3 配置覆盖

1)配置,只能被特定覆盖
2)/etc/security/limits.d/ 下文件的相同配置可以覆盖 /etc/security/limits.conf
3)soft和hard需要都进行设置,才能生效
4)soft 设置的值 一定要小于或等于 hard 的值
5)nofile不能设置 unlimited

6)nofile可以设置的最大值为 1048576(2**20),设置的值大于该数,就会进行登录不了


配置生效问题

使用ulimit命令修改限制值后只是临时生效,重启后即会失效;
配置到/etc/security/limits.conf或者 /etc/security/limits.d/ 中,需要退出当前会话,重新登录。即可生效,重启配置也会保留。

3.1 systemd 管理的服务有单独的limit限制

systemd 管理的服务limit 配置取决于以下三个位置:
  • 系统服务的默认全局配置 /etc/systemd/system.conf
  • 用户服务默认全局配置 /etc/systemd/user.conf
  • 单个系统服务的配置 /usr/lib/systemd/system/*.conf
可以通过systemctl show ${service}查看。

3.2 systemd 管理的服务修改limit限制

1)改全局配置 /etc/systemd/system.conf
  • 修改后直接重启主机生效
  • 执行 systemctl daemon-reexec , 然后重启服务生效
2)更改单服务配置(推荐)
  • 增加 LimitNOFILE 配置
  • vim  /usr/lib/systemd/system/mariadb.service
    [Service]
    LimitNOFILE=32768
  • 重新加载配置
    systemctl daemon-reload
  • 重启服务
    systemctl restart mariadb

3.3 PAM 模块没有开启

当PAM模块没有开启时,修改资源限制后重新登录是不生效的;
因为/etc/security/limits.conf 文件实际是 Linux PAM(插入式认证模块,Pluggable Authentication Modules)中 pam_limits.so 的配置文件,没有开启 PAM 模块,最终也就没有读取到 /etc/security/limits.conf 的内容。
在 /etc/ssh/sshd_config 将 UsePAM no 更改为 UsePAM yes, 然后重启 sshd 服务。

END


本文作者:吴 磊(上海新炬中北团队)

本文来源:“IT那活儿”公众号

文章转载自IT那活儿,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论