Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1729600
  • 博文数量: 150
  • 博客积分: 660
  • 博客等级: 上士
  • 技术积分: 2480
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 11:39
文章分类

全部博文(150)

文章存档

2019年(4)

2018年(36)

2017年(53)

2016年(7)

2015年(3)

2014年(3)

2013年(27)

2012年(2)

2011年(1)

2006年(1)

2005年(13)

分类: LINUX

2017-04-21 19:07:36

深入了解centos7系统下打开文件数等资源限制

前言

centos7发布以后,关于open files的设置有所变化,最近运维中恰好遇到的打开文件数限制的问题,遂通过互联网进行了学习和了解,并将学习结果分享出来,希望能帮到其他同学,如果文中有疏漏和错误,欢迎大家批评指正。

三处配置

1、系统编译时默认设置文件(centos7新增)

服务配置 /etc/systemd/system.conf

用户配置 /etc/systemd/user.conf

#DefaultLimitNOFILE= 

2、PAM模块配置文件

  • /etc/security/limits.conf 
EXAMPLES
       These are some example lines which might be specified in /etc/security/limits.conf.

           *               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
第一列用户和组名:*表示通配所有用户;@表示组名;:表示uid或者gid;
第二列资源值类型:hard的值只能由root用户增加,其root权限用户和进程可以减少;soft的值,非root用户和进程可以修改;
第三列是资源类型:nofile为打开文件数,nproc为运行进程数,maxlogins为登录次数cpu、ram等等
第四列是资源数值:大于0的数值
  • 非root用户的默认最大进程数配置文件/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
root       soft    nproc     unlimited 

3、profile配置文件

  • 系统默认配置文件(ulimit命令设置) /etc/profile 和 /etc/bashrc
  • 用户家目录下的配置文件 bash_profile 和 .bashrc
  • 用户shell或者服务启动脚本内运行 ulimit -n 10240

*4、验证非root权限用户不能增加hard值(以nofile为例) 如果ulimit运行在非root用户的shell下,只能修改比当前值小,大的会报错

[tomcat@localhost ~]$ ulimit -n
4096
[tomcat@localhost ~]$ ulimit -n 8192
-bash: ulimit: open files: cannot modify limit: Operation not permitted

优先级

  • 第一优先级:最后一次ulimit命令设置
  • 第二优先级:/etc/security/limits.d/
  • 第三优先级:/etc/security/limits.conf
  • 系统优先级:/etc/systemd/system.conf 、user.conf 

    所谓系统优先级,即不受一二三优先级的影响,针对系统服务和用户服务的配置

系统服务配置举例

假设系统内的apache需要设置打开文件为20000

  • 建立apache服务的配置文件的目录

mkdir -p /etc/systemd/system/httpd.service.d/

  • 创建如下apache的独立配置文件
cat /etc/systemd/system/httpd.service.d/limits.conf
[Service]
LimitNOFILE=20000
  • systemd重读新的配置文件

systemctl daemon-reload

  • 重启apache

systemctl restart httpd

参考及致谢

阅读(6554) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~