歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

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

1.通過yum 方式 安裝quota

復制代碼代碼如下:
#yum install quota

2.VirtualBox創建硬盤
如果你的Linux環境建立在VirtualBox下:
(1)關閉虛擬機鏡像
點擊setting 配置虛擬機所使用的硬件
2016412111217295.png (773×610)

(2)配置新硬盤
2016412111248895.png (660×467)

選擇Create new disk
2016412111306236.png (650×479)

選擇VHD (virtual hard disk)
2016412111427931.png (667×432)

輸入硬盤鏡像名字
2016412111446505.png (668×449)

(3)格式化硬盤
進入linux,使用命令創建硬盤進行格式化

復制代碼代碼如下:
#mkfs -t ext4 /dev/sdb

(4)為硬盤空間尋找目錄

復制代碼代碼如下:
#mount /dev/sdb /home

如果有需要長期掛著,請自行百度如何修改/etc/fstab,本人修改多次,讓系統多次崩潰,最終放棄,選擇在 vi /etc/rc.local 裡面,業余的加入 mount /dev/sdb /home 這條命令。

3.格式化新創建的硬盤

復制代碼代碼如下:
[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)

為系統設置啟動後自動掛載硬盤

復制代碼代碼如下:
[root@localhost ~]#vi /etc/rc.local

添加

復制代碼代碼如下:
mount /dev/sdb /home
mount -o remount,usrquota,grpquota /home

備注:本人並沒有采取修改/etc/fstab 的方法,因為該方法容易因為輸入的字符或格式不對導致系統崩潰,所以采取了修改用戶啟動文件的方法

4.配置quota

復制代碼代碼如下:
[root@localhost ~]#quotacheck -avug 對整個系統含有 usrquota, grpquota 參數的文件系統進行 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
出現報錯,報錯原因是因為沒有關閉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.為用戶添加硬盤空間限制
創建賬戶

復制代碼代碼如下:
[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
參數意義參考
  soft :這是最低限制容量的意思,使用者在寬限期間之內,他的容量可以超過 soft ,但必需要寬限時間之內將磁盤容量降低到 soft 的容量限制之下!
  hard :這是『絕對不能超過』的容量!跟 soft 相比的意思為何呢?通常 hard limit 會比 soft limit 為高,例如網絡磁盤空間為 30 MB ,那麼 hard limit 就設定為 30MB ,但是為了讓使用者有一定的警戒心,所以當使用空間超過 25 MB 時,例如使用者使用了 27 MB 的空間時,那麼系統就會警告使用者,讓使用者可以在『寬限時間內』將他的檔案量降低至 25 MB ( 亦即是 soft limit )之內!也就是說, soft 到 hard 之間的容量其實就是寬限的容量啦!可以達到針對使用者的『警示』作用!
  寬限時間:那麼寬限時間就可以很清楚的知道含意是什麼了!也就是當您的使用者使用的空間超過了 soft limit ,卻還沒有到達 hard limit 時,那麼在這個『寬限時間』之內,就必需要請使用者將使用的磁盤容量降低到 soft limit 之下!而當使用者將磁盤容量使用情況超過 soft limit 時,『寬限時間』就會自動被啟動,而在使用者將容量降低到 soft limit 之下,那麼寬限時間就會自動的取消啰!

6.測試
登陸quotauser1
創建超過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

可以發現文件的大小被限制了

Copyright © Linux教程網 All Rights Reserved