Linux采用quota进行磁盘配额限制 – 21运维
通知: .-...

Linux采用quota进行磁盘配额限制

linux 21运维 1009浏览

Linux (centos) quota配置 在网络管理的工作中,由于硬盘的资源是有限的,常常需要为多用户的服务器设定用户的linux磁盘配额。这个功能对公用的多用户服务器(免费的或者收费的)来说,更是非常必要的。Quota就是在RedHatlinux下实现linux磁盘配额的工具。 quota只支持单独的挂载文件系统  不是某一个目录。本应用一般多用于IDC厂商的虚拟主机,其他很少用到。

实验环境:centos7
测试磁盘:/dev/sdb3
测试目录:/test/
依赖系统安装包:quota 没有的话,可以自行yum安装。

1,新增一块磁盘/dev/sdb3,格式化。

虚拟机新添加一块磁盘,一般新磁盘挂载后我们操作办法:分区、格式化、挂载。这里不做演示。

mkfs.ext4 /dev/sdb3
mkdir   /test
mount  /dev/sdb3   /test/

2,启用磁盘配额功能

mount  -o remount,usrquota,grpquota   /test/

通过mount命令单独查看/dev/sdb3 已经可以看到如下信息:
/dev/sdb3 on /test type ext4 (rw,relatime,quota,usrquota,grpquota,data=ordered
写入分区表:

/dev/sdb3               /test                   ext4    defaults,usrquota,grpquota      0 0

现在开始,该/test已经支持配额设置。

3,检查磁盘配额并生成配额文件

[[email protected] ~]# quotacheck   -vugb   /test/
quotacheck: Your kernel probably supports journaled quota but you are not using it. Consider switching to journaled quota to avoid running quotacheck after an unclean shutdown.
quotacheck: Scanning /dev/sdb3 [/test] done
quotacheck: Cannot stat old user quota file /test/aquota.user: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old group quota file /test/aquota.group: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old user quota file /test/aquota.user: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old group quota file /test/aquota.group: No such file or directory. Usage will not be subtracted.
quotacheck: Checked 2 directories and 0 files
quotacheck: Old file not found.
quotacheck: Old file not found.
[[email protected] ~]# ll  -a /test/
total 36
drwxr-xr-x   3 root root  4096 Apr 17 05:22 .
dr-xr-xr-x. 19 root root   267 Apr 17 05:10 ..
-rw-------   1 root root  6144 Apr 17 05:22 aquota.group
-rw-------   1 root root  6144 Apr 17 05:22 aquota.user
drwx------   2 root root 16384 Apr 17 05:10 lost+found

4,用户和组账号的配额设置
使用命令edquota,参数说明:

[[email protected] ~]# useradd  21yunwei  
[[email protected] ~]# edquota  -g 21yunwei
Disk quotas for group 21yunwei (gid 1000):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sdb3                         0         50        80          0        0        0

激活磁盘配额:quotaon命令

[[email protected] ~]# quotaon  -ugv /test/
/dev/sdb3 [/test]: group quotas turned on
/dev/sdb3 [/test]: user quotas turned on

quotaon参数说明:
命令:quotaon
格式:quotaon 参数 文件系统名
功能:激活磁盘配额;各分区的文件系统根目录必须有quota.user和quota.group配置文件
参数:
-u:开启用户的磁盘空间限制
-g:开启用户组的磁盘空间限制
-a:开启在/etc/fstab文件里,有加入quota设置的分区的空间限制
-v:显示

如果关闭配额,则采用quotaoff命令。

切换到其他用户比如21yunwei测试账号进行测试:

[[email protected] 21yunwei]$ dd  if=/dev/zero   of=21yunwei.test   bs=1K count=70  #大于50报警,但依旧可以写入。
sdb3: warning, group block quota exceeded.
70+0 records in
70+0 records out
71680 bytes (72 kB) copied, 0.000341477 s, 210 MB/s
[[email protected] 21yunwei]$ ll  -trhS
total 72K
-rw-rw-r-- 1 21yunwei 21yunwei 70K Apr 17 05:41 21yunwei.test


[[email protected] 21yunwei]$ rm  -f 21yunwei.test 
[[email protected] 21yunwei]$ dd  if=/dev/zero   of=21yunwei.test   bs=1K count=90 #大于80报警,报错,提示配额满,但依旧可以写入,只不过是数据不完整。
sdb3: warning, group block quota exceeded.
sdb3: write failed, group block limit reached.
dd: error writing ‘21yunwei.test’: Disk quota exceeded
81+0 records in
80+0 records out
81920 bytes (82 kB) copied, 0.000555453 s, 147 MB/s
[[email protected] 21yunwei]$ ll  -trh
total 80K
-rw-rw-r-- 1 21yunwei 21yunwei 80K Apr 17 05:42 21yunwei.test

说明:磁盘配额中的blocks,一个block是1K。

本文章实验实现参考文章:http://zhoualine.iteye.com/blog/1613788

转载请注明:21运维 » Linux采用quota进行磁盘配额限制