Linux限制用户可用硬盘空间

2024-01-08 15:47:39

为了防止某个用户占用大量资源导致其他用户无法正常使用,一般会对单个用户可占用资源进行限制。就磁盘限额XFS文件系统原生支持目录级别的限制。ext文件系统不支持目录限制,曲线方式是限制用户的总占用空间。
本文介绍使用quota程序限制用户可用硬盘空间。
Linux磁盘配额(Quota),可以限制用户、用户组的总磁盘占用空间。

安装quota

root@aihuidi:~# apt install -y quota

修改/etc/fstab文件,限制磁盘分区的属性

/dev/disk/by-id/dm-uuid-LVM-wsB2oX99MvDWWYaCVuoQu9aeZIuYQQkzbc2nfyFy3gYUIcyjWaioNEQmTLg0rhHQ / ext4 defaults,usrquota,grpquota 0 1

在这里插入图片描述
重新挂载根目录

mount -o remount

PS:如果这步没成功,可以重启机器

接下来扫描要限制的分区

root@aihuidi:~# quotacheck -ugmv /
quotacheck: Your kernel probably supports ext4 quota feature but you are using external quota files. Please switch your filesystem to use ext4 quota feature as external quota files on ext4 are deprecated.
quotacheck: Scanning /dev/mapper/ubuntu--vg-ubuntu--lv [/] done
quotacheck: Cannot stat old user quota file //aquota.user: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old group quota file //aquota.group: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old user quota file //aquota.user: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old group quota file //aquota.group: No such file or directory. Usage will not be subtracted.
quotacheck: Checked 33875 directories and 244071 files
quotacheck: Old file not found.
quotacheck: Old file not found.
root@aihuidi:~#

在这里插入图片描述
开启磁盘限额

root@aihuidi:~# quotaon -ugv /
quotaon: Your kernel probably supports ext4 quota feature but you are using external quota files. Please switch your filesystem to use ext4 quota feature as external quota files on ext4 are deprecated.
/dev/mapper/ubuntu--vg-ubuntu--lv [/]: group quotas turned on
/dev/mapper/ubuntu--vg-ubuntu--lv [/]: user quotas turned on
root@aihuidi:~#

在这里插入图片描述
编辑用户的限额

root@aihuidi:~# ls /home/
aihuidi
root@aihuidi:~# edquota -u aihuidi

要编辑的是soft和hard的值,以KB为单位。上面我们限制work用户占用超过2G(soft)就发出警告,超过3G(hard)就拒绝写入。inode的值一般无需限制。
在这里插入图片描述
切换aihuidi账号,测试验证

root@aihuidi:~# su aihuidi
aihuidi@aihuidi:/root$ cd
aihuidi@aihuidi:~$ fallocate -l 3G testfile
fallocate: fallocate failed: Disk quota exceeded
aihuidi@aihuidi:~$ fallocate -l 1.8G testfile
aihuidi@aihuidi:~$

3G的创建失败,说明限制起作用了
在这里插入图片描述
关闭限制

root@aihuidi:~# quotaoff -ugv /
quotaoff: Your kernel probably supports ext4 quota feature but you are using external quota files. Please switch your filesystem to use ext4 quota feature as external quota files on ext4 are deprecated.
/dev/mapper/ubuntu--vg-ubuntu--lv [/]: group quotas turned off
/dev/mapper/ubuntu--vg-ubuntu--lv [/]: user quotas turned off
root@aihuidi:~# 
root@aihuidi:~# su aihuidi
aihuidi@aihuidi:/root$ cd
aihuidi@aihuidi:~$ fallocate -l 5G testfile

在这里插入图片描述

文章来源:https://blog.csdn.net/weixin_43822878/article/details/135456427
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。