Linux中限制用户内存、CPU等资源

365封号余额怎么办 admin 2025-10-26 07:52:55

Linux中限制用户内存、CPU等资源

一、临时修改

通过ulimit的方式修改,只在当前的terminal中有效,如果需要永久修改,可以将其添加到bashrc中

ulimit -m xxx

-a 显示目前资源限制的设定。

-H 设定资源的硬性限制,也就是管理员所设下的限制。

-S 设定资源的弹性限制。

-c 设定core文件的最大值,单位为区块。

-d <数据节区大小> 程序数据节区的最大值,单位为KB。

-f <文件大小> shell所能建立的最大文件,单位为区块。

-m <内存大小> 指定可使用内存的上限,单位为KB。

-n <文件数目> 指定同一时间最多可开启的文件数。

-p <缓冲区大小> 指定管道缓冲区的大小,单位512字节。

-s <堆叠大小> 指定堆叠的上限,单位为KB。

-t 指定CPU使用时间的上限,单位为秒。

-u <程序数目> 用户最多可开启的程序数目。

-v <虚拟内存大小> 指定可使用的虚拟内存上限,单位为KB

如果系统中设置了限制,那么ulimit设置的限制必须小于等于该限制

二、永久修改

1、配置

修改/etc/security/limits.conf即可将限制永久生效

该文件为通过PAM登录的用户设置资源限制

它不影响系统服务的资源限制

/etc/security/limits.d下按照字母顺序排列的配置文件会覆盖 /etc/security/limits.conf中的domain相同的的配置

这意味着,例如使用通配符的domain会被子目录中相同的通配符配置所覆盖,但是某一用户的特定配置只能被字母路中用户的配置所覆盖。其实就是某一用户A如果在/etc/security/limits.conf有配置,当/etc/security/limits.d子目录下配置文件也有用户A的配置时,那么A中某些配置会被覆盖。最终取的值是 /etc/security/limits.d 下的配置文件的配置。

# /etc/security/limits.conf

#Each line describes a limit for a user in the form:

#

#Where:

# can be:

# - a user name 一个用户名

# - a group name, with @group syntax 用户组格式为@GROUP_NAME

# - the wildcard *, for default entry 默认配置为*,代表所有用户

# - the wildcard %, can be also used with %group syntax,

# for maxlogin limit

#

# can have the two values:

# - "soft" 指的是当前系统生效的设置值,软限制也可以理解为警告值

# - "hard" 表示系统中所能设定的最大值

# - "-" 表示同时设置了soft和hard的值

# 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 最大打开的文件数(以文件描叙符,file descripter计数)

# - rss - max resident set size (KB) 最大持久设置大小

# - stack - max stack size (KB) 最大栈大小

# - cpu - max CPU time (MIN) 最多CPU占用时间,单位为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] max nice优先级允许提升到值

# - rtprio - max realtime pr iority

#

#

#

#* soft core 0

#* hard rss 10000

#@student hard nproc 20

#@faculty soft nproc 20

#@faculty hard nproc 50

#ftp hard nproc 0

# root soft nproc unlimited

2、注意点

1)通配符 *,不包含root

这里root的取值还是6000,*虽然在root之后配置,但是*不包含root,root只能被root覆盖

root soft nofile 60000

root hard nofile 60000

* soft nofile 70000

* hard nofile 70000

2)soft的值不会大于hard,否则配置无效

root 用户的取值还是60000,因为虽然 root soft nofile 70000 会覆盖我们之前的配置,但是这个配置是不生效的。因为 root soft nofile 70000 配置的值大于root hard nofile 60000 , soft 配置的值不能大于 hard.

root soft nofile 60000

root hard nofile 60000

root soft nofile 70000

3)limits.d中配置覆盖limits.conf

/etc/security/limits.conf中配置会被/etc/security/limits.d/20-nofile.conf中的值取代

# /etc/security/limits.conf

root soft nofile 60000

root hard nofile 60000

* soft nofile 70000

* hard nofile 70000

# /etc/security/limits.d/20-nofile.conf

root soft nofile 65536

root hard nofile 65536

* soft nofile 65540

* hard nofile 65540

4)其它注意事项

配置,只能被特定domain覆盖

/etc/security/limits.d/ 下文件的相同配置可以覆盖 /etc/security/limits.conf

soft和hard需要都进行设置,才能生效

nofile不能设置 unlimited

nofile可以设置的最大值为 1048576(2**20),设置的值大于该数,就会进行登录不了

soft设置的值 一定要小于或等于hard的值

配置完成以后,重新登录就会生效

在使用VNC的场景下,如果在VNC中修改了limits,需要将vnc kill,然后通过ssh远程登录,再重新开启vnc,如果没有ssh登录,limit可能不生效