centos6系统资源限制

1.限制用户CPU使用资源

/etc/security/limits.conf #主配置文件 /etc/security/limits.d/     #子模块配置文件

新建一个普通用户,并查看默认资源限制shell

[root@reddhat6_155_201 ~]# useradd admin [root@reddhat6_155_201 ~]# ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 30471 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited #内存大小不限制 open files (-n) 65535 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority              (-r) 0 stack size (kbytes, -s) 10240 cpu time (seconds, -t) unlimited #cpu时间不限制 max user processes (-u) 30471
virtual memory          (kbytes, -v) unlimited file locks (-x) unlimited

exit #退出登陆

设置admin用户使用CPU资源vim /etc/security/limits.confvim

admin soft cpu 1 #限制admin连续使用CPU不超过1分钟 最大不超过两分钟  admin hard cpu 2

切换至admin用户,查看可用资源限制bash

[root@reddhat6_155_200 ~]# su - admin [admin@reddhat6_155_200 ~]$ ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 31501 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 65535 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority              (-r) 0 stack size (kbytes, -s) 10240 cpu time (seconds, -t) 60 #已经变为了60秒了 注意此处的单位 max user processes (-u) 31501
virtual memory          (kbytes, -v) unlimited file locks (-x) unlimited

exit #退出当前用户登陆

使用dd命令测试 #超过一分钟后自动中断ide

使用top命令观察dd命令TIME使用时间测试

尝试本身调大CPU时间 #没有权限spa

可是可用调大至hard值的上限,即2分钟  #两分钟能够,超过一秒也不行code

其余用户限制格式:blog

username  soft cpu 1 #指定用户 启动的程序最长只能够使用1分钟的CPU时间,soft警告 hard直接终止程序(重启程序后从新计时) username hard cpu 1 #也能够使用UID UID后面加:表示大于等于 还能够设置组名 格式为 @groupname 例: admin hard cpu 1 #admin用户硬限制CPU使用时间为1分钟 500      soft    cpu        5 #UID为500的用户软限制CPU使用时间为5分钟 500:     hard    cpu        10 #UID大于等于500的用户硬限制CPU使用时间为10分钟 @root hard cpu 20   #root组用户硬限制CPU使用时间为20分钟

2.限制用户内存使用资源

内存限制有两个地方分别是:进程

[root@reddhat6_155_200 ~]# egrep "rss|as" /etc/security/limits.conf # - rss - max resident set size (KB) #对应top命令中 RES 字段 驻留内存 系统实际分配给程序的内存大小 # - as - address space limit (KB)          #对应top目录中 VIRT 字段 虚存地址空间 表示程序但愿要多少内存

******* 须要注意的是当前pam模块不支持RSS如今,只能经过as限制 ******

添加对admin用户内存的限制ip

[root@reddhat6_155_200 ~]# tail -1 /etc/security/limits.conf admin hard as    262144 #对as限制 大小为 262144K 换算为256M

查看admin用户资源限制

[admin@reddhat6_155_200 ~]$ ulimit -a|grep "virtual memory"
virtual memory          (kbytes, -v) 262144 #能够看到已经变为262144K了,默认不限制

3.限制用户进程数

配置普通用户打开进程数为1024

[root@reddhat6_155_200 ~]# tail /etc/security/limits.d/90-nproc.conf # Default limit for number of user's processes to prevent
# accidental fork bombs. # See rhbz #432903 for reasoning. *          soft    nproc     1024 #普通用户限制进程数为1024 root soft nproc unlimited [root@reddhat6_155_200 ~]# su - admin [admin@reddhat6_155_200 ~]$ ulimit -u 1024

使用shell bomb炸弹进行测试

[admin@reddhat6_155_200 ~]$ :(){ :|:& };: #注意{}两边空格 [1] 749 [admin@reddhat6_155_200 ~]$ -bash: fork: retry: 资源暂时不可用 -bash: fork: retry: 资源暂时不可用 -bash: fork: retry: 资源暂时不可用 -bash: fork: retry: 资源暂时不可用
相关文章
相关标签/搜索