LINUX中ulimit限制对资源的使用

1. ulimit的功能

它是一种简单并且有效实现资源限制的方式。ulimit用于限制shell启动进程所占用的资源,支持以下各种类型的限制:

  • 所创建的内核文件的大小
  • 进程数据块的大小
  • Shell进程创建文件的大小
  • 内存锁住的大小
  • 常驻内存集的大小
  • 打开文件描述符的数量
  • 分配堆栈的最大大小
  • CPU 时间
  • 单个用户的最大线程数
  • Shell 进程所能使用的最大虚拟内存
  • 限制硬资源和软资源

2. ulimit的语法格式

ulimit [-SHacdefilmnpqrstuvx] [限制]

也可以写成这种形式:

ulimit [-aHS][-c <core文件上限>][-d <数据节区大小>][-f <文件大小>][-m <内存大小>][-n <文件数目>][-p <缓冲区大小>][-s <堆叠大小>][-t <CPU时间>][-u <程序数目>][-v <虚拟内存大小>]

注:参数S表示设置软限制,参数H表示设置硬限制。当都不指定时,表示设置软限制和硬限制均为指定值。

一般我们查看当前所有limit信息:

[root@ecs-2ae6 ~]# 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) 14781
max locked memory       (kbytes, -l) 64
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) 14781
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
[root@ecs-2ae6 ~]# 

上面参数选项的解释:

选项 参数名称 含义
-c core file size 设定core文件的最大值,单位为块,如果指定为0,不会产生core文件
-d data seg size 设定数据段的最大值,单位为KB
-e scheduling priority 进程优先级的限定,这个值对root不起作用
-f file size shell所能建立的最大文件大小,单位为区块
-i pending signals 进程最大挂起/阻塞的信号数量
-l max locked memory 可以锁住的物理内存的最大值,单位:kbytes,这个值只root用户不起作用,锁定内存的操作由mlock()函数提供,避免swap in/swap out
-m max memory size 可以使用的常驻内存的最大值,单位:kbytes
-n open files 指定系统打开的最大文件数,若超出设置的值报erro:too many open files
-p pipe size 设置管道的最大值,单位为block,1block=512bytes
-q POSIX message queues POSIX的消息队列的最大值为819200字节
-r real-time priority 限制程序实时优先级的范围,只针对普通用户,root不起作用
-s stack size 指定线程堆栈的最大值,单位:kbytes
-t cpu time 指定进程使用CPU时间的上限,单位为秒
-u max user processes 用户最多可开启的程序数目
-v virtual memory 指定可使用的虚拟内存上限,单位为KB
-x file locks 所能锁住的文件的最大个数,上面unlimited没有限制

3. 修改linux资源的方法

3.1临时生效

在查看的命令后面加上限制值,就可以调整某一项限制,但只对当前登录shell有效,且是临时生效,下次登录不起作用。

举例如下:

我们先查询当前终端的文件句柄数,默认为1024,然后在查询命令后面加上要修改的值,比如65535,然后再查看其值:

[root@mysrs ~]# ulimit -n
1024
[root@mysrs ~]# ulimit -n 65535
[root@mysrs ~]# ulimit -n
65535
[root@mysrs ~]# 

又如,修改单一用户程序上限:

[root@mysrs ~]# ulimit -u
15023
[root@mysrs ~]# ulimit -u 500
[root@mysrs ~]# ulimit -u
500
[root@mysrs ~]# 
3.2永久生效

这里永久生效指每次登陆shell时,都会按配置重新设定ulimit,以达到永久生效的效果。

需要三步完成

