添加 linux基础/linux限制单用户的cpu与内存使用率.md
This commit is contained in:
parent
31b14adc4a
commit
753ec99307
1 changed files with 37 additions and 0 deletions
37
linux基础/linux限制单用户的cpu与内存使用率.md
Normal file
37
linux基础/linux限制单用户的cpu与内存使用率.md
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
在Linux中,可以使用cgroups来限制单用户的CPU和内存使用率。以下是一个简单的例子,演示如何为用户username设置CPU和内存的使用限制。
|
||||||
|
|
||||||
|
首先,确保你的系统已经安装了cgroups和pam_cgroup模块。
|
||||||
|
|
||||||
|
创建cgroup子系统目录:
|
||||||
|
```
|
||||||
|
sudo mkdir /sys/fs/cgroup/cpu,memory/username
|
||||||
|
```
|
||||||
|
|
||||||
|
编辑/etc/cgconfig.conf文件,添加以下内容来确保在系统重启后自动创建cgroup:
|
||||||
|
```text
|
||||||
|
group username {
|
||||||
|
cpu {
|
||||||
|
cpu.cfs_period_us = "100000";
|
||||||
|
cpu.cfs_quota_us = "50000";
|
||||||
|
}
|
||||||
|
memory {
|
||||||
|
memory.limit_in_bytes = "256M";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
设置用户的session自动挂载cgroup:
|
||||||
|
编辑/etc/pam.d/common-session,添加以下行:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
session required pam_cgroup.so
|
||||||
|
```
|
||||||
|
设置用户的CPU和内存使用限制:
|
||||||
|
```bash
|
||||||
|
sudo cgset -r cpu.cfs_period_us=100000 -r cpu.cfs_quota_us=50000 -r memory.limit_in_bytes=268435456 /username
|
||||||
|
```
|
||||||
|
|
||||||
|
重新登录或重启以应用这些设置。
|
||||||
|
请注意,这些设置将在用户下次登录时生效。你可以根据需要调整cpu.cfs_quota_us(CPU使用限额,单位为微秒)和memory.limit_in_bytes(内存使用限额,单位为字节)的值。
|
||||||
|
|
||||||
|
这个例子中的设置将用户username的CPU使用限制为50%的可用CPU时间,并将内存使用限制为256MB。
|
Loading…
Add table
Add a link
Reference in a new issue