Linux 用户磁盘配额、资源限制与监控审计实战
一、磁盘配额(Quota)管理基础与实战
磁盘配额是防止单个用户或组过度占用存储空间的核心机制,支持用户级和组级限制。
1.1 启用文件系统 Quota 支持
多数 ext4/xfs 文件系统默认支持。
# 1. 检查与安装工具
yum install -y quota quota-tools # RHEL/CentOS/Rocky
apt install -y quota # Ubuntu/Debian
# 2. 修改 /etc/fstab 挂载选项
vi /etc/fstab
# 示例(根分区或数据分区)
/dev/sda1 /data ext4 defaults,usrquota,grpquota 0 2
重新挂载:
mount -o remount /data
1.2 初始化 Quota 数据库
quotacheck -cugm /data # -c 创建, -u 用户, -g 组, -m 不 remount
quotaon -vug /data # 启用
1.3 设置配额(edquota / setquota)
# 编辑用户配额
edquota -u username
# 典型模板(块以 KB 为单位,inode 为文件数)
Filesystem blocks soft hard inodes soft hard
/data 0 5242880 10485760 0 50000 100000
# soft:警告阈值,hard:硬限制
组配额:
edquota -g developers
非交互式设置(推荐脚本化):
setquota -u username 0 5G 10G 0 0 0 /data # 5G soft, 10G hard
setquota -g developers 0 20G 50G 0 0 0 /data
1.4 查看与报告
quota -u username # 用户视角
repquota -a # 全系统报告
repquota -ugs /data # 详细
自动化报告脚本:
#!/bin/bash
# quota_report.sh
repquota -a > /var/log/quota_report_$(date +%Y%m%d).log
OVERUSE=$(repquota -a | grep -E '\+[0-9]')
if [ -n "$OVERUSE" ]; then
echo"警告:以下用户超过配额" | mail -s "Quota Alert" ops@company.com
fi
cron 定时:
0 9 * * * /path/to/quota_report.sh
1.5 生产场景
XFS 项目配额(更高级):
xfs_quota -x -c 'limit -u bsoft=5g bhard=10g username' /data
二、ulimit 进程资源限制
ulimit 是 Shell 级资源限制,控制单个进程/会话的打开文件数、CPU 时间、内存等。
2.1 查看与设置
ulimit -a # 当前所有限制
ulimit -n 4096 # 打开文件数(临时)
ulimit -u 2048 # 最大用户进程数
ulimit -v 2097152 # 虚拟内存(KB)
2.2 永久配置(PAM 集成)
编辑 /etc/security/limits.conf:
# 用户或组限制
* soft nofile 4096
* hard nofile 8192
devops soft nproc 2048
devops hard nproc 4096
@developers soft as 4194304 # 地址空间 4GB
PAM 确保生效(/etc/pam.d/common-session 或 system-auth):
session required pam_limits.so
容器环境注意:Docker/Podman 默认继承主机 limits,可通过 --ulimit 覆盖。
三、cgroups 高级资源控制(Systemd + cgroup v2)
现代 Linux(RHEL 8+、Ubuntu 20.04+)推荐使用 systemd slice 实现精细控制。
3.1 Systemd 用户 Slice
# 为特定用户创建 slice
systemctl set-property user-$(id -u username).slice MemoryMax=4G
systemctl set-property user-$(id -u username).slice CPUQuota=200% # 2 核
3.2 创建自定义 Slice
# /etc/systemd/system/devops.slice
[Slice]
MemoryHigh=6G
MemoryMax=8G
CPUQuota=400%
IOWeight=500
systemctl daemon-reload
systemctl start devops.slice
将用户服务绑定到 slice:
systemctl set-property user-$(id -u devops).slice Delegate=yes
cgroup v2 工具(systemd-cgtop、cgexec):
systemd-cgtop
四、用户行为监控与审计体系
4.1 基础审计工具
# 登录监控
last, lastlog, lastb
w, who
# 进程监控
ps -u username
top -u username
htop
4.2 auditd 内核级审计(强烈推荐)
yum install -y audit audit-libs
systemctl enable --now auditd
关键规则(/etc/audit/rules.d/audit.rules):
# 用户相关
-w /etc/passwd -p wa -k user-modify
-w /etc/shadow -p wa -k passwd-modify
-w /etc/sudoers -p wa -k sudoers
-a always,exit -F arch=b64 -S execve -F uid>=1000 -k user-commands
# 敏感目录
-a always,exit -F dir=/home -F perm=wa -F uid>=1000 -k home-access
实时查看:
ausearch -m user_cmd -ts recent
aureport -u
4.3 集成 ELK / Loki 集中日志
- rsyslog / journald 转发到 Logstash 或 Promtail
- 仪表盘:异常登录、sudo 使用、quota 超限、进程杀掉等
Prometheus + Node Exporter + cAdvisor 监控资源:
- Alertmanager 告警:quota 超 80%、CPU 持续高
五、自动化配额与监控脚本集成
综合管理脚本示例(quota + limits + audit):
#!/bin/bash
# user_resource_control.sh
USERNAME=$1
QUOTA_GB=10
SLICE_MEM=4G
# Quota
setquota -u "$USERNAME" 0 $((QUOTA_GB*1024*1024)) $((QUOTA_GB*2*1024*1024)) 0 0 0 /home
# limits.conf 追加
cat >> /etc/security/limits.conf << EOF
$USERNAME soft nofile 8192
$USERNAME hard nofile 16384
$USERNAME soft nproc 2048
EOF
# systemd slice
systemctl set-property user-$(id -u "$USERNAME").slice MemoryMax=${SLICE_MEM}
# 添加 audit 规则(动态)
echo"-a always,exit -F arch=b64 -S execve -F uid=$(id -u $USERNAME) -k ${USERNAME}-cmds" >> /etc/audit/rules.d/custom.rules
auditctl -R /etc/audit/rules.d/custom.rules
监控告警脚本(结合 cron + mail / 企业微信 / Prometheus)。
六、生产案例与故障排查
案例1:开发环境磁盘爆满
- 启用 Quota + 定期 repquota 报告 + 自动清理 tmp 文件。
案例2:失控进程导致 OOM
- ulimit + cgroup MemoryMax + systemd-oomd。
案例3:审计发现异常 sudo
常见故障:
- Quota 不生效:检查 fstab remount、quotacheck、quotaon。
- auditd 性能影响:合理规则,避免过度 -a always。
- cgroup v1 vs v2 兼容:
systemd.unified_cgroup_hierarchy=1 内核参数。
排查命令:
quotaon -p /data
cat /proc/$(pgrep -u username)/limits
systemd-cgls
ausearch -k user-modify
七、安全最佳实践与合规
- 分层限制:ulimit(会话) + cgroup(进程树) + Quota(存储)。
- Just-In-Time 资源:临时提升配额,事后回收。
- 监控可视化:Grafana 仪表盘展示 per-user 资源使用。
- 异常自动响应:高占用进程自动 kill + 告警。
- 合规要求:资源隔离、操作审计、容量规划均满足等保三级以上。
- 容器/K8s 对标:ResourceQuota、LimitRange(下一篇文章重点)。
推荐工具链:
- quota + systemd + auditd + Prometheus + Grafana + ELK/ Loki