一、概述

 在从 CentOS 6 迁移到 CentOS 7 的过程中,可能有一些地方需要调整,最显著的地方莫过于 systemd 带来的改变,不同的管理服务的方式,不同的日志方式,设置时区,时间等等。 当然,除了这些显而易见的改变之外,有些变化并不是那么引人注目,例如这里要介绍的你可能发现曾经配置了正确的 /etc/security/limits.conf 在 CentOS 7 中却没有生效了。

  如果遇到上面的问题,很可能已经导致了你的应用异常了,例如:以前配置了服务的 nproc 为一个非常大的数值, 结果现在发现服务不能打开更多的文件。

  惊讶之余,查看服务的 /proc/<pid>/limits 却发现:

 Max open files                                            files

  那么,为什么服务使用 systemd 启动就没有生效呢?

  根据 redhat bugzilla 754285, systemd 实际上是会忽略 /etc/security/limits.conf,下面是 systemd 2 号人物的回答:

  而这个 bug 也标记为已解决,在 pam 包中解决,这个包包含了 /etc/security/limits.conf, 在这个文件中加入了如下注释:

# /etc/security/limits.conf
#
#This file sets the resource limits for the users logged in via PAM.
#It does not affect resource limits of the system services.
#

注意,这里的 system services 指的是 system wide service,对于 CentOS 7 来说也就是系统的 service。

  systemd 还可以对普通用户启动,使用 --user 参数。

二、解决之道

 在CentOS 7 / RHEL 7的系统中,使用Systemd替代了之前的SysV,因此 /etc/security/limits.conf 文件的配置作用域缩小了一些。limits.conf这里的配置,只适用于通过PAM认证登录用户的资源限制,它对systemd的service的资源限制不生效。登录用户的限制,与上面讲的一样,通过 /etc/security/limits.conf 和 limits.d 来配置即可。

  对于systemd service的资源限制,如何配置呢?

全局的配置,放在文件 /etc/systemd/system.conf 和 /etc/systemd/user.conf。 同时,也会加载两个对应的目录中的所有.conf文件 /etc/systemd/system.conf.d/*.conf 和 /etc/systemd/user.conf.d/*.conf
其中,system.conf 是系统实例使用的,user.conf用户实例使用的。一般的sevice,使用system.conf中的配置即可。systemd.conf.d/*.conf中配置会覆盖system.conf。

 DefaultLimitCORE=infinity
DefaultLimitNOFILE=
DefaultLimitNPROC=

  然后运行如下命令,才能生效。

 sudo systemctl daemon-reload
sudo systemctl restart nginx.service

查看一个进程的limit设置:cat /proc/YOUR-PID/limits
例如我的一个nginx service的配置效果:

 $cat /proc/$(cat /var/run/nginx.pid)/limits
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size unlimited bytes
Max core file size unlimited unlimited bytes
Max resident set unlimited unlimited bytes
Max processes processes
Max open files files
Max locked memory bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals signals
Max msgqueue size bytes
Max nice priority
Max realtime priority
Max realtime timeout unlimited unlimited us

  顺便提一下,我还被CentOS7自带的/etc/security/limits.d/20-nproc.conf文件坑过,里面默认设置了非root用户的最大进程数为4096,难怪我上次在limits.conf中设置了没啥效果,原来被limit.d目录中的配置覆盖了。

CentOS 7 systemd的坑的更多相关文章

  1. asp.net core 发布centos 7 遇到的坑

    只是简单记录 .net core 在linux 的安装部署步骤,大神可以忽略 虚拟机:VMware Workstation Pro Linux 版本:http://mirrors.aliyun.com ...

  2. centos部署yapi爬坑记

    前言 这几天终于完成了为期三个月的公司某个demo版的项目,在这期间和公司的后台因为API的事怼过无数次了,'我的接口没问题,是你请求的方式不对吧!'.'一定是你请求的参数不对'......诸如此类问 ...

  3. CentOS 7 systemd添加自定义系统服务

    systemd: CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,即:/usr/lib/systemd/syste ...

  4. 安装CentOS 7 遇到的坑

    1,U盘安装 1.1  用最新版UltraISO刻录到U盘 坑1,如果之前不是用UltraISO刻录,或者不是最新版,u盘的label就不是系统自带的,当你选择install centos 7然后ta ...

  5. Docker创建Centos踩出来的坑

    屁话不多说,先来一遍正常的流程 1.下载centos镜像 # docker pull centos 2.运行容器,修改镜像 2.1 运行 # docker run -itd --name centos ...

  6. CentOS安装时小坑记录

    在安装CentOS的时候,由于第一次安装小白,将VM虚拟机的内存设置为512M,导致进行安装的时候无法进入正常的画面安装模式,只能使用简版安装界面,可能对于很多小白不是很熟悉,特此记录,安装CentO ...

  7. CentOS 7 systemd service开机启动设定

    #vi /etc/systemd/system/xxx.service [Unit] Description=startup script test [Service] Type=simple Exe ...

  8. 【Centos】systemd入门教程

    systemd使用教程 常用指令 运行一个服务: systemctl start <服务名> 关闭一个服务: systemctl stop <服务名> 重启一个服务: syst ...

  9. django项目部署到centos,踩的坑

    FAQ1:在使用pip3安装库的时候,提示需要升级pip pip3 install --upgrade pip FAQ2:在创建软链接时,提示:ln: failed to create symboli ...

随机推荐

  1. Port already be taken

    我运行同一个docker run命令两次后,第二次给出提示,说端口已经被占用. Port has already been allocated [解决方法] 运行docker container ls ...

  2. ExtMail telnet 25端口号 不通

    搭建好的Mail服务器在本地端口号25是开的,但是在别的电脑上就连不上. 修改/etc/postfix/main.cf文件,将 inet_interfaces = localhost 注释掉即可.

  3. maven 打包以及上传

    插件 ------------------------------------------------------------------------------------------------- ...

  4. ACCESS和MSSQL-如何随机读取数据库记录

    查询语句只要这样写,就可以随机取出记录了 SQL="Select top 6 * from Dv_bbs1 where isbest = 1 and layer = 1 order by n ...

  5. CheeseZH: Stanford University: Machine Learning Ex5:Regularized Linear Regression and Bias v.s. Variance

    源码:https://github.com/cheesezhe/Coursera-Machine-Learning-Exercise/tree/master/ex5 Introduction: In ...

  6. PD 之 连接数据库并导出数据及生成PDM文件

    使用PowerDesigner工具,连接数据库并导出数据及生成PDM文件. 1.建立连接 “以管理员身份运行”打开PowerDesigner,右键“Workspace”→“New”→“Physical ...

  7. [转]Spring MVC之@RequestMapping 详解

    前段时间项目中用到了REST风格来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加任何注解),查看了提交方式为application/json, ...

  8. ES6学习笔记二:各种扩展

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7242967.html 一:字符串扩展 1:字符串遍历器 for (let char of str) { // ...

  9. Java多线程之线程阻塞原语LockSupport的使用

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6558597.html  看名字就知道了,LockSupport——提供对加锁机制的支持. 它是提供线程阻塞的原 ...

  10. 在 Vim 中使用 pydiction 对 Python 进行代码补全

    Pydiction 允许你在 Vim 中实现 TAB 代码补全, 可以补全的内容包括:标准的.自定义的,以及第三方模块和包.外加关键字.BIFs,和字符串. Pydiction 由 3 个主要文件构成 ...