Linux 磁盘配额(XFS & EXT4)

2020-09-26 0 333 百度已收录

是在Linux中搭建了FTP服务器,为了安全性,就要考虑磁盘配额,以防服务器磁盘空间恶意占满。


磁盘配额概述#

1.作用范围:只在指定的分区有效。

2.限制对象:主要针对用户、组进行限制,对组账号限制,组内所有用户的使用总和不能超过限制。

3.限制类型:磁盘容量限制(Block),默认单位KB、文件数量限制(Inode)。

4.限制方法:软限制、硬限制。软限制默认7天内允许超过,会有警告。硬限制不允许超过,硬限制应当比软限制大,否者软限制失效。


磁盘配额管理#

XFS 文件系统#

XFS文件系统通过xfs_quota工具进行管理。

查看xfsprogs软件包的安装情况,列表查看xfsprogs软件包安装的xfs_quota配额管理程序

[root@localhost ~]# rpm -q xfsprogs  xfsprogs-4.5.0-12.el7.x86_64  [root@localhost ~]# rpm -ql xfsprogs | grep -i "xfs_quota"  /usr/sbin/xfs_quota  /usr/share/man/man8/xfs_quota.8.gz

xfs_quota 等命令介绍#

  • mount -o usrquota,grpquota /dev/class/stu01 /mnt/stu01

-o usrquota,grpquota:以支持配额的形式挂载

  • xfs_quota -x -c ‘limit -u bsoft=N bhard=N isoft=N ihard=N 用户名’ 挂载点

配置磁盘配额

-x:启动专家模式,在当前模式下允许对配额系统进行修改的所有管理命令可用
-c:直接调用管理命令,

管理命令limit后相关:

-u:对用户限制
-g:对组限制
bsoft:磁盘容量软限制
bhard:磁盘容量硬限制
isoft:文件数量软限制
ihard:文件数量硬限制

  • xfs_quota -x -c ‘report -abi’ 挂载点

查看磁盘配额

管理命令report后相关:

-u:对用户查看
-g:对组查看
-a:查看所有可用分区的配额使用报告
-b:查看磁盘容量
-i:查看文件数

  • dd if=指定输入设备或文件 of=指定输出设备或文件 bs=指定读取block大小 count=指定读取block的数量。

设备转换和复制命令

XFS 实验过程#

1.关闭SELinux,若启用SELinux功能,不是所有的目录都能设定quota,默认quota仅能对/home进行设定。

[root@localhost ~]# setenforce 0

2.为stu01开启磁盘配额功能,并使其永久生效。

[root@localhost ~]# vim /etc/fstab  //修改stu01那行,这里为了方便,直接使用上一篇博客的实验成果。  /dev/class/stu01        /mnt/stu01      xfs     defaults,usrquota,grpquota      0 0

3.查看stu01的磁盘配额是否生效。(未生效)

[root@localhost ~]# mount | grep stu01  /dev/mapper/class-stu01 on /mnt/stu01 type xfs (rw,relatime,seclabel,attr2,inode64,noquota)

4.重新挂载stu01,使磁盘配额生效。

[root@localhost ~]# umount /mnt/stu01/  [root@localhost ~]# mount -a  [root@localhost ~]# df -hT | grep stu01  /dev/mapper/class-stu01 xfs        33G   33M   33G   1% /mnt/stu01

5.查看stu01的磁盘配额是否生效。(已生效)

[root@localhost ~]# mount | grep stu01  /dev/mapper/class-stu01 on /mnt/stu01 type xfs (rw,relatime,seclabel,attr2,inode64,usrquota,grpquota)

6.若是不存在名为zhangsan测试用户,则添加。

[root@localhost ~]# useradd zhangsan && echo "000000" | passwd --stdin zhangsan  Changing password for user zhangsan.  passwd: all authentication tokens updated successfully.

7.设置磁盘配额的数据

[root@localhost ~]# xfs_quota -x -c 'limit -u bsoft=50M bhard=80M isoft=6 ihard=8 zhangsan' /mnt/stu01/

8.为了方便测试,放开权限

[root@localhost ~]# chmod 777 /mnt/stu01/

9.切换到zhangsan用户。

[root@localhost ~]# su - zhangsan

10.文件数量配额测试。

[zhangsan@localhost stu01]$ touch test{1..9}.txt  touch: cannot touch ‘test9.txt’: Disk quota exceeded  [zhangsan@localhost stu01]$ ls  test1.txt  test2.txt  test3.txt  test4.txt  test5.txt  test6.txt  test7.txt  test8.txt  [zhangsan@localhost stu01]$ rm -f *

11.磁盘空间配额测试。

