linux系统中配置磁盘容量配额服务(quota)

linux系统是多用户、多任务的操作系统,但是硬件的资源是固定而且有限的,如果某些用户不断地在linux系统上创建文件或者存放电影,硬盘空间总有一天会被占满。

针对这种情况,root用户就需要使用磁盘容量配额服务来限制某位用户或某个用户组针对特定文件夹可以使用的最大硬盘空间或最大文件个数,一旦达到这个最大值就不再允许继续使用。

可以使用quota命令进行磁盘容量配额管理,从而限制用户的硬盘可用容量或所能创建的最大文件个数。(linux就该这么学p129)

 1、使用mount命令查看配置前服务状态

[root@linuxprobe dev]# mount | grep boot  ## 查看配置前磁盘容量配额服务,/boot目录没有quota配额服务
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)

 2、使用vim编辑器修改 /etc/fstab配置文件

[root@linuxprobe dev]# cat /etc/fstab  ## 查看修改配置前配置文件

#
# /etc/fstab
# Created by anaconda on Thu Oct 15 18:36:35 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1
UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
/dev/sdb1       /mnt/sdb1test   xfs     defaults        0       0
/dev/sdb2       swap    swap    defaults        0       0
[root@linuxprobe dev]# vim /etc/fstab ## 使用vim编辑器进行修改配置文件,加入,quota,见下图
UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults,uquota        1 2
[root@linuxprobe dev]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Thu Oct 15 18:36:35 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1
UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults,uquota        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
/dev/sdb1       /mnt/sdb1test   xfs     defaults        0       0
/dev/sdb2       swap    swap    defaults        0       0

3、 重启系统,然后查看配置后磁盘容量配额服务

[root@linuxprobe /]# reboot  ## 重启
[root@linuxprobe ~]# mount | grep boot ## 查看 /boot目录磁盘配额服务 /dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,usrquota)

4、对/boot目录其他人增加写的权限

[root@linuxprobe /]# ll -d /boot  ## 查看当前权限,其他人只有读和执行的权限
dr-xr-xr-x. 3 root root 4096 Oct 15 18:41 /boot
[root@linuxprobe /]# chmod -Rf o+w /boot ## 对其他人增加写的权限,-R表示递归,f表示强制,为了下一步测试其他用户像/boot目录写入文件
[root@linuxprobe /]# ll -d /boot  ## 查看赋予权限后的权限状态,其他人增加了写的权限
dr-xr-xrwx. 3 root root 4096 Oct 15 18:41 /boot

5、创建一个测试用户

[root@linuxprobe /]# tail -n 3 /etc/passwd  ## /etc/passwd储存用户信息
tcpdump:x:72:72::/:/sbin/nologin
linuxprobe:x:1000:1000:linuxprobe:/home/linuxprobe:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@linuxprobe /]# useradd tom  ## 创建测试用户
[root@linuxprobe /]# tail -n 3 /etc/passwd  ## 查看创建效果,多出了最后一行用户tom的信息
linuxprobe:x:1000:1000:linuxprobe:/home/linuxprobe:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
tom:x:1001:1001::/home/tom:/bin/bash

6、使用xfs_quota命令为tom用户在/boot目录设置磁盘配合

[root@linuxprobe /]# xfs_quota -x -c 'limit bsoft=3m bhard=6m isoft=3 ihard=6 tom' /boot 
## -x表示专家模式,-c表示用以以参数的形式设置要执行的命令,设置了/boot目录针对tom用户可以使用的最大磁盘软限制为3MB,
硬限制为6MB,文件个数的软限制为3个,硬限制为6个。
[root@linuxprobe /]# xfs_quota -x -c report /boot  ## 查看/boot目录的磁盘配额情况
User quota on /boot (/dev/sda1)
                               Blocks
User ID          Used       Soft       Hard    Warn/Grace
---------- --------------------------------------------------
root            95348          0          0     00 [--------]
tom                 0       3072       6144     00 [--------]
[root@linuxprobe /]# su - tom  ## 切换至tom用户
Last login: Sat Oct 24 22:36:39 CST 2020 on pts/1
Last failed login: Sat Oct 24 22:36:58 CST 2020 on pts/1
There was 1 failed login attempt since the last successful login.
[tom@linuxprobe ~]$ whoami
tom
[tom@linuxprobe ~]$ dd if=/dev/zero bs=2M count=1 of=/boot/a.txt  ## 在/boot目录中创建 大小为2MB的文件
1+0 records in
1+0 records out
2097152 bytes (2.1 MB) copied, 0.00122827 s, 1.7 GB/s
[tom@linuxprobe ~]$ dd if=/dev/zero bs=20M count=1 of=/boot/b.txt  ## 在/boot目录中创建大小为20MB的文件,显示超过磁盘配额
dd: error writing ‘/boot/b.txt’: Disk quota exceeded
1+0 records in
0+0 records out
4194304 bytes (4.2 MB) copied, 0.00490335 s, 855 MB/s
[tom@linuxprobe ~]$ rm /boot/{a.txt,b.txt} -f  ## 删除a.txt、b.txt
[tom@linuxprobe ~]$ touch /boot/{a.txt,b.txt}  ## 一次性创建两个文件
[tom@linuxprobe ~]$ rm /boot/{a.txt,b.txt} -f  ## 删除
[tom@linuxprobe ~]$ touch /boot/{a.txt,b.txt,c.txt,d.txt,e.txt,f.txt,g.txt} ## 一次性创建7个文件,显示超过磁盘配额
touch: cannot touch ‘/boot/g.txt’: Disk quota exceeded

