# cgred cgrulesengd(cgred服务对于的应用)在Redhat 7中,用于按用户管理资源,包含在`libcgroup-tools`工具包中。但是,Redhat 8抛弃了这一工具。 ```bash [root@bogon ~]# rpm -q --changelog libcgroup * Tue Jan 14 2014 Peter Schiffer 0.41-1 - resolves: #966008 updated to 0.41 - removed deprecated cgred service please use Control Group Interface in Systemd instead ``` 有关评论: > Looking at version numbers: 0.41-21.el7 and 0.41-19.el8, it is no wonder that dnf can’t raise huge alarm as it should. While both appear “0.41”, they are entirely different beasts, branched ages ago (in 2013, I guess). # systemd限制用户资源 **注意:本方法不适用于`su`方式切换登录的用户。即,用户使用`GDM`或`ssh`登录时资源使用受此限制。** ## 配置 可以使用`systemd`设置用户的资源配置。例如,假设想限制用户`fairyfar`的CPU使用上限为30%(即单个CPU核的30%),可以按以下步骤配置: Step 1. 获取用户UID ```bash [root@bogon ~]# id fairyfar uid=1002(fairyfar) gid=1002(fairyfar) groups=1002(fairyfar) ``` Step 2. 设置配额 ```bash [root@bogon ~]# systemctl set-property user-1002.slice CPUQuota=30% ``` 以上步骤如果报以下错误: ```bash [root@bogon ~]# systemctl set-property user-1002.slice CPUQuota=30% Failed to set unit properties on user-1002.slice: Unit user-1000.slice is not loaded. ``` 则,先执行以下命令: ```bash [root@bogon ~]# systemctl start user-1002.slice ``` Step 3. 查看配置情况 ```bash [root@bogon ~]# systemctl cat user-1002.slice # /etc/systemd/system/user-1002.slice.d/50-CPUQuota.conf [Slice] CPUQuota=30% ``` Step 4. 让配置立即生效 ```bash [root@bogon ~]# systemctl daemon-reload ``` 可以控制的资源列表详见 [参考文献3](https://www.freedesktop.org/software/systemd/man/latest/systemd.resource-control.html)。请注意:不同参数引入的systemd版本。 ## 删除配置 如果systemd版本大于等于229,则可以使用revert命令: ```bash [root@bogon ~]# systemctl revert user-1002.slice ``` 否则,低版本需要手动删除: ```bash [root@bogon ~]# rm -rf /etc/systemd/system/user-1002.slice.d/ ``` 删除的路径在`systemctl cat`结果中可以找到,当然也可以有选择性地删除部分配置项。 # 参考 1. [Cgrulesengd is cut in rhel8-based systems](https://forums.rockylinux.org/t/cgrulesengd-is-cut-in-rhel8-based-systems/5062) 2. [systemd, per-user cpu and/or memory limits](https://serverfault.com/questions/874274/systemd-per-user-cpu-and-or-memory-limits) 3. [systemd.resource-control](https://www.freedesktop.org/software/systemd/man/latest/systemd.resource-control.html)