[zhangsan@localhost stu01]$ dd if=/dev/zero of=/mnt/stu01/test1.txt bs=1M count=100  dd: error writing ‘/mnt/stu01/test1.txt’: Disk quota exceeded  81+0 records in  80+0 records out  83886080 bytes (84 MB) copied, 0.0799075 s, 1.0 GB/s  [zhangsan@localhost stu01]$ ls -lh  total 80M  -rw-rw-r--. 1 zhangsan zhangsan 80M Aug 25 02:47 test1.txt  [zhangsan@localhost stu01]$ touch test2.txt  touch: cannot touch ‘test2.txt’: Disk quota exceeded

12.查看配额使用情况。

[root@localhost ~]# xfs_quota -x -c 'report -abi' /mnt/stu01/  User quota on /mnt/stu01 (/dev/mapper/class-stu01)                                 Blocks                                          Inodes  User ID          Used       Soft       Hard    Warn/Grace           Used       Soft       Hard    Warn/ Grace  ---------- -------------------------------------------------- --------------------------------------------------  root                0          0          0     00 [--------]          3          0          0     00 [--------]  zhangsan        81920      51200      81920     00  [6 days]          1          6          8     00 [--------]    Group quota on /mnt/stu01 (/dev/mapper/class-stu01)                                 Blocks                                          Inodes  Group ID         Used       Soft       Hard    Warn/Grace           Used       Soft       Hard    Warn/ Grace  ---------- -------------------------------------------------- --------------------------------------------------  root                0          0          0     00 [--------]          3          0          0     00 [--------]  zhangsan        81920          0          0     00 [--------]          1          0          0     00 [--------]

EXT4 文件系统#

EXT3/4文件系统通过quota工具进行管理。

查看quota软件包的安装情况。

[root@localhost ~]# rpm -q quota  quota-4.01-17.el7.x86_64

quota 相关命令介绍#

  • quotacheck

检测磁盘配额并生成配额文件

-a:检测所有可用的分区
-u:检测用户配额
-g:检测组配额
-c:创建配额数据文件
-v:显示执行过程

quotacheck -ugcv 设备文件名  quotacheck -augcv
  • edquota

编辑用户和组账号的配额设置

-u:修改用户配额,默认KB。
-g:修改组配额,默认KB。
-t:修改宽限时间

edquota -u 用户名  edquota -g 组名  edquota -t
  • quotaon & quotaoff

启动文件系统的磁盘配额功能

-u:用户
-g:组
-v:显示过程

quotaon -ugv 设备文件名或挂载点  quotaoff -ugv 设备文件名或挂载点
  • quota

查看用户、组配额使用情况

-u:用户
-g:组

quota -u 用户名  quota -g 组名
  • repquota

查看分区磁盘配额使用情况

repquota 挂载点

EXT4 实验过程#

1.关闭SELinux,若启用SELinux功能,不是所有的目录都能设定quota,默认quota仅能对/home进行设定。

[root@localhost ~]# setenforce 0

2.为stu02开启磁盘配额功能,并使其永久生效。

[root@localhost ~]# vim /etc/fstab  //修改stu02那行,这里为了方便,直接使用上一篇博客的实验成果。  /dev/class/stu02        /mnt/stu02      ext4    defaults,usrquota,grpquota      0 0

3.查看stu02的磁盘配额是否生效。(未生效)

[root@localhost ~]# mount | grep stu02  /dev/mapper/class-stu02 on /mnt/stu02 type ext4 (rw,relatime,seclabel,data=ordered)

4.重新挂载stu02,使磁盘配额生效。

[root@localhost ~]# umount /mnt/stu02/  [root@localhost ~]# mount -a  [root@localhost ~]# df -hT | grep stu02  /dev/mapper/class-stu02 ext4       23G   44M   22G   1% /mnt/stu02

5.查看stu02的磁盘配额是否生效。(已生效)

[root@localhost ~]# mount | grep stu02  /dev/mapper/class-stu02 on /mnt/stu02 type ext4 (rw,relatime,seclabel,quota,usrquota,grpquota,data=ordered)

6.若是不存在名为zhangsan的测试用户,则添加。

[root@localhost ~]# useradd zhangsan && echo "000000" | passwd --stdin zhangsan  Changing password for user zhangsan.  passwd: all authentication tokens updated successfully.

7.检测磁盘配额并生成配额文件。

[root@localhost ~]# ls /mnt/stu02/  lost+found  [root@localhost ~]# quotacheck -ugcv /dev/class/stu02  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/mapper/class-stu02 [/mnt/stu02] done  quotacheck: Cannot stat old user quota file /mnt/stu02/aquota.user: No such file or directory. Usage will not be subtracted.  quotacheck: Cannot stat old group quota file /mnt/stu02/aquota.group: No such file or directory. Usage will not be subtracted.  quotacheck: Cannot stat old user quota file /mnt/stu02/aquota.user: No such file or directory. Usage will not be subtracted.  quotacheck: Cannot stat old group quota file /mnt/stu02/aquota.group: No such file or directory. Usage will not be subtracted.  quotacheck: Checked 3 directories and 0 files  quotacheck: Old file not found.  quotacheck: Old file not found.  [root@localhost ~]# ls /mnt/stu02/  aquota.group  aquota.user  lost+found

