歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> CentOS關於quota的總結與實踐

CentOS關於quota的總結與實踐

日期:2017/3/3 11:06:50   编辑:Linux技術
1 理論基礎
[b]1.1 Quota的概念[/b]
Quota即限額的意思,用來限制用戶、組、文件夾的空間使用量。
[b]1.2 用途范例[/b]
- web服務器控制站點可用空間大小
- mail服務器控制用戶可用空間大小
- file服務器控制用戶可用空間大小
[b]1.3 限制模式[/b]
- 根據用戶(UID)控制每個用戶的可用空間大小
- 根據組(GID)控制每個組的可用空間大小
- 根據目錄(directory,project)控制每個目錄的可用空間大小(xfs可用project模式)
[b]1.4 使用條件[/b]
- EXT格式只能對文件系統進行限制,xfs可用對project進行限制
- 內核需要預開啟對Quota支持
- Quota限制只對非管理員有效
- 默認只開啟對/home使用Quota,其他需要配置SELinux
[b]1.5 限制的可配置對象[/b]
- 根據用戶(User)、組(Group)、特定目錄(project)
- 容量限制或文件數量限制(block/inode)
- 限制值soft(超過空間用量給予警告和寬限時間)和hard(超過空間用量則剝奪用戶使用權)
- 寬限時間(grace time),空間用量超出soft限定而未達到hard限定給予的處理時限(超出時限soft值變成hard值)
2 實際操作
[b]2.1 配置前准備[/b]
[b][b]2.1.1 建立用戶組[/b][/b]
groupadd gp1
[b][b]2.1.2 添加組成員[/b][/b]
useradd -g gp1 user1
echo "pwd1" | passwd --stdin user1

useradd -g gp1 user2
echo "pwd1" | passwd --stdin user2
[b][b]2.1.2 創建用戶目錄並變更所有組[/b][/b]
mkdir /home/gp1
chgrp gp1 /home/gp1
chmod 2770 /home/gp1
[b][b]2.1.2 檢查文件系統類型[/b][/b]
df -hT /home
顯示如下:
Filesystem              Type  Size  Used Avail Use% Mounted on
/dev/mapper/centos-home xfs   5.0G   67M  5.0G   2% /home
[b]2.2 啟用文件系統的quota功能[/b]
[b][b]2.2.1 編輯fstab[/b][/b]
vim /etc/fstab
修改內容如下:
/dev/mapper/centos-home  /home  xfs  defaults,usrquota,grpquota   0 0
注,類型如下:
- 根據用戶(uquota/usrquota/quota)
- 根據組(gquota/grpquota)
- 根據目錄(pquota/prjquota)(不能與grpquota同時設定)
[b][b]2.2.2 卸載並重新掛載[/b][/b]
umount /home
mount -a
[b][b]2.2.3 檢查[/b][/b]
mount | grep home
顯示如下:
/dev/mapper/centos-home on /home type xfs (rw,relatime,seclabel,attr2,inode64,usrquota,grpquota)
[b]2.3 查閱Quota信息[/b]
[b][b]2.3.1 命令格式[/b][/b]
xfs_quota -x -c "子命令" [掛載點]
選項:
-x  :專家模式,使用-x才能使用-c
-c  :子命令選項
子命令:
      print :列出當前系統參數等
      df    :類似於df,選項有-b (block) -i (inode) -h (加上單位)等
      report:列出quota項目,包含-ugr (user/group/project)和-bi等
      state :列出當前支持quota文件系統信息和相關的啟動項
[b][b]2.3.2 查詢支持Quota的分區[/b][/b]
xfs_quota -x -c "print"
[b][b]2.3.3 查詢Quota目錄的使用情況[/b][/b]
xfs_quota -x -c "df -h" /home
[b][b]2.3.4 顯示用戶的Quota的限制信息[/b][/b]
xfs_quota -x -c "report -ubih" /home
注,顯示項目加參數“-u”
[b]2.4 配置限制[/b]
[b][b]2.4.1 命令格式:[/b][/b]
xfs_quota -x -c "limit [-ug] b[soft|hard]=N i[soft|hard]=N name"
xfs_quota -x -c "timer [-ug] [-bir] Ndays"
選項:
limit :限制的對象,包括user/group/project
        bsoft/bhard : block的soft/hard限制值
        isoft/ihard : inode的soft/hard限制值
        name        : 用戶和組的名稱
