redhat 5版本磁盘配额quota配置
磁盘配额配置。
磁盘配置操作对root用户不生效。
 
1要想使用磁盘配额功能,首先要确定自己的系统内核是否支持磁盘。
查看方法
[root@zhaoyun ~]# grep -i quota /boot/config-2.6.18-164.el5
如果出现如下配置则支持quota配置功能。
[root@zhaoyun ~]# grep -i quota /boot/config-2.6.18-164.el5
CONFIG_NETFILTER_XT_MATCH_QUOTA=m
CONFIG_QUOTA=y
CONFIG_QUOTACTL=y
[root@zhaoyun ~]#
2、在配置quota前查看要配置quota的分区是否支持quota。
现在的根分区是不支持quota的
[root@zhaoyun ~]# mount |grep /dev/sda1
/dev/sda1 on / type ext3 (rw)
[root@zhaoyun ~]#
3、使分区支持quota
[root@zhaoyun ~]# mount -o remount,usrquota,grpquota /
usrquota 可以为用户配置磁盘配额
grpquota 可以为组配置磁盘配额
[root@zhaoyun ~]# mount -o remount,usrquota,grpquota /
[root@zhaoyun ~]# mount |grep /dev/sda1
/dev/sda1 on / type ext3 (rw,usrquota,grpquota)
[root@zhaoyun ~]#
上面的方法是临时生效的重启后就没有了,解决办法
1、可以把他加到开机启动中
echo "mount -o remount,usrquota,grpquota /" >> /etc/rc.local
这样下次开机就可以自动使分区支持quota功能了。
2、编写/etc/fstab文件
添加
如果在/分区支持,则修改/的一行,如果是其他分区需要自己添加。
只需要在default字段后以,号分隔,添加  ,usrquota,grpquota就可以了。
格式
[root@zhaoyun ~]# vi /etc/fstab
LABEL=/                 /                       ext3    defaults,usrquota,grpquota        1 1
/dev/sda2             /quota              ext3       defaults,usrquota,grpquota     0  0
写完后,使用mount -o remount 命令使配置生效。
[root@zhaoyun ~]# mount |grep /dev/sda1
/dev/sda1 on / type ext3 (rw)
[root@zhaoyun ~]#
[root@zhaoyun ~]# mount -o remount /
[root@zhaoyun ~]# mount
/dev/sda1 on / type ext3 (rw,usrquota,grpquota)
4、创建quota配置文件
[root@zhaoyun ~]# quotacheck -cugm /
在执行完此命令,在/目录下生成俩个文件,其他分区在分区挂载的目录下查看。
[root@zhaoyun ~]# ll / |grep quota
-rw-------   1 root root  7168 11-29 06:57 aquota.group
-rw-------   1 root root  7168 11-29 06:57 aquota.user
[root@zhaoyun ~]#
5、开启用户支持quota
quotaon -p / 看根分区的是否开启用户quota配置
quotaon / 开启根分区支持用户quota配置
[root@zhaoyun ~]# quotaon -p /
group quota on / (/dev/sda1) is off
user quota on / (/dev/sda1) is off
[root@zhaoyun ~]# quotaon /
[root@zhaoyun ~]# quotaon -p /
group quota on / (/dev/sda1) is on
user quota on / (/dev/sda1) is on
[root@zhaoyun ~]#
6、编辑quota配置文件。
假如说我有个user1的用户,需要对他进行在根分区的配额管理,那么就这么写
[root@zhaoyun ~]# edquota user1 /
对文件数的限制
限制用户在写第4个文件的时候警告,第7个文件不允许写入。
Disk quotas for user user1 (uid 500):
                                       soft 超过 大小警告                                     soft超过个数警告 
  Filesystem              blocks       soft                   hard     inodes                soft                hard
  /dev/sda1                  60             0                        0               8                      11                   14
                                                 hard超过大小不能写                          hard超过个数不能写
验证效果
[user1@zhaoyun ~]$ touch file1
[user1@zhaoyun ~]$ touch file2
[user1@zhaoyun ~]$ touch file3
[user1@zhaoyun ~]$ touch file4
sda1: warning, user file quota exceeded.
[user1@zhaoyun ~]$ touch file5
[user1@zhaoyun ~]$ touch file6
[user1@zhaoyun ~]$ touch file7
sda1: write failed, user file limit reached.
touch: 无法触碰 “file7”: 超出磁盘限额
[user1@zhaoyun ~]$ ls
file1  file2  file3  file4  file5  file6
[user1@zhaoyun ~]$
对磁盘大小使用的限制
写入文件超过4M警告,不能错过6M
[root@zhaoyun ~]# edquota user1 /
Disk quotas for user user1 (uid 500):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sda1                        84       4000         6000         14              0          0
~
 
验证效果
[user1@zhaoyun ~]$ dd if=/dev/zero of=file bs=1M count=5
sda1: warning, user block quota exceeded.
5+0 records in
5+0 records out
5242880 bytes (5.2 MB) copied, 0.0343485 seconds, 153 MB/s
[user1@zhaoyun ~]$ dd if=/dev/zero of=file bs=1M count=6
sda1: warning, user block quota exceeded.
sda1: write failed, user block limit reached.
dd: 写入 “file”: 超出磁盘限额
6+0 records in
5+0 records out
6057984 bytes (6.1 MB) copied, 0.0394068 seconds, 154 MB/s
第三列:soft 对使用磁盘空间大小的限制 单位k,只警告,单不超过hard的值就可以继续写    对大小的软限制
第四列:hard 对使用空间大小的限制,单位k,不允许超过此值的大小。如果一个文件操作则造成文件顺坏。 对大小的硬限制
 
第六列: soft 对文件个数的限制,限制可以有几个文件,在原有inodes上增加限制的个数,只警告。
第七列:   hard 硬限制,创建文件的个数不允许超过此值。
 
7、用命令设置quota,适合写脚本的时候用
#设置user1的配额,大小限制,超过4M警告,不允许超过5M
[root@zhaoyun ~]# setquota user1 4000 5000 0 0 /
#拷贝user1的配额信息给user2
[root@zhaoyun ~]# edquota -p user1 user2
edquota: User user2 doesn't exist.
[root@zhaoyun ~]#
[root@zhaoyun ~]# useradd user2
[root@zhaoyun ~]# edquota -p user1 user2
[root@zhaoyun ~]# edquota user2
Disk quotas for user user2 (uid 501):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sda1                        60       4000       5000          8        0        0
8、查看配置使用情况
repquota -a  #查看用户配额使用情况   等同于 repquota -ua 
[root@zhaoyun ~]# repquota -a
*** Report for user quotas on device /dev/sda1
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      -- 3621420       0       0         117355     0     0      
daemon    --      20       0       0              3     0     0      
lp        --      16       0       0              2     0     0      
apache    --      24       0       0              3     0     0      
rpc       --       4       0       0              1     0     0      
smmsp     --      16       0       0              2     0     0      
webalizer --      32       0       0              4     0     0      
squid     --      16       0       0              2     0     0      
avahi     --      20       0       0              3     0     0      
xfs       --       4       0       0              1     0     0      
rpcuser   --       8       0       0              1     0     0      
user1     +-    6000    4000    5000  6days      10     0     0      
user2     --      60    4000    5000              8     0     0      
 
repquota -ga  查看组配额使用情况。