Linux配置文件-limits.conf

一 作用

为通过 PAM 登录的用户设置资源限制。

二 配置文件

配置文件路径

  • /etc/security/limits.conf
  • /etc/security/limits.d

其中,/etc/security/limits.d 会覆盖 /etc/security/limits.conf 中的配置。

配置文件格式


#<domain>        <type>  <item>  <value>

domain

限制的范围:user、group以及通配符

#Where:
#<domain> can be:
#        - a user name
#        - a group name, with @group syntax
#        - the wildcard *, for default entry
#        - the wildcard %, can be also used with %group syntax,
#                 for maxlogin limit
#        - NOTE: group and wildcard limits are not applied to root.
#          To apply a limit to the root user, <domain> must be
#          the literal username root.

type

限制资源的类别,soft 的限制不能比 hard 限制高

#<type> can have the two values:
#        - "soft" for enforcing the soft limits
#        - "hard" for enforcing hard limits

item

限制的资源类型

#<item> 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占用时间(分钟)
#        - 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)      # POSIX消息队列使用的最大内存
#        - nice - max nice priority allowed to raise to values: [-20, 19]  # 最大的nice优先级允许提高到值:[-20, 19]
#        - rtprio - max realtime priority   # 最大实时优先级
#        - chroot - change root to directory (Debian-specific)  # 将根目录更改为目录

value

该类型资源对应的具体的值。

设置所有用户 nofile、nprocsoft、hard limit 值均为 204800

三 命令行

help

ulimit: usage: ulimit [-SHacdefilmnpqrstuvx] [limit]

参数:

-   -S Set a soft limit for the given resource.
-   -H Set a hard limit for the given resource.
-   -a All current limits are reported.
-   -c The maximum size of core files created.
-   -d The maximum size of a process’s data segment.
-   -e The maximum scheduling priority (“nice”)
-   -f The maximum size of files created by the shell(default option).
-   -i The maximum number of pending signals.
-   -l The maximum size that can be locked 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 processes available to a single user.
-   -v The maximum amount of virtual memory available to the process.
-   -x The maximum number of file locks.

demo

# 查看配置
ulimit  -a

# 查看最大打开文件数
ulimit  -n

# 临时配置,打开文件的最大数为 `65535`,重启失效
ulimit  -SHn  65535

# 不限制 core file size
ulimit -c unlimited

四 ulimit 生效

临时配置

打开文件的最大数为 65535,重启失效:

ulimit  -SHn  65535

永久配置

  • 写入文件 /etc/security/limits.d/etc/security/limits.conf 配置文件中。
  • 由于是对 SSH session 生效的,也可以写入 /etc/profile
# 配置
echo "ulimit -n 204800" >> /etc/profile
echo "ulimit -c unlimited"  >> /etc/profile
egrep "^ulimit -n 204800" /etc/profile >& /dev/null || echo "ulimit -n 204800" >> /etc/profile
egrep "^ulimit -c unlimited" /etc/profile >& /dev/null || echo "ulimit -c unlimited"  >> /etc/profile

生效:
source /etc/profile

配置命令行:

cp /etc/security/limits.conf /etc/security/limits.conf.raw
sed -i '/^es .*nofile/d' /etc/security/limits.conf
echo "es soft nofile 204800" >> /etc/security/limits.conf
echo "es hard nofile 204800" >> /etc/security/limits.conf

五 限制示例

限制 xiexianbin 用户最大使用 CPU 资源 1 分钟,配置 limits.conf

$ cat /etc/security/limits.conf
xiexianbin hard cpu 1

等同于 ulimit -t 60

  • 可通过cpu压测工具或自己编写脚本进行测试,达到指定时间系统则会发送信号结束进程。

六 其他配置项

  • open file :一个进程可打开的最大文件数

  • max user processes:系统允许创建的最大进程数

查看系统支持的最大进程号:cat /proc/sys/kernel/pid_max

查看系统支持的最大线程数:cat /proc/sys/kernel/threads-max
/sys/fs/cgroup/pids/system.slice/ssh.service/pids.max

查看当前系统打开文件数总量:lsof | wc –l

查看当前进程打开文件数:lsof –p pid |wc –l

查看系统总限制最大打开文件总量:cat /proc/sys/fs/file-max

查看当前系统上进程总量:ps –ef |wc –l

七 报错:

too many open file in this system

fork:Resource temporarily unavailable
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IForFree

整理不易,望多支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值