timer :寬限時間(grace time)
[b][b]2.4.2 根據用戶和塊大小限制[/b][/b]
xfs_quota -x -c "limit -u bsoft=250M bhard=300M user1" /home
xfs_quota -x -c "limit -u bsoft=250M bhard=300M user2" /home
檢查配置:
xfs_quota -x -c "report -ubih" /home
[b][b]2.4.3 根據組和塊大小限制[/b][/b]
xfs_quota -x -c "limit -g bsoft=950M bhard=1G gp1" /home
檢查配置:
xfs_quota -x -c "report -gbih" /home
[b][b]2.4.5 配置寬限時間[/b][/b]
xfs_quota -x -c "timer -ug -b 14days" /home
驗證配置:
xfs_quota -x -c "state" /home
[b][b]2.4.6 驗證Quta[/b][/b]
su - user1
dd if=/dev/zero of=123.img bs=1M count=310
ll -h
exit
xfs_quota -x -c "report -ubh" /home
[b]2.5 根據project限制[/b]
[b][b]2.5.1 修改fstab[/b][/b]
vim /etc/fstab
[b][b]2.5.2 卸載掛載並重新掛載[/b][/b]
umount /home
mount -a
[b][b]2.5.3 檢查取消[/b][/b]
xfs_quota -x -c "state"
[b][b]2.5.4 創建專案存儲位置[/b][/b]
mkdir /home/proj01
[b][b]2.5.5 指定項目識別號[/b][/b]
echo "01:/home/proj01" >> /etc/projects
[b][b]2.5.6 指定項目名稱並關聯項目識別號[/b][/b]
echo "proj01:01" >> /etc/projid
[b][b]2.5.7 初始化項目名稱[/b][/b]
xfs_quota -x -c "project -s proj01"
檢查配置:
xfs_quota -x -c "print " /home
xfs_quota -x -c "report -pbih " /home
[b][b]2.5.8 根據塊大小配置限制[/b][/b]
xfs_quota -x -c "limit -p bsoft=450M bhard=500M proj01" /home
檢查配置:
xfs_quota -x -c "report -pbih " /home
[b][b]2.5.9 驗證配置[/b][/b]
dd if=/dev/zero of=/home/myquota/123.img bs=1M count=510
[b]2.6 Quota的管理[/b]
[b][b]2.6.1 臨時禁用Quota限制[/b][/b]
xfs_quota -x -c "disable -up" /home
檢查禁用:
xfs_quota -x -c "state" /home
驗證禁用:
dd if=/dev/zero of=/home/user1/123.img bs=1M count=520
查閱Quota狀態:
xfs_quota -x -c "report -pbh" /home
清理測試文件:
rm -rf /home/user1/123.img
[b][b]2.6.2 臨時啟動Quota限制[/b][/b]
xfs_quota -x -c "enable -up" /home
檢驗啟動:
dd if=/dev/zero of=/home/user1/123.img bs=1M count=520
[b][b]2.6.3 完全關閉Quota限制[/b][/b]
xfs_quota -x -c "off -up" /home
測試關閉:
xfs_quota -x -c "enable -up" /home
恢復關閉:
umount /home; mount -a
[b][b]2.6.4 [/b][/b][b][b]刪除Quota限制(無法恢復)[/b][/b]
xfs_quota -x -c "off -up" /home
xfs_quota -x -c "remove -p" /home
驗證刪除:
xfs_quota -x -c "report -phb" /home
[b]2.7 利用軟連接實現Quota[/b]
1)啟用/home分區的Quota功能
編輯fstab
vim /etc/fstab
修改內容如下:
/dev/mapper/centos-home  /home  xfs  defaults,usrquota,grpquota   0 0
2)創建軟連接到Quota分區
ln -s /home/mail /var/spool/mail
-------------------------------------------------------------
參閱文檔
-------------------------------------------------------------
http://linux.vbird.org/linux_basic/0420quota.php
http://www.centoscn.com/CentOS/config/2013/1103/2043.html
http://www.jb51.net/os/RedHat/400503.html
本文出自 “老譚linux集群博客” 博客,請務必保留此出處http://cmdschool.blog.51cto.com/2420395/1831311
Copyright © Linux教程網 All Rights Reserved