Linux限制用户可用硬盘空间

为了防止某个用户占用大量资源导致其他用户无法正常使用,一般会对单个用户可占用资源进行限制。就磁盘限额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

在这里插入图片描述

  • 11
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值