一、原理
Linux下的磁盘配额可以针对各个用户和组对磁盘容量使用进行限制。可以限制容量和文件的个数。软限制:在一段时间内允许超出限制
(默认7天),可以使用#edquota -t来修改软限制宽限期
硬限制:不允许超过限制
如果对于组来做限制,组成员“争用”限制,比如限制aa组为100M,BOB属于aa组,存了99M,其他人只能存1M。
二、实验
首先创建一个新分区,然后在所创建的新的分区下来进行磁盘配额的实验
1、查看新的磁盘分区(最好在虚拟机里面添加新的磁盘来做实验,我添加的是2G的scsi磁盘)
[root@localhost ~]# fdisk -l
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 778 6144862+ 83 Linux
/dev/sda3 779 909 1052257+ 82 Linux swap / Solaris
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdb doesn't contain a valid partition table
2、创建新的磁盘分区
[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun,
SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory
only,
until you decide to write them. After that, of course, the
previous
content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be
corrected by w(rite)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-261, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-261, default 261):
+100M
Command (m for help): p
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 13 104391 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
3、格式化新添分区
[root@localhost ~]# mkfs.ext3 /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
26104 inodes, 104388 blocks
5219 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
13 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to
override.
4、将新添加分区挂载到/pub目录里面去,并实现开机自动挂载
[root@localhost ~]# mkdir /pub
[root@localhost ~]# cat /etc/fstab
LABEL=/ / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
LABEL=SWAP-sda3 swap swap defaults 0 0
/dev/sdb1 /pub ext3 defaults 0 0
5、挂载新添加分区
[root@localhost ~]# mount /dev/sdb1 /pub/
[root@localhost /]# chmod 757 /pub/
6、对pub分区做磁盘配额,修改/etc/fstab
[root@localhost pub]# cat /etc/fstab
LABEL=/ / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
LABEL=SWAP-sda3 swap swap defaults 0 0
/dev/sdb1 /pub ext3 defaults,usrquota,grpquota 0 0
7、重新挂载/pub分区
[root@localhost pub]# mount -o remount /pub
[root@localhost pub]# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/hdc on /media/VMware Tools type iso9660
(ro,noexec,nosuid,nodev,uid=0)
/dev/sdb1 on /pub type ext3 (rw,usrquota,grpquota)
8、创建配额文件
[root@localhost pub]# quotacheck -cmug /pub/
[root@localhost pub]# ls /pub
aquota.group aquota.user lost+found
9、创建普通用户测试
[root@localhost pub]# useradd user01
[root@localhost pub]# passwd user01
Changing password for user user01.
New UNIX password:
BAD PASSWORD: it is WAY too short
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
10、对user01账户进行配额限制,软限制容量为10M,硬限制容量为20M,创建文件个数的软限制为5个,硬限制的文件个数为10个
[root@localhost pub]# edquota -u user01
Disk quotas for user user01 (uid 500):
Filesystem blocks soft hard inodes soft hard
/dev/sdb1 0 10000 20000 0 5 10
11、开启配额功能
[root@localhost pub]# quotaon /pub
12、容量验证
[user01@localhost ~]# su – user01
[user01@localhost ~]$ dd if=/dev/zero of=/pub/1.txt bs=15K
count=1024
sdb1: warning, user block quota exceeded.
1024+0 records in
1024+0 records out
15728640 bytes (16 MB) copied, 0.110386 seconds, 142 MB/s
[user01@localhost ~]$ dd if=/dev/zero of=/pub/2.txt bs=15K
count=1024
sdb1: write failed, user block limit reached.
sdb1: write failed, user block limit reached.
dd: 写入 “/pub/2.txt”: 超
出磁盘限额
304+0 records in
303+0 records out
4665344 bytes (4.7 MB) copied, 0.0562608 seconds, 82.9 MB/s
[user01@localhost ~]$ du -sh /pub/
du: 无法读取目录 “/pub/lost+found”:
权限不够
20M /pub/
13、文件个数的限制验证
[user01@localhost ~]$ ls -l /pub
总计 20026
-rw-rw-r-- 1 user01 user01 15728640 05-03 07:55 1.txt
-rw-rw-r-- 1 user01 user01 4665344 05-03 07:57 2.txt
-rw------- 1 root root 7168 05-03 07:55 aquota.group
-rw------- 1 root root 7168 05-03 07:44 aquota.user
drwx------ 2 root root 12288 05-03 07:30 lost+found
[user01@localhost ~]$ touch /pub/3.txt
[user01@localhost ~]$ touch /pub/4.txt
[user01@localhost ~]$ touch /pub/5.txt
sdb1: write failed, user block limit reached.
touch: 无法触碰 “/pub/5.txt”:
超出磁盘限额
[user01@localhost ~]$ ls -l /pub/
总计 20028
-rw-rw-r-- 1 user01 user01 15728640 05-03 07:55 1.txt
-rw-rw-r-- 1 user01 user01 4665344 05-03 07:57 2.txt
-rw-rw-r-- 1 user01 user01 0 05-03 07:59 3.txt
-rw-rw-r-- 1 user01 user01 0 05-03 07:59 4.txt
-rw------- 1 root root 7168 05-03 07:55 aquota.group
-rw------- 1 root root 7168 05-03 07:44 aquota.user
drwx------ 2 root root 12288 05-03 07:30 lost+found
14、磁盘配额的关闭及删除
[root@localhost /]# quotaoff /pub/
[root@localhost /]# rm -rf /pub/aquota.*
未完,待续。。。
本文出自 “crazylinux
工作室” 博客,请务必保留此出处http://crazylinux.blog.51cto.com/259244/163070