บน Server ที่มีหลาย User หรือหลาย Application ทำงานพร้อมกัน การที่ User คนใดคนหนึ่งเก็บไฟล์มากเกินไปจนทำให้ Disk เต็มเป็นปัญหาที่พบบ่อย Disk Quotas คือระบบกำหนดขีดจำกัดการใช้พื้นที่ Disk และจำนวนไฟล์ให้แต่ละ User หรือ Group เพื่อป้องกันไม่ให้ User ใดคนหนึ่งใช้พื้นที่เกินสิทธิ์
บทความนี้อธิบายการติดตั้งและตั้งค่า Disk Quotas บน Linux ตั้งแต่การเปิดใช้งาน Quota บน Filesystem, การกำหนด Soft Limit และ Hard Limit, การตรวจสอบสถานะ Quota ของ User รวมถึงการใช้ Project Quota สำหรับ Directory เฉพาะเจาะจง
Disk Quota คืออะไร
Disk Quota ทำงานใน 2 ระดับคือ User Quota (จำกัดต่อ User) และ Group Quota (จำกัดต่อ Group) แต่ละระดับมี 2 ค่าจำกัดคือ Soft Limit ที่ User สามารถเกินได้ชั่วคราวภายใน Grace Period และ Hard Limit ที่เกินไม่ได้เด็ดขาด
# หลักการทำงานของ Quota
# Soft Limit: เกินได้ชั่วคราว (ค่าเริ่มต้น Grace Period = 7 วัน)
# Hard Limit: เกินไม่ได้เด็ดขาด — เขียนไฟล์ไม่ได้ทันทีเมื่อถึงขีดจำกัด
# ตัวอย่าง:
# User มี Soft Limit = 1GB, Hard Limit = 1.2GB
# ถ้า User ใช้ 1.1GB → ได้รับ Warning แต่ยังเขียนไฟล์ได้ (ภายใน Grace Period)
# ถ้า User ใช้ 1.2GB → เขียนไฟล์ไม่ได้อีกต่อไป
# Quota มีสองชนิด:
# Block Quota — จำกัดพื้นที่ (KB/MB/GB)
# Inode Quota — จำกัดจำนวนไฟล์
ติดตั้งและเปิดใช้งาน Quota
# ติดตั้ง quota tools
sudo apt install quota # Ubuntu/Debian
sudo dnf install quota # RHEL/Rocky
# ตรวจสอบว่า Filesystem รองรับ Quota หรือไม่
# Filesystem ต้อง Mount ด้วย options: usrquota และ/หรือ grpquota
# แก้ไข /etc/fstab เพื่อเปิด Quota บน /home
# เดิม:
# /dev/sda2 /home ext4 defaults 0 2
# ใหม่:
# /dev/sda2 /home ext4 defaults,usrquota,grpquota 0 2
# Remount Filesystem (หรือ Reboot)
sudo mount -o remount /home
# สร้าง Quota Database Files
sudo quotacheck -cugm /home
# -c = สร้างไฟล์ใหม่, -u = User, -g = Group, -m = ไม่ Remount read-only
# ตรวจสอบว่าสร้าง Database แล้ว
ls /home/aquota.*
# /home/aquota.user — User Quota database
# /home/aquota.group — Group Quota database
# เปิด Quota
sudo quotaon /home
sudo quotaon -v /home # แสดงผลแบบ Verbose
กำหนด Quota ให้ User
# เปิด Quota Editor สำหรับ User
sudo edquota -u username
# ตัวอย่าง Output ของ edquota (แก้ไขได้ในนี้)
# Disk quotas for user deploy (uid 1001):
# Filesystem blocks soft hard inodes soft hard
# /dev/sda2 204800 512000 614400 1250 0 0
#
# blocks = พื้นที่ที่ใช้งานอยู่ (KB)
# soft = Soft Limit (KB) — 0 = ไม่จำกัด
# hard = Hard Limit (KB) — 0 = ไม่จำกัด
# inodes = จำนวนไฟล์ที่ใช้งานอยู่
# ค่าตัวเลข blocks/inodes ใน edquota คือ KB (1GB = 1048576)
# กำหนด Quota แบบไม่ Interactive (ใช้ใน Script)
# Soft Limit = 500MB (512000 KB), Hard Limit = 600MB (614400 KB)
sudo setquota -u username 512000 614400 0 0 /home
# กำหนด Quota แบบใช้หน่วยที่อ่านง่าย (quota-tools รุ่นใหม่)
sudo setquota -u username 500M 600M 0 0 /home
# Copy Quota จาก User หนึ่งไปยังอีก User
sudo edquota -p template_user target_user
กำหนด Quota ให้ Group
# เปิด Quota Editor สำหรับ Group
sudo edquota -g groupname
# กำหนด Group Quota แบบไม่ Interactive
# Soft Limit = 5GB, Hard Limit = 6GB, ไม่จำกัด Inode
sudo setquota -g developers 5242880 6291456 0 0 /home
# ดู Quota ของ Group
sudo repquota -g /home
ตรวจสอบสถานะ Quota
# ดู Quota ของ User ปัจจุบัน
quota
quota -s # Human-readable
# ดู Quota ของ User เฉพาะ (root เท่านั้น)
sudo quota -u username
sudo quota -us username # Human-readable
# ดู Quota ทุก User บน Filesystem
sudo repquota /home
sudo repquota -s /home # Human-readable
# ดู Quota ทุก Filesystem
sudo repquota -a
# ตัวอย่าง Output ของ repquota
# User used soft hard grace files soft hard
# root -- 36 0 0 5 0 0
# deploy -- 204800 512000 614400 1250 0 0
# john +- 520000 512000 614400 6days 2100 0 0
# -- = ใช้งานปกติ
# +- = เกิน Soft Limit (กำลังนับ Grace Period)
ปรับ Grace Period
# ดู Grace Period ปัจจุบัน
sudo edquota -t
# ตัวอย่าง Output
# 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/sda2 7days 7days
# แก้ใน Editor นี้ได้เลย เช่น เปลี่ยนเป็น 3 วัน
# 3days 3days
XFS Quota — Filesystem ที่รองรับ Quota แบบ Native
XFS Filesystem รองรับ Quota แบบ Native โดยใช้ Mount Options และ xfs_quota command แทนที่จะใช้ quota tools ทั่วไป
# Mount XFS พร้อม Quota (ใน /etc/fstab)
# /dev/sdb1 /data xfs defaults,uquota,gquota 0 2
# ใช้ xfs_quota จัดการ Quota บน XFS
sudo xfs_quota -x -c 'limit bsoft=500m bhard=600m user1' /data
# ดู Quota ทุก User
sudo xfs_quota -c 'report -h' /data
# ดู Quota ของ User เฉพาะ
sudo xfs_quota -c 'quota -h user1' /data
# กำหนด Inode Limit ด้วย
sudo xfs_quota -x -c 'limit isoft=10000 ihard=12000 user1' /data
Project Quota — จำกัดพื้นที่ต่อ Directory
Project Quota (หรือ Directory Quota) ช่วยจำกัดพื้นที่ของ Directory เฉพาะโดยไม่ขึ้นกับ User ที่เป็นเจ้าของไฟล์ เหมาะสำหรับ Web Hosting ที่แต่ละ Site ควรได้รับพื้นที่จำกัด
# สร้าง Project ID และกำหนด Path (XFS)
echo "1:/var/www/site1" | sudo tee -a /etc/projects
echo "1:site1" | sudo tee -a /etc/projid
# กำหนด Quota ให้ Project
sudo xfs_quota -x -c 'project -s site1' /var/www
sudo xfs_quota -x -c 'limit -p bsoft=2g bhard=2.5g site1' /var/www
# ตรวจสอบ Project Quota
sudo xfs_quota -c 'report -h -p' /var/www
Script ตรวจสอบ Quota อัตโนมัติ
#!/bin/bash
# /usr/local/bin/quota-report.sh
# ส่ง Email แจ้ง User ที่ใช้ Quota เกิน 80%
FILESYSTEM="/home"
THRESHOLD=80
repquota -s "$FILESYSTEM" | tail -n +3 | while read line; do
user=$(echo "$line" | awk '{print $1}')
used=$(echo "$line" | awk '{print $3}' | sed 's/M//')
soft=$(echo "$line" | awk '{print $4}' | sed 's/M//')
if [ -n "$soft" ] && [ "$soft" -gt 0 ]; then
pct=$(( used * 100 / soft ))
if [ "$pct" -ge "$THRESHOLD" ]; then
echo "Your disk usage is ${pct}% of your quota limit." \
| mail -s "[Warning] Disk Quota ${pct}% on $(hostname)" "${user}@example.com"
fi
fi
done
# เพิ่มใน Crontab รันทุกวัน
# 0 8 * * * /usr/local/bin/quota-report.sh
สรุป
Disk Quotas ช่วยป้องกัน User หรือ Application ใดคนหนึ่งจากการใช้ Disk เกินควร การตั้งค่าต้องเปิด usrquota/grpquota ใน /etc/fstab ก่อน จากนั้นสร้าง Database ด้วย quotacheck และกำหนด Limit ด้วย edquota หรือ setquota Soft Limit ให้ Grace Period ในการแจ้งเตือน ส่วน Hard Limit บังคับทันที สำหรับ XFS Filesystem ใช้ xfs_quota แทน และรองรับ Project Quota สำหรับจำกัดพื้นที่ต่อ Directory
แนะนำบริการ DE
การตั้งค่า Disk Quotas บน Server จริงต้องการสิทธิ์ Root และ Filesystem ที่รองรับ Cloud VPS ของ DE ให้ Root Access เต็มรูปแบบพร้อม ext4 และ XFS Filesystem สามารถตั้งค่า Disk Quotas สำหรับ Multi-tenant Environment หรือ Web Hosting ที่ต้องการควบคุมพื้นที่ของแต่ละ User ได้อย่างอิสระ
หากต้องการโฮสต์เว็บไซต์โดยมีการจัดการ Disk Quota อัตโนมัติ Cloud Hosting ของ DE มีระบบควบคุม Storage Quota ต่อ Account ผ่าน Control Panel พร้อมแจ้งเตือนเมื่อใกล้เต็ม