7、使用edquota命令修改/boot目录针对tom用户的磁盘配额

[tom@linuxprobe ~]$ su - root  ## 切换至root用户
Password:
Last login: Sat Oct 24 22:32:42 CST 2020 from 192.168.3.4 on pts/2
[root@linuxprobe ~]# xfs_quota -x -c report /boot  ## 查看磁盘配额
User quota on /boot (/dev/sda1)
                               Blocks
User ID          Used       Soft       Hard    Warn/Grace
---------- --------------------------------------------------
root            95348          0          0     00 [--------]
tom                 0       3072       6144     00 [--------]
[root@linuxprobe ~]# edquota -u tom  ## edquota -u user 对用户的磁盘配额进行设置,执行命令后进入编辑模式,修改红色标记内容
Disk quotas for user tom (uid 1001):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sda1                         0       3072      100044          6        3       10
[root@linuxprobe ~]# xfs_quota -x -c report /boot  ## 查看磁盘配额
User quota on /boot (/dev/sda1)
                               Blocks
User ID          Used       Soft       Hard    Warn/Grace
---------- --------------------------------------------------
root            95348          0          0     00 [--------]
tom                 0       3072      100044     00 [--------]
[root@linuxprobe ~]# rm -f /boot/*.txt  ## 删除/boot目录下所有txt文本
[root@linuxprobe ~]# su - tom   ## 切换至tom用户
Last login: Sat Oct 24 22:37:11 CST 2020 on pts/1
[tom@linuxprobe ~]$ dd if=/dev/zero bs=20M count=1 of=/boot/a.txt ## 创建大小为20MB的文件
1+0 records in
1+0 records out
20971520 bytes (21 MB) copied, 0.015644 s, 1.3 GB/s
[tom@linuxprobe ~]$ dd if=/dev/zero bs=200M count=1 of=/boot/a.txt  ## 创建大小为200MB的文件,显示超出磁盘配额
dd: error writing ‘/boot/a.txt’: Disk quota exceeded
1+0 records in
0+0 records out
10285056 bytes (10 MB) copied, 1.86754 s, 5.5 MB/s
[tom@linuxprobe ~]$ rm -f /boot/*.txt
[tom@linuxprobe ~]$ touch /boot/{01..07}.txt  ## 创建7个文件
[tom@linuxprobe ~]$ rm -f /boot/*.txt
[tom@linuxprobe ~]$ touch /boot/{01..12}.txt ## 创建12个文件,因为设置了10个配额,因此两个无法创建
touch: cannot touch ‘/boot/11.txt’: Disk quota exceeded
touch: cannot touch ‘/boot/12.txt’: Disk quota exceeded

 8、在其他目录进行测试

[root@linuxprobe ~]# whoami
root
[root@linuxprobe ~]# cat /etc/fstab  ## 查看配置文件

#
# /etc/fstab
# Created by anaconda on Thu Oct 15 18:36:35 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1
UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults,uquota        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
/dev/sdb1       /mnt/sdb1test   xfs     defaults        0       0
/dev/sdb2       swap    swap    defaults        0       0
[root@linuxprobe ~]# mount | grep /mnt/sdb1test  ## 查看 /mnt/sdb1test 是否有磁盘配额服务
/dev/sdb1 on /mnt/sdb1test type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@linuxprobe ~]# vim /etc/fstab  ##修改配置文件
/dev/sdb1       /mnt/sdb1test   xfs     defaults,uquota 0       0
[root@linuxprobe ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Thu Oct 15 18:36:35 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1
UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults,uquota        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
/dev/sdb1       /mnt/sdb1test   xfs     defaults,uquota 0       0
/dev/sdb2       swap    swap    defaults        0       0

 

9、重启系统

reboot

10、查看/mnt/sdb1test磁盘容量配额

[root@linuxprobe ~]# mount | grep /mnt/sdb1test  ## 已经开启磁盘容量配额服务
/dev/sdb1 on /mnt/sdb1test type xfs (rw,relatime,seclabel,attr2,inode64,usrquota)

 11、使用xfs_quota命令设置/mnt/sdb1test对tom的磁盘配额

[root@linuxprobe ~]# ls /mnt/sdb1test/
[root@linuxprobe ~]# ll -d /mnt/sdb1test/
drwxr-xr-x. 2 root root 6 Oct 24 23:30 /mnt/sdb1test/
[root@linuxprobe ~]# chmod -Rf o+w /mnt/sdb1test/  ## 增加写的权限
[root@linuxprobe ~]# ll -d /mnt/sdb1test/
drwxr-xrwx. 2 root root 6 Oct 24 23:30 /mnt/sdb1test/
[root@linuxprobe ~]# xfs_quota -x -c 'limit bsoft=10m bhard=100m isoft=5 ihard=10 tom' /mnt/sdb1test/  ## 设置最大文件100MB,最多文件10个
[root@linuxprobe ~]# xfs_quota -x -c report /mnt/sdb1test/  ## 查看
User quota on /mnt/sdb1test (/dev/sdb1)
                               Blocks
User ID          Used       Soft       Hard    Warn/Grace
---------- --------------------------------------------------
root                0          0          0     00 [--------]
tom                 0      10240     102400     00 [--------]

 12、测试配额效果

[root@linuxprobe ~]# su - tom
Last login: Sat Oct 24 23:08:13 CST 2020 on pts/1
[tom@linuxprobe ~]$ dd if=/dev/zero bs=5M count=1 of=/mnt/sdb1test/a.txt  ## 5MB
1+0 records in
1+0 records out
5242880 bytes (5.2 MB) copied, 0.0136401 s, 384 MB/s
[tom@linuxprobe ~]$ dd if=/dev/zero bs=50M count=1 of=/mnt/sdb1test/a.txt  ##50MB
1+0 records in
1+0 records out
52428800 bytes (52 MB) copied, 0.156174 s, 336 MB/s
[tom@linuxprobe ~]$ dd if=/dev/zero bs=500M count=1 of=/mnt/sdb1test/a.txt  ## 500MB,超过限额
dd: error writing ‘/mnt/sdb1test/a.txt’: Disk quota exceeded
1+0 records in
0+0 records out
104857600 bytes (105 MB) copied, 1.33644 s, 78.5 MB/s
[tom@linuxprobe ~]$ rm -f /mnt/sdb1test/*
[tom@linuxprobe ~]$ touch /mnt/sdb1test/{01..08}.txt  ## 8个文件
[tom@linuxprobe ~]$ rm -f /mnt/sdb1test/*
[tom@linuxprobe ~]$ touch /mnt/sdb1test/{01..15}.txt  ## 15个文件,5个超过限额
touch: cannot touch ‘/mnt/sdb1test/11.txt’: Disk quota exceeded
touch: cannot touch ‘/mnt/sdb1test/12.txt’: Disk quota exceeded
touch: cannot touch ‘/mnt/sdb1test/13.txt’: Disk quota exceeded
touch: cannot touch ‘/mnt/sdb1test/14.txt’: Disk quota exceeded
touch: cannot touch ‘/mnt/sdb1test/15.txt’: Disk quota exceeded

13、eduqota修改配额

[tom@linuxprobe ~]$ su - root
Password:
Last login: Sat Oct 24 23:27:27 CST 2020 from 192.168.3.4 on pts/0
[root@linuxprobe ~]# edquota -u tom  ## 修改配额,见下图,上限1000MB,20个文件

[root@linuxprobe ~]# xfs_quota -x -c report /mnt/sdb1test/
User quota on /mnt/sdb1test (/dev/sdb1)
Blocks
User ID Used Soft Hard Warn/Grace
---------- --------------------------------------------------
root 0 0 0 00 [--------]
tom 0 10240 1024000 00 [--------]

 

测试修改配额效果:

[root@linuxprobe ~]# su - tom
Last login: Sat Oct 24 23:35:24 CST 2020 on pts/0
[tom@linuxprobe ~]$ rm -f /mnt/sdb1test/*
[tom@linuxprobe ~]$ dd if=/dev/zero bs=500M count=1 of=/mnt/sdb1test/a.txt
1+0 records in
1+0 records out
524288000 bytes (524 MB) copied, 1.5145 s, 346 MB/s
[tom@linuxprobe ~]$ rm -f /mnt/sdb1test/*
[tom@linuxprobe ~]$ dd if=/dev/zero bs=1500M count=1 of=/mnt/sdb1test/a.txt
dd: error writing ‘/mnt/sdb1test/a.txt’: Disk quota exceeded
1+0 records in
0+0 records out
1048576000 bytes (1.0 GB) copied, 1.86438 s, 562 MB/s
[tom@linuxprobe ~]$ rm -f /mnt/sdb1test/*
[tom@linuxprobe ~]$ touch /mnt/sdb1test/{01..15}.txt
[tom@linuxprobe ~]$ rm -f /mnt/sdb1test/*
[tom@linuxprobe ~]$ touch /mnt/sdb1test/{01..25}.txt
touch: cannot touch ‘/mnt/sdb1test/21.txt’: Disk quota exceeded
touch: cannot touch ‘/mnt/sdb1test/22.txt’: Disk quota exceeded
touch: cannot touch ‘/mnt/sdb1test/23.txt’: Disk quota exceeded
touch: cannot touch ‘/mnt/sdb1test/24.txt’: Disk quota exceeded
touch: cannot touch ‘/mnt/sdb1test/25.txt’: Disk quota exceeded
## 没有问题
原文地址:https://www.cnblogs.com/liujiaxin2018/p/13870965.html