centos8平台使用ulimit做系统资源限制

一,ulimit的用途

1,

ulimit 用于shell启动进程所占用的资源,可用于修改系统资源限制

2,

使用ulimit -a 可以查看当前系统的所有限制值

使用ulimit -n <可以同时打开的文件数> 设置用户可以同时打开的最大文件数(max open files)

新装的linux默认只有1024,当作为并发访问量大的服务器时,很容易遇到error: too many open files。

error: too many open files

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: https://github.com/liuhongdi/

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,查看ulimit所属的包

[root@centos8 limits.d]# whereis ulimit
ulimit: /usr/bin/ulimit 

[root@centos8 limits.d]# rpm -qf /usr/bin/ulimit 
bash-4.4.19-10.el8.x86_64

属于bash这个包,默认已经安装

三,查看ulimit的版本和帮助

1,查看版本:

ulimit没有提供显示版本的参数,

但它属于bash包,可以查看bash的版本

[root@centos8 limits.d]# bash --version
GNU bash,版本 4.4.19(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
许可证 GPLv3+: GNU GPL 许可证第三版或者更新版本 <http://gnu.org/licenses/gpl.html>

本软件是自由软件,您可以自由地更改和重新发布。
在法律许可的情况下特此明示,本软件不提供任何担保。

2,查看帮助

[root@centos8 limits.d]# ulimit --help

3,查看手册

[root@centos8 limits.d]# man ulimit

四,ulimit的使用例子

1,列出当前的所有限制

#-a: 所有当前限制都被报告

[root@centos8 ~]# ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 14898
max locked memory       (kbytes, -l) 16384
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
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) 14898
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

各字段的说明:

core file size     -c:设置core文件的最大值.单位:blocks

data seg size      -d:设置数据段的最大值.单位:kbytes

scheduling priority -e: 最高的调度优先级 (`nice')

file size          -f:设置创建文件的最大值.单位:blocks

pending signals    -i : 最多的可以挂起的信号数

max locked memory  -l:设置在内存中锁定进程的最大值.单位:kbytes

max memory size    -m:设置可以使用的常驻内存的最大值.单位:kbytes

open files         -n:设置内核可以同时打开的文件描述符的最大值.单位:n

pipe size          -p:设置管道缓冲区的最大值.单位:kbytes

stack size         -s:设置堆栈的最大值.单位:kbytes

cpu time           -t:设置CPU使用时间的最大上限.单位:seconds

virtual memory     -v:设置虚拟内存的最大值.单位:kbytes

max user processes:    -u   用户最多可开启的程序数目

file locks                      -x :最大的文件锁数量

2,查看/设置用户最多打开的文件描述符数量

#-n: open files 

[root@centos8 limits.d]# ulimit -n
1024
[root@centos8 limits.d]# ulimit -n 65535
[root@centos8 limits.d]# ulimit -n
65535

3,查看/设置用户最多可开启的进程数目

#-u: max user processes

[root@centos8 limits.d]# ulimit -u
14898
[root@centos8 limits.d]# ulimit -u 65535
[root@centos8 limits.d]# ulimit -u
65535

4,其他参数:

#-S指soft 限制 (只给出警告信息,而不是硬限制)

#-H指hard 限制

[root@centos8 limits.d]# ulimit -SHn 65535

说明:此命令等效 ulimit -n 65535

五,使对最大进程数和最大文件数的修改永久生效

1,使用 ulimit -n 65535 可即时修改,但重启后就无效了。

  要设置limits.conf配置文件才可以永久生效

2,修改资源的配置文件

[root@centos8 ~]$ vi /etc/security/limits.conf 

增加内容:

* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535

内容说明:

limits.conf的格式如下:

<domain>   <type>   <item>    <value>

domain: 起作用的范围,

            通配符*表示是针对所有用户的限制

            也可以指定用户,例如:root

type:  取值: soft,hard 和 -

        soft 指的是当前系统生效的设置值(警告)

        hard 表明系统中所能设定的最大值(错误)

        soft 的限制不能比har 限制高,- 表明同时设置了 soft 和 hard 的值。

item:

        nofile - 打开的文件描述符的最大数目

        nproc - 进程最大数量

编辑完成后,重启服务器使生效

六, ulimit的配置对于服务并不起作用,为什么?

因为ulimit和limits.conf的配置只针对登录用户,

而对systemd管理的服务不起作用,

服务的limit要在service文件中单独指定

请参考这一篇:

https://www.cnblogs.com/architectforest/p/12794986.html

七,查看centos版本

[root@centos8 ~]# cat /etc/redhat-release 
CentOS Linux release 8.1.1911 (Core)
原文地址:https://www.cnblogs.com/architectforest/p/12794818.html