1)修改/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.
#
#Also note that configuration files in /etc/security/limits.d directory,
#which are read in alphabetical order, override the settings in this
#file in case the domain is the same or more specific.
#That means for example that setting a limit for wildcard domain here
#can be overriden with a wildcard setting in a config file in the
#subdirectory, but a user specific setting here can be overriden only
#with a user specific setting in the subdirectory.
#
#Each line describes a limit for a user in the form:
#
#<domain>        <type>  <item>  <value>
#
#Where:
#<domain> can be:
#        - a user name
#        - a group name, with @group syntax
#        - the wildcard *, for default entry
#        - the wildcard %, can be also used with %group syntax,
#                 for maxlogin limit
#
#<type> can have the two values:
#        - "soft" for enforcing the soft limits
#        - "hard" for enforcing hard limits
#
#<item> can be one of the following:
#        - core - limits the core file size (KB)
#        - data - max data size (KB)
#        - fsize - maximum filesize (KB)
#        - memlock - max locked-in-memory address space (KB)
#        - nofile - max number of open file descriptors
#        - rss - max resident set size (KB)
#        - stack - max stack size (KB)
#        - cpu - max CPU time (MIN)
#        - nproc - max number of processes
#        - as - address space limit (KB)
#        - maxlogins - max number of logins for this user
#        - maxsyslogins - max number of logins on the system
#        - priority - the priority to run user process with
#        - locks - max number of file locks the user can hold
#        - sigpending - max number of pending signals
#        - msgqueue - max memory used by POSIX message queues (bytes)
#        - nice - max nice priority allowed to raise to values: [-20, 19]
#        - rtprio - max realtime priority
#
#<domain>      <type>  <item>         <value>
#

#*               soft    core            0
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#@student        -       maxlogins       4

# End of file

该文件中包括四个字段,分别为domain、type、item和value,其含义如下表:

字段 含义
domain 表示限定的对象,可以是用户、用户组(组前名称加@区分用户)、或者*表示所有用户
type soft表示警告的设定,即超过这个值就会产生告警信息 hard表示严格设定,超过这个设定的值会报错
item core:限制内核文件大小 data:最大数据大小 fsize:最大文件大小 memlock:最大锁定内存地址空间 nofile:打开文件的最大数目 rss:最大持久设置大小 stack:最大栈大小 cpu:以分钟为单位的最多CPU时间 noproc:进程的最大数目 as:地址空间限制 maxlogins:此用户的最大登录数量 maxsyslogins:在系统上登录的最大数目 priority:优先级运行用户进程 locks:文件的最大数量锁定用户可容纳 sigpending:最大挂起信号的数量 msgqueue:通过POSIX消息队列使用的最大内存(字节) nice:最大不错优先允许提高到值:[-20,19] rtprio:最大实时优先
value 表示要限制的值

打开文件,将-u和-n的软限制和硬限制同时修改为65535,在其末尾加上要限制的项目:

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

#*               soft    core            0
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#@student        -       maxlogins       4
* hard nproc 65535
* soft nproc 65535
* hard nofile 65535
* soft nofile 65535

2)修改/etc/pam.d/login文件

这里可以先看一下它的默认配置内容:

[root@mysrs ~]# cat /etc/pam.d/login
#%PAM-1.0
auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so
auth       substack     system-auth
auth       include      postlogin
account    required     pam_nologin.so
account    include      system-auth
password   include      system-auth
# pam_selinux.so close should be the first session rule
session    required     pam_selinux.so close
session    required     pam_loginuid.so
session    optional     pam_console.so
# pam_selinux.so open should only be followed by sessions to be executed in the user context
session    required     pam_selinux.so open
session    required     pam_namespace.so
session    optional     pam_keyinit.so force revoke
session    include      system-auth
session    include      postlogin
-session   optional     pam_ck_connector.so

打开/etc/pam.d/login文件,添加如下内容:

session    required      pam_limits.so

image-20230820211141891

它表示,在登录时使用pam管理limit。

3)修改/etc/profile文件

打开该文件,在其最后加上以下两条:

ulimit -u 65535
ulimit -n 65535

这样每次登陆shell后,会初始执行这两条ulimit命令,并使其生效。

为了使刚配的/etc/profile文件最后两行生效,还需执行source命令:

[root@mysrs ~]# source /etc/profile
[root@mysrs ~]# 

注1:这里重新登录也可使其生效。

注2:source使当前shell对指定文件内容生效,日常维护中使用频率高。

posted @ 2023-08-20 21:12  寻梦99  阅读(284)  评论(0)    收藏  举报