控制 shell 程序的资源
1 ulimit [-SHacdefilmnpqrstuvx] [limit ]
配置文件 ulimit 命令,立即生效,但无法保存,永久保存需要修改配置文件:
1 2 3 4 5 6 7 8 /etc/security/limits.conf /etc/security/limits.d/*.conf <domain> <type > <item> <value> * soft nproc 4096 root soft nproc unlimited
配置文件详细信息:man 5 limits.conf
domain
domain
description
username
一个用户
@group
组内所有用户
*
所有用户
%
限制最多有多少用户登录,%对*生效,限制组使用%group
:
@:
%:
type
type
description
hard
软限制,普通用户自己可以修改
soft
硬限制,由 root 用户设定,且通过 kernel 强制生效
-
二者同时限定
item
item
default
description
core
data
fsize
memlock
最大锁定内存地址空间
nofile
1024
所能够同时打开的最大文件数量
rss
stack
cpu
nproc
1024
所能够同时运行的进程的最大数量
as
maxlogins
maxsyslogins
priority
locks
sigpending
msgqueue
POSIX 消息队列所使用的最大内存
nice
rtprio
范例:
1 2 3 4 5 6 7 8 9 10 * soft core 0 * hard nofile 512 @student hard nproc 20 @faculty soft nproc 20 @faculty hard nproc 50 ftp hard nproc 0 @student - maxlogins 4 :123 hard cpu 5000 @500: soft cpu 10000 600:700 hard locks 10
生产案例:
1 2 3 4 5 * - core unlimited * - nproc 1000000 * - nofile 1000000 * - memlock 32000 * - msgqueue 8192000
案例:ulimit 命令修改用户打开的文件个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [root@centos8 ~] 1024 [root@centos8 ~] -bash: ulimit : open files: cannot modify limit : Operation not permitted [root@centos8 ~] [root@centos8 ~] core file size (blocks, -c) unlimited data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 7111 max locked memory (kbytes, -l) 16384 max memory size (kbytes, -m) unlimited open files (-n) 1048576 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 7111 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited [root@centos8 ~] 1048576
案例:限制用户最多打开的文件数和运行进程数,并持久保存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 cat /etc/pam.d/system-authsession required pam_limits.so vim /etc/security/limits.conf apache - nofile 10240 student hard nproc 10 [root@centos8 ~] wang - nofile 66666 wang - nproc 5 mage - nofile 88888 [root@centos8 ~] Last login: Mon May 25 14:40:38 CST 2020 on pts/0 [wang@centos8 ~]$ulimit -n 66666
案例:限制 mage 用户最大的同时登录次数
1 2 3 4 5 6 7 8 [root@centos8 ~] mage - maxlogins 2 [root@centos8 ~] mage tty1 2020-05-25 14:35 root pts/0 2020-05-25 14:35 (10.0.0.1) root pts/3 2020-05-25 14:06 (10.0.0.1) mage tty3 2020-05-25 14:35