Linux 磁盤配額(XFS & EXT4)


CentOS-Logo

若是在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文件系統則不同,磁盤空間滿了還可以創建空文件,直至數量達到限額,或者數量達到限額,覆蓋已有文件創建非空文件,直至磁盤容量達到限額。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM