磁盤配額實現
磁盤配額要求必須是獨立的分區
創建一個新的分區
#同步分區表
[root@localhost ~]# partx -a /dev/sda
[root@localhost ~]# mkfs.ext4 /dev/sda6 -L /home
[root@localhost ~]# blkid
/dev/sda6: LABEL=”/home” UUID=”49a738a9-05f5-46ba-bd46-37998b8bb4c3″ TYPE=”ext4″
目錄遷移:注意防止用戶訪問造成數據丟失
[root@localhost ~]# mkdir /mnt/home
[root@localhost ~]# mount /dev/sda6 /mnt/home/
[root@localhost ~]# cp -av /home/* /mnt/home/
[root@localhost ~]# du -sh /home/
736K /home/
[root@localhost ~]# du -sh /mnt/home/
748K /mnt/home/
[root@localhost ~]# rm -rf /home/*
#且到單用戶模式,只能在機器跟前操作了,,小心使用
[root@localhost ~]# init 1
修改開機掛載配置文件
[root@localhost ~]# vim /etc/fstab
UUID=49a738a9-05f5-46ba-bd46-37998b8bb4c3 ??????/home ??ext4 ???defaults 0 0
[root@localhost ~]# mount -a
[root@localhost ~]# df
Filesystem ????1K-blocks ???Used Available Use% Mounted on
/dev/sda6 ??????20510716 ??45720 ?19416328 ??1% /mnt/home
/dev/sda6 ??????20510716 ??45720 ?19416328 ??1% /home
[root@localhost ~]# umount /mnt/home/
家目錄遷移完畢
[root@localhost ~]# su – ding
[ding@localhost ~]$ pwd
/home/ding
[ding@localhost ~]$ touch f1
實現配額
#1. 修改配置文件啟用磁盤配額功能
[root@localhost ~]# vim /etc/fstab
UUID=49a738a9-05f5-46ba-bd46-37998b8bb4c3 ??????/home ??ext4 ???usrquota,grpquota?0 0
usrquota:限定用戶
grpquota:限定組
[root@localhost ~]# mount -o remount /home
[root@localhost ~]# mount
/dev/sda6 on /home type ext4 (rw,usrquota,grpquota)
#在磁盤配額所在分區的根上,創建磁盤配額數據庫,永來限定每個用戶使用磁盤空間的配置信息
#2. 創建磁盤配額數據庫
禁用selinux
[root@localhost home]# ll /etc/selinux/config /etc/sysconfig/selinux
-rw-r–r–. 1 root root 459 Dec ?5 16:18 /etc/selinux/config
lrwxrwxrwx. 1 root root ?17 Nov ?7 15:36 /etc/sysconfig/selinux?-> ../selinux/config
[root@localhost home]# cat /etc/selinux/config
[root@localhost home]# cat /etc/sysconfig/selinux
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# cd /home/
#-cug是對應著/etc/fstab中的usrquoat和grpquota的,如果只在配置文件中寫的是usrquota,使用 -cu就好了
[root@localhost home]# quotacheck -cug /home/
[root@localhost home]# ls
aquota.group ?aquota.user ?ding ?lost+found
#3. 啟用磁盤配額數據庫
#查看磁盤配額數據庫是否啟用
[root@localhost home]# quotaon -p /home/
group quota on /home (/dev/sda6) is off
user quota on /home (/dev/sda6) is off
#啟用磁盤配額數據庫
[root@localhost home]# quotaon /home/
[root@localhost home]# quotaon -p /home/
group quota on /home (/dev/sda6) is on
user quota on /home (/dev/sda6) is on
#4. 指定用戶的空間限制
Disk quotas for user ding (uid 500):
Filesystem blocks soft hard inodes soft hard
/dev/sda6 748 0 0 155 0 0
Filesystem:設置磁盤配額啟動的分區,有多個會都列出來
blocks soft hard 關心空間大小,限定了指定用戶最多用多大空間
inodes soft hard 節點個數,限定了指定用戶可以建立的文件個數
blocks:已經使用的空間
inodes:當前用戶已經建立了多少文件
soft:警告值,超過就會報警,但是不限制用戶
hard:不能操作
bloke單位為:1k
Disk quotas for user ding (uid 500):
Filesystem ??????????????????blocks ??????soft ??????hard ????inodes ????soft ????hard
/dev/sda6 ??????????????????????748 ?????????0 ?????????0 ???????155 ???????0 ???????0
Disk quotas for user ding?(uid 500):
Filesystem ??????????????????blocks ??????soft ??????hard ????inodes ????soft ????hard
/dev/sda6 ??????????????????????748 ??????800000 ????1000000 ?155 ???????160 ?????170
#5. 測試
注意:使用指定的用戶去測,在家目錄測試
[ding@localhost ~]$ dd if=/dev/zero of=~/f1 bs=1M count=830
sda6: warning, user block quota exceeded.
830+0 records in
830+0 records out
870318080 bytes (870 MB) copied, 25.87 s, 33.6 MB/s
[ding@localhost ~]$ dd if=/dev/zero of=~/bigfile bs=1M count=780
[ding@localhost ~]$ dd if=/dev/zero of=~/bigfile2 bs=1M count=21
sda6: warning, user block quota exceeded.
21+0 records in
21+0 records out
22020096 bytes (22 MB) copied, 0.0183497 s, 1.2 GB/s
[ding@localhost ~]$ dd if=/dev/zero of=~/bigfile3 bs=1M count=300
sda6: write failed, user block limit reached.
dd: writing `/home/ding/bigfile3′: Disk quota exceeded
不是所有的命令都可以看到報警提示,有時候只是執行失敗,但是不提示為什么錯
[ding@localhost ~]$ echo >> f1
-bash: echo: write error: Disk quota exceeded
#超出指定后創建文件失敗
[ding@localhost ~]$ touch f{f..k}
sda6: write failed, user file limit reached.
touch: cannot touch `fg’: Disk quota exceeded
#有警告提示,但文件創建成功
[ding@localhost ~]$ touch f11q
sda6: warning, user file quota exceeded.
-rw-rw-r–. 1 ding ?ding ????????0 Dec ?5 17:03 f11q
#達到最大值創建失敗
[ding@localhost ~]$ touch {a..k}
sda6: write failed, user file limit reached.
touch: cannot touch `j’: Disk quota exceeded
根據什么判斷用戶占用的空間?
把用戶家目錄中的文件移走,并不能減少限制,刪除可以,更改所有則所屬組可以。文件占用的空間和它放哪沒關系,和文件的所有者有關系
#文件的所有者是誰,空間就算在誰頭上
[root@localhost ding]# chown mayun bigfile4
[root@localhost ding]# quota ding
Disk quotas for user ding (uid 500):
Filesystem ?blocks ??quota ??limit ??grace ??files ??quota ??limit ??grace
/dev/sda6 ?385596??800000 1000000 ????????????154 ????160 ????170
復制有個用戶的權限設置給另一個用戶
[root@localhost ding]# edquota -p ding mayun
[root@localhost ding]# edquota mayun
復制之后可以在改改
統一設置磁盤配額
[root@localhost ding]# useradd xm; setquota xm 10000 20000 100 200 /home
[root@localhost ding]# quota xm
Disk quotas for user xm (uid 502):
Filesystem ?blocks ??quota ??limit ??grace???files ??quota ??limit ??grace
/dev/sda6 ?????32 ??10000 ??20000 ??????????????8 ????100 ????200
[root@localhost ding]# edquota xm
Disk quotas for user xm (uid 502):
Filesystem ??????????????????blocks ??????soft ??????hard ????inodes ????soft ????hard
/dev/sda6 ???????????????????????32 ?????10000 ?????20000 ?????????8 ?????100 ?????200
grace:?警告可以超過,limit不能超過,grace是允許暫時超,7天之內可以達到警告超了沒事,7天之后,soft也是不能超的了。再創建新的就不讓創建了,只能減少不能增加了。只是給了一個寬限期而已
[root@localhost ding]# repquota /home/
*** Report for user quotas on device /dev/sda6
Block?grace?time: 7days; Inode grace time: 7days
Block limits ???????????????File limits
User ???????????used ???soft ???hard ?grace ???used ?soft ?hard ?grace
———————————————————————-
root ?????— ?????20 ??????0 ??????0 ?????????????2 ????0 ????0
ding ?????-+ ?385596 ?800000 1000000 ???????????170 ??160 ??170 ?6days
mayun ????— ?614436 ?800000 1000000 ?????????????9 ??160 ??170
xm ???????— ?????32 ??10000 ??20000 ?????????????8 ??100 ??200
組配額的問題
[root@localhost ~]# setquota –help
setquota: Usage:
setquota [-u|-g] [-rm] [-F quotaformat] <user|group>
-u, –user ????????????????set limits for user
-g, –group ???????????????set limits for group
-a, –all ?????????????????set limits for all filesystems
[root@localhost ding]# useradd xh
[root@localhost ding]# groupadd sales
[root@localhost ding]# usermod -G sales xh
[root@localhost ding]# usermod -G sales xm
設置組的磁盤配額
[root@localhost ding]# edquota -g?sales
Disk quotas for group sales (gid 504):
Filesystem ??????????????????blocks ??????soft ??????hard ????inodes ????soft ????hard
/dev/sda6 ????????????????????????0 ?????????0 ?????????0 ?????????0 ???????0 ???????0
測試:
[root@localhost ~]# su – xm
直接測試會無效,因為創建的文件的所有者是小明,和組沒關系
[xm@localhost ~]$ newgrp sales
[xm@localhost ~]$ dd if=/dev/zero of=f1 bs=1M count=200
sda6: warning, group block quota exceeded.
200+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 1.90333 s, 110 MB/s
[xm@localhost ~]$ ll
total 204800
-rw-r–r–. 1 xm sales 209715200 Dec ?5 17:37 f1
組中的用戶數據總和超過設定值
取消磁盤配額
[root@localhost ding]# quotaoff /home/
[root@localhost ding]# quotaon -p /home/
group quota on /home (/dev/sda6) is off
user quota on /home (/dev/sda6) is off
是二進制文件
[root@localhost home]# ls
aquota.group ?aquota.user …………
[root@localhost home]# rm -rf aquota.*
[root@localhost home]# vim /etc/fstab
UUID=1e33b24a-4a67-4ea4-889b-ce5c553b0bb6 /home ext4 ???defaults ???????0 ??????0
[root@localhost home]# mount -o remount /home/
[root@localhost home]# mount
/dev/sda6 on /home type ext4 (rw)
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/89707