8.编辑磁盘配额设置。

[root@localhost ~]# edquota -u zhangsan  Disk quotas for user zhangsan (uid 1000):    Filesystem                   blocks       soft       hard     inodes     soft     hard    /dev/mapper/class-stu01       81920      51200      81920          1        6        8    /dev/mapper/class-stu02           0      40960      61440          0        4        6

9.启用磁盘配额功能。

[root@localhost ~]# quotaon -ugv /mnt/stu02/  /dev/mapper/class-stu02 [/mnt/stu02]: group quotas turned on  /dev/mapper/class-stu02 [/mnt/stu02]: user quotas turned on

10.为了方便测试,放开权限。

[root@localhost ~]# chmod 777 /mnt/stu02/

11.切换到zhangsan用户。

[root@localhost ~]# su - zhangsan

12.磁盘空间配额测试。

[zhangsan@localhost stu02]$ dd if=/dev/zero of=/mnt/stu02/test1.txt bs=1M count=50  dm-3: warning, user block quota exceeded.  50+0 records in  50+0 records out  52428800 bytes (52 MB) copied, 0.509643 s, 103 MB/s  [zhangsan@localhost stu02]$ dd if=/dev/zero of=/mnt/stu02/test2.txt bs=1M count=50  dm-3: write failed, user block limit reached.  dd: error writing ‘/mnt/stu02/test2.txt’: Disk quota exceeded  11+0 records in  10+0 records out  10485760 bytes (10 MB) copied, 0.117714 s, 89.1 MB/s  [zhangsan@localhost stu02]$ ls -l  total 61472  -rw-------. 1 root     root         7168 Aug 25 03:01 aquota.group  -rw-------. 1 root     root         7168 Aug 25 03:08 aquota.user  drwx------. 2 root     root        16384 Aug 25 00:20 lost+found  -rw-rw-r--. 1 zhangsan zhangsan 52428800 Aug 25 03:30 test1.txt  -rw-rw-r--. 1 zhangsan zhangsan 10485760 Aug 25 03:30 test2.txt

13.文件数量配额测试。

[zhangsan@localhost stu02]$ touch test{3..7}.txt  dm-3: warning, user file quota exceeded.  dm-3: write failed, user file limit reached.  touch: cannot touch ‘test7.txt’: Disk quota exceeded  [zhangsan@localhost stu02]$ ls -l  total 61472  -rw-------. 1 root     root         7168 Aug 25 03:01 aquota.group  -rw-------. 1 root     root         7168 Aug 25 03:08 aquota.user  drwx------. 2 root     root        16384 Aug 25 00:20 lost+found  -rw-rw-r--. 1 zhangsan zhangsan 52428800 Aug 25 03:30 test1.txt  -rw-rw-r--. 1 zhangsan zhangsan 10485760 Aug 25 03:30 test2.txt  -rw-rw-r--. 1 zhangsan zhangsan        0 Aug 25 03:33 test3.txt  -rw-rw-r--. 1 zhangsan zhangsan        0 Aug 25 03:33 test4.txt  -rw-rw-r--. 1 zhangsan zhangsan        0 Aug 25 03:33 test5.txt  -rw-rw-r--. 1 zhangsan zhangsan        0 Aug 25 03:33 test6.txt

14.查看用户zhangsan的配额使用情况。

[root@localhost ~]# quota -u zhangsan  Disk quotas for user zhangsan (uid 1000):       Filesystem  blocks   quota   limit   grace   files   quota   limit   grace  /dev/mapper/class-stu01                    81920*  51200   81920   6days       1       6       8  /dev/mapper/class-stu02                    61440*  40960   61440   6days       6*      4       6   6days

总结#

通过以上实验,可以发现XFS文件系统的磁盘配额只要文件数量、磁盘空间其中任何一个达到限额,就无法继续写文件;而EXT4文件系统则不同,磁盘空间满了还可以创建空文件,直至数量达到限额,或者数量达到限额,覆盖已有文件创建非空文件,直至磁盘容量达到限额。

Linux 磁盘配额(XFS & EXT4)

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

栗子博客 软件 Linux 磁盘配额(XFS & EXT4) https://www.lizi.tw/soft/15844.html

常见问题
  • 1、杰齐1.7仅适用于PHP5.2 2、需Zend支持 3、尽量使用宝塔面板 4、尽量使用Windows 系统,关关对Linux支持不太友好。
查看详情

相关文章

评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Linux 磁盘配额(XFS & EXT4)-海报

分享本文封面