Linux is an operating system managed by multiple users. The disk quota is to prevent a user from neglecting or maliciously occupying the disk space and causing the disk to crash.
- Disk quota limit type
by
对用户/组进行磁盘空间使用量的限制(data block)
对用户/组进行磁盘空间创建文件个数的限制(inode)
To meet the effects of disk quotas
-
Disk quota limit sector
1. Soft limit: low level limit, this limit can be broken, and it will be warned
2. Hard limit: Absolute limit, this limit will not be broken, and no more space can be used after reaching the specified limit
3. Grace days: Allow users to exceed their soft limits within the grace period specified by each file system. Once the grace period expires, the soft limit will be enforced as a hard limit.
注意:磁盘配额是针对分区的
-
Disk quota experiment planning
2.1 Prepare a partition with sufficient space
2.2 Add disk, modify partition table type, partition, format, mount
reference partition steps2.3 Users and groups for creating experiments
Users: laow, laos, laom,
group: old2.4 Disk quota planning
old: Hard: 500M Soft: 450M The number of files is
unlimited laow: Hard: 50M Soft: 40M Hard: 10 Soft: 8
laos: Hard: 250M Soft: 200M The number of files is not limited
laom: Hard: 250M Soft : Unlimited number of 200M files
Start disk quota
-
Disk open conditions
First confirm whether the kernel supports disk quota
[root@xxx ~]# :grep "CONFIG_QUOTA" /boot/config-3.10.0-957.el7.x86_64
CONFIG_QUOTA=y
CONFIG_QUOTA_NETLINK_INTERFACE=y
# CONFIG_QUOTA_DEBUG is not set
CONFIG_QUOTA_TREE=y
CONFIG_QUOTACTL=y
CONFIG_QUOTACTL_COMPAT=y #表示支持状态
Check if the quota command is installed
[root@xxx ~]# :rpm -q quota
quota-4.01-19.el7.x86_64 #这里会显示版本
-
Check whether the partition quota function is enabled
Check whether the result has the two attributes of usrquota and grpquota
[root@xxx ~]# :mount | grep /dev/sdc1
/dev/sdc1 on /disk/guazai4 type xfs (rw,relatime,seclabel,attr2,inode64,usrquota,grpquota)
如果没有,那么我们需要去/etc/fstab中配置
If it has been mounted in fstab, our mount -a is useless. In
this case, we can use mount -ao remount to update the mount
或者输出重定向也可以
[root@xxx ~]# echo "/dev/sdb1 /movie xfs defaults,usrquota,grpquota 0 0" >> /etc/fstab
[root@localhost ~]# mount -a
mount -a 是在我们没有在fatab中做任何挂载,通过echo来实现挂载,我们在mount -a就可以
After the configuration is complete, we again mount | grep / dev / sdc1 to check.
-
Create users and user groups
User: laow, laos, laom
Group: old
Set a password by the way
[root@xxx /boot]# :groupadd old
You have new mail in /var/spool/mail/root
[root@xxx /boot]# :useradd -g old laow
[root@xxx /boot]# :useradd -g old laos
[root@xxx /boot]# :useradd -g old laom
You have new mail in /var/spool/mail/root
[root@xxx /boot]# :echo "123123" | passwd --stdin laow
Changing password for user laow.
passwd: all authentication tokens updated successfully.
You have new mail in /var/spool/mail/root
[root@xxx /boot]# :echo "123123" | passwd --stdin laos
Changing password for user laos.
passwd: all authentication tokens updated successfully.
[root@xxx /boot]# :echo "123123" | passwd --stdin laom
Changing password for user laom.
passwd: all authentication tokens updated successfully.
注意:创建用户时,所有人都指定old为初始组
- Set disk quota information of the user / group
format: xfs_quota -x -c (-u / g ) 'limit bsoft = capacity bhard = capacity isoft = File Number File ihard = the number of users / groups' partition
parameters:
the -X-: Expert mode
- c: Used to specify the follow-up rules as parameters of the quota command (if not specified, the default is to perform interactively)
limit: Command to set the limit
Capacity: bsoft (soft) bhard (hard)
Number of files: isoft (soft) ihard (hard)
[root@xxx /disk/guazai4]# :xfs_quota -x -c 'limit bsoft=40m bhard=50m isoft=8 ihard=10 laow' /dev/sdc1
[root@xxx /disk/guazai4]# :xfs_quota -x -c 'limit bsoft=200m bhard=250m isoft=0 ihard=0 laos' /disk/guazai4
[root@xxx /disk/guazai4]# :xfs_quota -x -c 'limit bsoft=200m bhard=250m isoft=0 ihard=0 laom' /disk/guazai4
[root@xxx /disk/guazai4]# :xfs_quota -x -c 'limit -g bsoft=450m bhard=500m isoft=0 ihard=0 old' /disk/guazai4
- Start disk quota
Startup format: quotaon option partition name
-u 启动用户的磁盘配额
-g 启动组的磁盘配额
-v 显示启动过程信息
[root@xxx /boot]# :quotaon -ugv /dev/sdc1
quotaon: Enforcing group quota already on /dev/sdc1
quotaon: Enforcing user quota already on /dev/sdc1
Close: quotaoff option partition name
parameter is the same
[root@xxx /boot]# :quotaoff -ugv /dev/sdc1
Disabling group quota enforcement on /dev/sdc1
/dev/sdc1: group quotas turned off
Disabling user quota enforcement on /dev/sdc1
/dev/sdc1: user quotas turned off
You have new mail in /var/spool/mail/root
[root@xxx /boot]# :quotaon -ugv /dev/sdc1
Enabling group quota enforcement on /dev/sdc1 #再次启动,提示会变
/dev/sdc1: group quotas turned on
Enabling user quota enforcement on /dev/sdc1
/dev/sdc1: user quotas turned on
- View disk quota information
Query user or group quota: quota option user or group name
-u 用户名
-g 组名
-v 显示详细信息
-s 以常见单位显示大小
常见组合:-uvs -gvs
拿laow举例
[root@xxx /boot]# :quota -uvs laow #显示laow的磁盘配额详细信息
Disk quotas for user laow (uid 1019):
Filesystem space quota limit grace files quota limit grace
/dev/sdc1 0K 40960K 51200K 0 8 10
You can also check the disk quota by querying the specified partition
查询指定分区的磁盘配额:repquota 选项 分区名
-u 查询用户配额
-g 查询组配额
-v 显示详情
-s 以常见单位显示
可以同时查看用户和组 组合: -ugvs
[root@xxx ~]# :repquota -ugvs /dev/sdc1
*** Report for user quotas on device /dev/sdc1
Block grace time: 7days; Inode grace time: 7days
Space limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 0K 0K 0K 3 0 0
laow -+ 0K 40960K 51200K 10 8 10 6days
laos +- 250M 200M 250M 6days 1 0 0
laom -- 200M 200M 250M 1 0 0
*** Status for user quotas on device /dev/sdc1
Accounting: ON; Enforcement: ON
Inode: #67 (3 blocks, 2 extents)
*** Report for group quotas on device /dev/sdc1
Block grace time: 7days; Inode grace time: 7days
Space limits File limits
Group used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 0K 0K 0K 3 0 0
old -- 450M 450M 500M 12 0 0
*** Status for group quotas on device /dev/sdc1
Accounting: ON; Enforcement: ON
Inode: #68 (2 blocks, 2 extents)
Here our disk quota information has been completed and can be verified
切换普通用户,进行文件创建,测试容量大小以及文件数是否被限制
注意:我们的普通用户一定要对挂载目录有权限
chmod 777 目录
- Test capacity
Via dd command
格式:dd if=数据来源 of=数据目标 bs=块大小 count=块数量
if=文件名:输入文件名,缺省为标准输入。即指定源文件。< if=input file >
of=文件名:输出文件名,缺省为标准输出。即指定目的文件。< of=output file >
bs=bytes:同时设置读入/输出的块大小为bytes个字节(可自定义单位)。
count=blocks:拷贝块的数量,块大小等于bs指定的大小。(多少个)
1.1 User limit test
laow用户这里磁盘配额为50M.我们使用laiw用户输入一个60M的文件
[laow@xxx ~]$ :dd if=/dev/zero of=/disk/guazai4/laow.txt bs=1M count=60
dd: error writing ‘/disk/guazai4/laow.txt’: Disk quota exceeded
51+0 records in #被限额了。只能输入50M
50+0 records out
52428800 bytes (52 MB) copied, 0.0424156 s, 1.2 GB/s
1.2 Group limit test
因为实验规划
laow: 硬:50M 软:40M
laos: 硬:250M 软:200M
laom: 硬:250M 软:200M
old: 硬:500M 软:450M
三个用户相加的硬限额大于所属组硬限额的大小
看一下测试效果
[laos@xxx ~]$ :dd if=/dev/zero of=/disk/guazai4/laos.txt bs=1M count=250
250+0 records in
250+0 records out
262144000 bytes (262 MB) copied, 4.03898 s, 64.9 MB/s
[laos@xxx ~]$ :ll -h /disk/guazai4/
total 300M
-rw-r--r--. 1 laos old 250M Apr 22 00:03 laos.txt
-rw-r--r--. 1 laow old 50M Apr 21 23:48 laow.txt
[laos@xxx ~]$ :su - laom
Password:
[laom@xxx ~]$ :dd if=/dev/zero of=/disk/guazai4/laom.txt bs=1M count=250
dd: error writing ‘/disk/guazai4/laom.txt’: Disk quota exceeded
201+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 4.28356 s, 49.0 MB/s
[laom@xxx ~]$ :ll -h /disk/guazai4/
total 500M
-rw-r--r--. 1 laom old 200M Apr 22 00:04 laom.txt
-rw-r--r--. 1 laos old 250M Apr 22 00:03 laos.txt
-rw-r--r--. 1 laow old 50M Apr 21 23:48 laow.txt
我们最后配置的laom用户的配额受到了限额(给了250M,只能输入200M),优先被组所限额
-
Number of test files
我们删除刚才输入给laow用户的文件 使用laow用户在挂载文件中,创建20个子文件,看一下效果
[laow@xxx /disk/guazai4]$ :rm -rf laow.txt
[laow@xxx /disk/guazai4]$ :touch {0..20}.txt
touch: cannot touch ‘10.txt’: Disk quota exceeded
touch: cannot touch ‘11.txt’: Disk quota exceeded
touch: cannot touch ‘12.txt’: Disk quota exceeded
touch: cannot touch ‘13.txt’: Disk quota exceeded
touch: cannot touch ‘14.txt’: Disk quota exceeded
touch: cannot touch ‘15.txt’: Disk quota exceeded
touch: cannot touch ‘16.txt’: Disk quota exceeded
touch: cannot touch ‘17.txt’: Disk quota exceeded
touch: cannot touch ‘18.txt’: Disk quota exceeded
touch: cannot touch ‘19.txt’: Disk quota exceeded
touch: cannot touch ‘20.txt’: Disk quota exceeded
[laow@xxx /disk/guazai4]$ :ll #因为实验规格化: laow:硬:10个 软:8个
total 460800 #我们创建了20个就会被磁盘限额,限制数量
-rw-r--r--. 1 laow old 0 Apr 22 00:08 0.txt
-rw-r--r--. 1 laow old 0 Apr 22 00:08 1.txt
-rw-r--r--. 1 laow old 0 Apr 22 00:08 2.txt
-rw-r--r--. 1 laow old 0 Apr 22 00:08 3.txt
-rw-r--r--. 1 laow old 0 Apr 22 00:08 4.txt
-rw-r--r--. 1 laow old 0 Apr 22 00:08 5.txt
-rw-r--r--. 1 laow old 0 Apr 22 00:08 6.txt
-rw-r--r--. 1 laow old 0 Apr 22 00:08 7.txt
-rw-r--r--. 1 laow old 0 Apr 22 00:08 8.txt
-rw-r--r--. 1 laow old 0 Apr 22 00:08 9.txt
-rw-r--r--. 1 laom old 209715200 Apr 22 00:04 laom.txt
-rw-r--r--. 1 laos old 262144000 Apr 22 00:03 laos.txt
因为实验规格化: laow:硬:10个 软:8个
我们创建了20个就会被磁盘限额,限制数量
Supplement: modify the disk quota
Command: edquota option user / group
-u:修改用户的限额配置
-g:修改组的限额配置
[root@xxx /]# :edquota -u laow
Disk quotas for user laow (uid 1019):
Filesystem blocks soft hard inodes soft hard
/dev/sdc1 0 40960 51200 10 8 10
直接修改对应的容量或文件数即可