• Centos ulimit设置


    1.三处配置 

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

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

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

    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

    2.优先级  

    这里因为系统版本不同,所以优先级不同。这里分为centos6和centos7比较。

    centos6

    第一优先级:最后一次ulimit命令设置

    第二优先级:/etc/security/limits.d/

    第三优先级:/etc/security/limits.conf

    系统优先级:/etc/systemd/system.conf 、user.conf 

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

    centos7(因为系统版本不同,优先级也不相同,这里一定要注意)

    第一优先级:最后一次ulimit命令设置

    第二优先级:/etc/security/limits.conf

    第三优先级:/etc/security/limits.d/

    系统优先级:/etc/systemd/system.conf 、user.conf 

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

    3.系统服务配置举例

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

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

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

    创建如下apache的独立配置文件

    cat /etc/systemd/system/httpd.service.d/limits.conf
    [Service]
    LimitNOFILE=20000 #操作系统级别对每个用户创建的进程数的限制
    LimitNPROC=20000  #每个进程可以打开的文件数的限制

    systemd重读新的配置文件

    systemctl daemon-reload
    systemctl restart httpd

    4.系统配置举例

    虽然优先级不同,但是这里还是使用/etc/security/limits.d/来举例,因为centos版本不同,相对应的文件名也不相同。如果使用centos7系统使用下面的方式,记得把/etc/securit/limits.conf下的配置文件注释掉。(centos7 limits.conf优先级高)

    centos7

    [root@centos7 infra]# cat /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
    * soft nofile 102400
    * hard nofile 102400
    * soft nproc 102400
    * hard nproc 102400
    * soft core unlimited #不是非必要更改这个参数
    * hard core unlimited

    #上面用*表示所有用户,如果指定单个用户,像下面一样把 * 改成用户名就可以了

    root soft nofile 102400
    root hard nofile 102400
    root soft nproc 102400
    root hard nproc 102400
    root soft core unlimited   #不是非必要更改这个参数
    root hard core unlimited

    
    

    centos6

    [root@centos6 infra]# cat /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
    #root       soft    nproc     unlimited
    
    * soft nofile 100001
    * hard nofile 100002
    * soft nproc 100001
    * hard nproc 100002
    * soft core unlimited
    * hard core unlimited

    作者:小家电维修

    相见有时,后会无期。

  • 相关阅读:
    AttributeError: 'DatabaseFeatures' object has no attribute 'autocommits_when_autocommit_is_off'
    mac 添加adb环境变量
    ios环境搭建
    接口自动化测试平台介绍
    听风系统介绍
    nginx启动报错整理
    Mongodb: Sort operation used more than the maximum 33554432 bytes of RAM
    Mac上python 在终端使用pip3安装包报ConnectTimeoutError错误
    crontab -让服务器执行定时任务
    koajs项目之memcached实现session共享
  • 原文地址:https://www.cnblogs.com/lizexiong/p/13219456.html
Copyright © 2020-2023  润新知