Linux下使用quota命令管理磁盤空間的實例教程

1.通過yum 方式 安裝quota
#yum install quota
2.VirtualBox創(chuàng)建硬盤
如果你的Linux環(huán)境建立在VirtualBox下:
(1)關閉虛擬機鏡像
點擊setting 配置虛擬機所使用的硬件
(2)配置新硬盤
選擇Create new disk
選擇VHD (virtual hard disk)
輸入硬盤鏡像名字
(3)格式化硬盤
進入linux,使用命令創(chuàng)建硬盤進行格式化
#mkfs -t ext4 /dev/sdb
(4)為硬盤空間尋找目錄
#mount /dev/sdb /home
如果有需要長期掛著,請自行百度如何修改/etc/fstab,本人修改多次,讓系統(tǒng)多次崩潰,最終放棄,選擇在 vi /etc/rc.local 里面,業(yè)余的加入 mount /dev/sdb /home 這條命令。
3.格式化新創(chuàng)建的硬盤
[root@localhost ~]# mkfs -t ext4 /dev/sdb
mke2fs 1.41.12 (12-Apr-2016)
/dev/sdb is entire device, not just one partition!
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
524288 inodes, 2097152 blocks
104857 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2147483648
64 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
掛在硬盤到/home目錄
[root@localhost ~]# mount /dev/sdb /home/
[root@localhost ~]# mount -o remount,usrquota,grpquota /home
[root@localhost ~]# mount
/dev/mapper/VolGroup-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sdb on /home type ext4 (rw,usrquota,grpquota)
為系統(tǒng)設置啟動后自動掛載硬盤
[root@localhost ~]#vi /etc/rc.local
添加
mount /dev/sdb /home
mount -o remount,usrquota,grpquota /home
備注:本人并沒有采取修改/etc/fstab 的方法,因為該方法容易因為輸入的字符或格式不對導致系統(tǒng)崩潰,所以采取了修改用戶啟動文件的方法
4.配置quota
[root@localhost ~]#quotacheck -avug 對整個系統(tǒng)含有 usrquota, grpquota 參數(shù)的文件系統(tǒng)進行 quotacheck 掃描
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/sdb [/home] done
quotacheck: Cannot stat old user quota file: No such file or directory
quotacheck: Cannot stat old group quota file: No such file or directory
quotacheck: Cannot stat old user quota file: No such file or directory
quotacheck: Cannot stat old group quota file: No such file or directory
quotacheck: Checked 2 directories and 0 files
quotacheck: Cannot create new quotafile /home/aquota.user.new: Permission denied
quotacheck: Cannot initialize IO on new quotafile: Permission denied
quotacheck: Cannot create new quotafile /home/aquota.group.new: Permission denied
quotacheck: Cannot initialize IO on new quotafile: Permission denied
出現(xiàn)報錯,報錯原因是因為沒有關閉selinux
[root@localhost ~]# setenforce 0
[root@localhost ~]# quotacheck -avug
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/sdb [/home] done
quotacheck: Cannot stat old user quota file: No such file or directory
quotacheck: Cannot stat old group quota file: No such file or directory
quotacheck: Cannot stat old user quota file: No such file or directory
quotacheck: Cannot stat old group quota file: No such file or directory
quotacheck: Checked 2 directories and 0 files
quotacheck: Old file not found.
quotacheck: Old file not found.
成功生成
[root@localhost ~]#quotaon -auvg 啟動quota
5.為用戶添加硬盤空間限制
創(chuàng)建賬戶
[root@localhost ~]# useradd quotauser1
[root@localhost ~]# passwd quotauser1
[root@localhost ~]# edquota -u quotauser1
Disk quotas for user quotauser1 (uid 500):
Filesystem blocks soft hard inodes soft hard
/dev/sdb 16 500000 600000 4 0 0
限制用戶quotauser1的使用空間為500M,最大限制是600M
參數(shù)意義參考
soft :這是最低限制容量的意思,使用者在寬限期間之內,他的容量可以超過 soft ,但必需要寬限時間之內將磁盤容量降低到 soft 的容量限制之下!
hard :這是『絕對不能超過』的容量!跟 soft 相比的意思為何呢?通常 hard limit 會比 soft limit 為高,例如網絡磁盤空間為 30 MB ,那么 hard limit 就設定為 30MB ,但是為了讓使用者有一定的警戒心,所以當使用空間超過 25 MB 時,例如使用者使用了 27 MB 的空間時,那么系統(tǒng)就會警告使用者,讓使用者可以在『寬限時間內』將他的檔案量降低至 25 MB ( 亦即是 soft limit )之內!也就是說, soft 到 hard 之間的容量其實就是寬限的容量啦!可以達到針對使用者的『警示』作用!
寬限時間:那么寬限時間就可以很清楚的知道含意是什么了!也就是當您的使用者使用的空間超過了 soft limit ,卻還沒有到達 hard limit 時,那么在這個『寬限時間』之內,就必需要請使用者將使用的磁盤容量降低到 soft limit 之下!而當使用者將磁盤容量使用情況超過 soft limit 時,『寬限時間』就會自動被啟動,而在使用者將容量降低到 soft limit 之下,那么寬限時間就會自動的取消啰!
6.測試
登陸quotauser1
創(chuàng)建超過600M的文件
[quotauser1@localhost ~]$ dd if=/dev/zero of=bigfile bs=1M count=700
sdb: warning, user block quota exceeded.
sdb: write failed, user block limit reached.
dd: writing `bigfile': Disk quota exceeded
586+0 records in
585+0 records out
614379520 bytes (614 MB) copied, 2.75934 s, 223 MB/s
[quotauser1@localhost ~]$ ls
bigfile
[quotauser1@localhost ~]$ ls -l
total 599984
-rw-rw-r--. 1 quotauser1 quotauser1 614379520 Sep 28 03:28 bigfile
可以發(fā)現(xiàn)文件的大小被限制了
相關文章
Linux系統(tǒng)中開起和關閉用戶的磁盤空間限制的命令
這篇文章主要介紹了Linux系統(tǒng)中開起和關閉用戶的磁盤空間限制的命令,需要的朋友可以參考下2016-01-14Linux系統(tǒng)中quota磁盤命令的相關使用解析
這篇文章主要介紹了Linux系統(tǒng)中quota磁盤命令的相關使用解析,包括edquota和quotacheck的命令的用法講解,需要的朋友可以參考下2016-01-14在CentOS系統(tǒng)中安裝quota來管理磁盤配額
這篇文章主要介紹了在CentOS系統(tǒng)中安裝quota來管理磁盤配額的方法,同樣適用于Fedora等其他RedHat系的Linux系統(tǒng),需要的朋友可以參考下2016-01-15- 這系列文章主要是一刀寫的linux相關學習資料,這篇文章主要介紹了linux下的磁盤配額相關知識,需要的朋友可以參考下2013-04-15
- Quota:OpenBSD下配置磁盤限額 Author: MichaelBibby Date: 2005/11/26 本文可以任意轉載,但請保留以上作者信息,謝謝。 Quota是用來配置磁盤限額的一個工具。2008-09-08
- 虛擬主機中經常要限制用戶空間的大小和文件的數(shù)量。這些限制在linux和FreeBSD中都是用QUOTA來實現(xiàn)的。這里我說下在FreeBSD下實現(xiàn)的方法; 開啟QUOTA支持 首先需要修2008-09-08
- 配額可以使各個用戶和組無法占用分區(qū)的全部空間。可以通過限定inode數(shù)而配置配額,每個inode與特定文件相關聯(lián);也可以設置絕對極限(KB數(shù))。 默認情況下,readhat會2008-09-08
Linux系統(tǒng)中的mount掛載磁盤命令使用教程
這篇文章主要介紹了Linux系統(tǒng)中的mount掛載磁盤命令使用教程,文中列舉了包括掛載光盤、移動硬盤和U盤的實例,需要的朋友可以參考下2016-03-17- 這篇文章主要介紹了Linux中mdadm命令管理RAID磁盤陣列的實例總結,包括詳細的mdadm命令的基本參數(shù)整理,需要的朋友可以參考下2016-03-15
在騰訊云的Linux系統(tǒng)服務器上格式化和分區(qū)磁盤的教程
這篇文章主要介紹了在騰訊云的Linux系統(tǒng)服務器上格式化和分區(qū)磁盤的教程,文中分格式化大于2TB和小于2TB的數(shù)據(jù)盤兩種情況來講,非常詳細,需要的朋友可以參考下2016-03-08