Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> Linux資訊 >> 更多Linux

FreeBSD 磁碟配額(Quota)實作:限制使用者空間與檔案數

虛擬主機代管的關鍵之一,是把每個帳號可用的空間與檔案數量鎖在特定範圍內。FreeBSD 與 Linux 同樣以 Quota 機制達成,本文說明 FreeBSD 上的完整設定流程。

一、讓核心支援 Quota

編輯核心設定檔,加入 QUOTA 選項後重新編譯:

machine i386
cpu I686_CPU
ident CNOSvhost
maxusers 0
options QUOTA

接著在 /etc/rc.conf 打開開機時的配額檢查:

enable_quotas="YES"
check_quotas="YES"

二、在 fstab 指定套用配額的檔案系統

以 /pub 為例,同時啟用使用者配額與群組配額:

# Device Mountpoint FStype Options Dump Pass#
/dev/ad0s1b none swap sw 0 0
/dev/ad0s1a / ufs rw 1 1
/dev/ad0s1h /pub ufs rw,userquota,groupquota 2 2
/dev/ad0s1e /tmp ufs rw 2 2
/dev/ad0s1g /usr ufs rw 2 2
/dev/ad0s1f /var ufs rw 2 2
/dev/acd0c /cdrom cd9660 ro,noauto 0 0
proc /proc procfs rw 0 0

存檔後建立配額資料庫並列出目前狀態:

# quotacheck -av
# repquota -a

三、編輯個別帳號的配額

# edquota c4st

開啟的編輯畫面如下:

Quotas for user c4st:
/pub: kbytes in use: 3438, limits (soft = 100000, hard = 100020)
  inodes in use: 25, limits (soft = 25, hard = 26)

第一行是空間:已用 3438 KB,soft 為軟上限(到達時先警告),hard 為真正不能超越的硬上限,本例約 100 MB。第二行 inodes 限制的是檔案數量,換算下來該帳號只能建立 26 個檔案。

實務上 inodes 的數字要依用途調整。若要安裝以文字資料檔為主的 CGI 系統,例如各類 web bbs 或文章管理程式,就必須把 hard 值大幅提高,否則網站很快就會因為「檔案數已滿」而無法寫入。

四、常用 Quota 指令

edquota -t 設定軟上限的寬限期,單位可用 days、hours、minutes 或 seconds:

Time units may be: days, hours, minutes, or seconds
Grace period before enforcing soft limits for users:
/pub: block grace period: 1 day, file grace period: 1 day

repquota -a 輸出各檔案系統的配額總表,可清楚看到每個帳號的 used/soft/hard 與剩餘寬限:

Block limits  File limits
User used soft hard grace used soft hard grace
root 9136 0 0 - 12 0 0 -
web 22152 0 0 - 122 0 0 -
c4st 3438 100000 100020 - 25 25 26 22:56

quota 查詢單一帳號的用量與上限,常用參數包括:-g 看群組配額、-l 略過 NFS 檔案系統、-u 看使用者配額、-q 只列出已超額的摘要、-v 顯示完整設定。

edquota -p test c4st c4st1 c4st2 可把 test 的配額範本直接複製給其他帳號,大量開站時特別省事。

quotacheck -a 建議放進 crontab 定期執行,用來確認配額設定與實際用量一致。

Copyright © Linux教程網 All Rights Reserved