磁盤配額
在Linux系統中一般都是多用戶同時進行,多個用戶使用同一塊磁盤會不會發生一個很尷尬的事情。用戶A大量使用磁盤空間,等到B使用的時候沒怎麼用就滿了。這不就尷尬了,對於B用戶來說也很不公平。
那麼今天就說說在linux中的磁盤配個是個什麼鬼東西,在所支持quota下的linux系統才能使用磁盤配額哦QWQ。
磁盤配額大意是指可以限制每個用戶使用多少空間,若使用超過多少會提醒,超過多少會禁止用戶使用。
但是這個限制是依據文件系統(可以說是某個分區),而不是某個目錄(其實掛載到某個目錄下不也是對某個目錄的限制,純屬個人瞎扯0.0)這個所謂的限制又可以分爲inode和block限制,簡單的來說就是限制文件數量或者磁盤使用的大小。
這裏的限制參數有兩個,一個是soft和hard。超過soft會報警但還可以使用,一旦超過hard值那就GG(不能使用了)。比如這裏還要提及一個時間的設定那就是一但報警超過設定時間直接也是不能使用。一會兒下面會提及到。。。QAQ
重點來了,瞎掰扯了半天一點有用的都沒有,那就實驗一波看看到底怎麼個用法。
在這裏本人就先創建兩個測試用戶來試一波這個功能
[root@centos6 ~]# Display all 2840 possibilities? (y or n) [root@centos6 ~]# useradd test1 [root@centos6 ~]# echo "test1" | passwd --stdin test1 Changing password for user test1. passwd: all authentication tokens updated successfully. [root@centos6 ~]# useradd test2 [root@centos6 ~]# echo "test2" | passwd --stdin test2 Changing password for user test2. passwd: all authentication tokens updated successfully.
然後再我的linux下有一個/dev/sdb1分區我就把他掛載到/home家目錄下來測試多用戶使用/home家目錄時超額會發生神馬情況,
[root@centos6 ~]# mount /dev/sdb1 /home/ >> 掛載也沒什麼好多說的了
如果只是想在本次開機實現配額那只需要
[root@centos6 ~]# mount -o remount,usrquota /home [root@centos6 ~]# mount | grep home /dev/sdb1 on /home type ext4 (rw,usrquota) [root@centos6 ~]#
然後直接執行一條命令自動掃描含有usrquota命令的文件系統,就是我們上面設置的
接下來就是檢查命令quotacheck
-u :針對用戶掃瞄檔案不目弽的使用情況,會建立 aquota.user
-g :針對羣組掃瞄檔案不目弽的使用情況,會建立 aquota.group
-v :顯示掃瞄過程的信息;
-f :強制掃瞄文件系統,幵寫入新的 quota 配置文件 (危險)
-M :強制以讀寫的方式掃瞄文件系統,只有在特殊情況下才會使用
[root@localhost etc]# quotacheck -avu 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/sdb1 [/home] done quotacheck: Cannot stat old user quota file /home/aquota.user: No such file or directory. Usage will not be subtracted. quotacheck: Old group file name could not been determined. Usage will not be subtracted. quotacheck: Checked 3 directories and 0 files quotacheck: Old file not found. 接下來就是開啓這個功能 (既然on是開啓 那不用想off就是關閉該功能了只加一個a選項就行) [root@localhost etc]# quotaon -auv /dev/sdb1 [/home]: user quotas turned on [root@localhost etc]#
上面的都是前戲,接下來就是具體的設置了
[root@localhost etc]# edquota -u test1 Disk quotas for user test1 (uid 1011): Filesystem blocks soft hard inodes soft hard /dev/sdb1 28 0 0 7 0 0 ~ ~ ~ ~ ~
上面是輸入edquota代碼之後的腳本,在裏面修改參數。用戶test2也是如此 下面就是我設置之後的腳本
Disk quotas for user test2 (uid 1012): Filesystem blocks soft hard inodes soft hard /dev/sdb1 28 50 100 7 0 0 ~
上面block參數是KB(系統默認)只是舉個例子,實際參數這樣設置太小。
還有就是報警的時間命令爲edquota -t
[root@localhost etc]# 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/sdb1 7days 7days ~ ~ ~ ~
上面設置soft,hard和時間都是執行命令進入文檔來修改文檔。
設置完畢後用quota -uvs 跟用戶名來查看設置的情況
quota -u 後跟用戶名顯示quota限制值可以跟一個或多個 -v顯示每個用戶在文件系統的限制值 -s用單位表示 限制值大小比如M -g 跟用戶組 [root@localhost etc]# quota -uvs test1 Disk quotas for user test1 (uid 1011): Filesystem space quota limit grace files quota limit grace /dev/sdb1 28K 50K 100K 7 0 0 [root@localhost etc]#
上面也可以規整一個repquota -avus (查看全局)
[root@localhost etc]# quota -uvs test1 Disk quotas for user test1 (uid 1011): Filesystem space quota limit grace files quota limit grace /dev/sdb1 28K 50K 100K 7 0 0 [root@localhost etc]# requota -aus bash: requota: command not found... [root@localhost etc]# repquota -avus *** Report for user quotas on device /dev/sdb1 Block grace time: 7days; Inode grace time: 7days Space limits File limits User used soft hard grace used soft hard grace ---------------------------------------------------------------------- root -- 20K 0K 0K 2 0 0 test1 -- 28K 50K 100K 7 0 0 test2 -- 28K 50000K 100000K 7 0 0 Statistics: Total blocks: 7 Data blocks: 1 Entries: 3 Used average: 3.000000 [root@localhost etc]#
到此爲爲止就該測試一波了看看效果怎麼樣
我在test1下創建個文件大小1M 剛纔設置最大1M [test1@localhost ~]$ dd if=/dev/zero of=new bs=1M count=1M sdb1: warning, user block quota exceeded. sdb1: write failed, user block limit reached. > 直接報錯 磁盤滿了寫不進去 sdb1: write failed, user block limit reached. dd: error writing ‘new’: Disk quota exceeded 1+0 records in 0+0 records out 49152 bytes (49 kB) copied, 0.00172157 s, 28.6 MB/s [test1@localhost ~]$ ls new
看來這次雖然馬馬虎虎,還是成功了test2就不測試了效果也是一樣的。如果要開機啓動,下次開機還有那就寫到/etc/fstab文件裏。具體就man一下看看man文檔找有路徑的字眼就好。。
到這裏簡單的配置磁盤配額就完了還是比較簡單的QAQ