利用quota對Vsftpd做磁盤配額
1 增加一塊2G的硬盤
打開VMware點擊VM---settings—add—Hard Disk—---Create a new virtual disk—SCSI—disk size(GB)2.0----重啓虛擬機系統
2 驗證硬盤:
[root@viong ~]# fdisk -l
#已增加了2G的/dev/sdb Disk /dev/sdb: 2147 MB, 2147483648 bytes -----------à說明已經生效了 255 heads, 63 sectors/track, 261 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes
3 先把/dev/sdb劃分爲一個擴展分區,再新建一個邏輯分區,格式化爲ext3分區,然後設置爲重啓自動加載。
[root@viong ~]# fdisk /dev/sdb #對/dev/sdb進行分區 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) Command (m for help): n #增加一個分區 Command action e extended p primary partition (1-4) #e增加擴展分區 Partition number (1-4): 1 First cylinder (1-261, default 1): 回車 Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-261, default 261): 回車 Using default value 261 Command (m for help): w #保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@viong ~]# fdisk /dev/sdb Command (m for help): n Command action l logical (5 or over) p primary partition (1-4) #l增加邏輯分區 First cylinder (1-261, default 1): 回車 Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-261, default 261): 回車 Using default value 261 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
4 驗證/dev/sdb的分區情況
Disk /dev/sdb: 2147 MB, 2147483648 bytes 255 heads, 63 sectors/track, 261 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sdb1 1 261 2096451 5 Extended /dev/sdb5 1 261 2096419+ 83 Linux
5 格式化 /dev/sdb5
[root@viong ~]# mkfs.ext3 /dev/sdb5 格式化sdb5 mke2fs 1.39 (29-May-2006) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) 262144 inodes, 524104 blocks 26205 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=536870912 16 block groups 32768 blocks per group, 32768 fragments per group 16384 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 25 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
6 創建一個目錄來掛載sdb5
[root@viong ~]# mkdir /mnt/ftp [root@viong ~]# mount /dev/sdb5 /mnt/ftp/ 掛載sdb5到/mnt/ftp/ [root@viong ~]# vi /etc/fstab 添加最後一條信息,來達到重啓自動掛載上去 /dev/sdb5 /mnt/ftp ext3 defaults 0 0
7 #mount -a 最後重啓系統看有沒有被自動掛載上
8 創建一個ftp用戶做測試
[root@red-hat-5 ~]# useradd -d /mnt/ftp/hom -s /sbin/nologin hom [root@red-hat-5 ~]# passwd hom Changing password for user hom. New UNIX password: Retype new UNIX password: passwd: all authentication tokens updated successfully. [root@red-hat-5 ~]# ll /mnt/ftp/ 總計 20 drwx------ 2 hom hom 4096 01-10 17:15 hom drwx------ 2 root root 16384 01-10 16:53 lost+found
[root@red-hat-5 ~]# service vsftpd start 爲 vsftpd 啓動 vsftpd:[確定]
9 爲ftp用戶做磁盤配額
剛纔創建的hom用戶是在/dev/sdb5分區中,那麼如果我們要對hom用戶進行磁盤限額,那我們需要修改/etc/fstab中根分區的記錄,將/dev/sdb5分區的第4個字段改成defaults,usrquota,如下:
/dev/sdb5 /mnt/ftp ext3 defaults,usrquota 0 0
爲了不然系統重啓才生效,利用一下命令直接生效
# mount -o remount /dev/sdb5
10 對一個組進行磁配額,那我們需要增加參數grpquota,如下:
/dev/sdb5 /mnt/ftp ext3 defaults,usrquota, grpquota 0 0
11 生成每個啓動了配額的文件系統的當前磁盤用量表
root@viong /]# quotacheck -avug quotacheck: Scanning /dev/sdb5 [/mnt/ftp] quotacheck: Cannot stat old user quota file: 沒有那個文件或目錄 quotacheck: Old group file not found. Usage will not be substracted. done quotacheck: Checked 4 directories and 6 files quotacheck: Old file not found.
註釋:
-a :掃瞄所有在 /etc/mtab 裏頭已經 mount 的具有 quota 支持的磁盤
-u :掃瞄使用者的檔案與目錄
-v :顯示掃瞄過程
-g :掃瞄羣組使用的檔案與目錄
-m :強制進行 quotacheck
12 設置限額容量
[root@viong /]# edquota -u hom (設置限額容量)
爲用戶hom設置磁盤配額
系統會自動用
VI文本打開配額文件,如下:
Disk quotas for user hom (uid 501): Filesystem blocks soft hard inodes soft hard /dev/sdb5 16 0 1024 4 0 0
這裏爲了做測試,我把硬塊限度爲1024kb
註釋:
Filesystem是啓用了配額的文件系統的名稱
blocks顯示了用戶當前使用的塊數,單位爲KB
soft用來設置用戶在該文件系統上的軟塊限度.使用者在寬限期間之內,他的容量可以超過 soft ,但必需要寬限時間之內將磁盤容量降低到 soft 的容量限制之下
hard用來設置用戶在該文件系統上的硬塊限度,絕對不能超過的容量
inodes顯示了用戶當前使用的i節點數量。
最後兩列用來設置用戶在該文件系統上的軟硬i節點限度.不同的是軟限可以在一段時期內被超過。 soft 到 hard 之間的容量其實就是寬限的容量啦!可以達到針對使用者的警示作用!這段時期被稱爲過渡期(grace period),默認七天的超越。過渡期可以用秒鐘、分鐘、小時、天數、週數、或月數表示。
如果以上值中的任何一個被設置爲 0,那個限度就不會被設置。
註釋:
-u :編輯 user 的 quota
-g :編輯 group 的 quota
-t :編輯寬限時間(就是超過 quota 值後,還能使用硬盤的寬限期限)
-p :copy 模板(以建立好的使用者或羣組)到另一個使用者(或羣組)
13 打開磁盤配額監控進程
root@viong /]# quotaon -avu
u是用戶g是組,這裏我沒設置g參數
/dev/sdb5 [/mnt/ftp]: user quotas turned on
註釋:
-a :全部的 quota 設定都啓動(會自動去尋找 /etc/mtab 的設定)
-u :使用者的 quota 啓動
-g :羣組的 quota 設定啓動
-v :顯示訊息
14 要校驗用戶的配額是否被設置
[root@viong /]# quota -uvs hom Filesystem blocks quota limit grace files quota limit grace /dev/sdb5 16 0 1024 4 0 0
註釋:
-g :顯示 group 羣組
-u :顯示 user
-v :顯示 quota 的值
-s :選擇 inod 或 硬盤空間來顯示
15 設置過渡期(grace period),只針對軟限制而言
[root@viong /]# edquota –t Grace period before enforcing soft limits for users: Time units may be: days, hours, minutes, or seconds Filesystem Block grace period Inode grace period /dev/sdb5 7days 7days
16 查看用戶配額
*** Report for user quotas on device /dev/sdb5 Block grace time: 7days; Inode grace time: 7days Block limits File limits User used soft hard grace used soft hard grace ---------------------------------------------------------------------- root -- 35880 0 0 5 0 0 hom -- 16 0 1024 4 0 0
註釋:
-a 列出在/etc/fstab文件裏,有加入quota設置的分區的使用狀況與用戶和羣組
-g 列出所有羣組的磁盤空間限制
-u 列出所有用戶的磁盤空間限制
-v 顯示該用戶或羣組的所有空間限制
17 測試用戶配額是否生效
下面利用FlashXP做測試,上傳一首故鄉原風景.mp3,傳輸到1M就失敗了.說明生效了
[root@viong /]# quotaoff -vug /dev/sdb5
關閉/mnt/ftp分區的磁盤限額
/dev/sdb5 [/mnt/ftp]: user quotas turned off
註釋:
-a :全部的 quota 設定都關閉(會自動去尋找 /etc/mtab 的設定)
最後並刪除/etc/fstab中設置配額的部分
總結:在對用戶進行磁盤限額時,需要掌握以下幾點原則:
A.由於對用戶進行文件數量的限制不是很實用,所以通常進行磁盤配額只限制用戶佔用的磁盤容量。
B.爲用戶設置的軟限制和硬限制的數值都不應該小於用戶已佔用的磁盤容量或文件數量,否則可能造成用戶無法正常登錄和使用系統。
C.設置的硬限制數量應該大於軟限制數量,否則沒有實際的意義
D././boot/./proc./mnt/cdrom 等不要使用配額
E.quota 實際在運作的時候,是針對整個分區進行限制的,例如:如果你的 /dev/sdb5 是掛載在/mnt/ftp底下,那麼在 /mnt/ftp底下的所有目錄都會受到限制!