Hi!请登陆

CentOS磁盘配额

2020-10-27 55 10/27

磁盘配额可以限制指定账户能够使用的磁盘空间,这样可以避免因某个用户的过度使用磁盘空间造成其他用户无法正常工作甚至影响系统运行.
在Centos6中默认的ext系列的文件系统没有挂载磁盘配额,需要我们手动来挂载.
挂载选项:

usrquota   对用户进行管理
grpquota   对组进行管理
[[email protected] ~]$ mount -o defaults,usrquota,grpquota /dev/sdb1 /test/
#-o后面跟的是挂载选项,把sdb1挂载到/test/目录.defaults是默认的挂载选项

上面是分区没有挂载的情况,如果分区已经挂载可以用这种方法:

[[email protected] ~]#vim /etc/fstab        #fstab文件是挂载的配置文件,每行定义都一个要挂载的文件系统
UUID=80f63701-5c1b-4dc1-9864-7c1247ed8561 /                       ext4    defaults        1 1
UUID=87c9aa50-7b7a-459e-babc-87bfaf5b49f2 /boot                   ext4    defaults        1 2
UUID=2f7f0957-6978-4028-ac0d-966fb8d486e5 swap                    swap    defaults        0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
UUID=a30c64e6-fd9c-4886-926c-2cb13c7c2225  /test ext4   defaults        0 0

在文件里面追加一行格式是:设备文件可以写设备文件的名字、LABEL、UUID系统默认用的是UUID,然后是要挂载到的文件、文件系统的类型、挂载选项:默认是defaults,后面的两个数字第一个表示转储频率:0不做备份、1每天转储、2每隔一天转储、第二个表示自检次序:0不自检、1首先自检,这个两个数字都写0就可以了.

UUID=a30c64e6-fd9c-4886-926c-2cb13c7c2225  /test ext4   defaults,usrquota,grpquota 0 0  #我们在defaults的后面加上usrquota grpquota这两个选项,用,号分隔,然后保存退出
[[email protected] ~]#mount -o remount /dev/sdb1 /test/   #用remount选项重新挂载分区

注:mount命令只能临时挂载分区重启之后就会失效,想要永久挂载需要写在fstab文件中.所以第一种方法想要保存的话,还要需要把配置写进fstab文件.

挂载之后可以用mount命令来查看有没有生效:

[email protected] ~]#mount
/dev/sda2 on / type ext4 (rw) #因为有点多,所以我这里删除了几个挂载
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw) fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/sdb1 on /test type ext4 (rw,usrquota,grpquota)  #()里的是挂载项rw表示这个分区可以读写,可以看到我们刚才添加的选项已经生效了

配置分区的配额:

初始化数据库:quotacheck:

quotacheck [-gucbfinvdmMR] [-F <quota-format>] filesystem|-a      #格式
#quotacheck的一些选项
-u, --user                检查用户文件
-g, --group               检查组文件
-c, --create-files        创建新的配额文件
-b, --backup              创建旧配额文件的备份
-f, --force               即使启用了配额,也强制强制检查
-i, --interactive         交互方式
-n, --use-first-dquot     使用重复结构的第一个副本
-v, --verbose             执行过程的详细信息
-d, --debug               更详细的信息
-m, --no-remount          不重新安装文件系统只读
-M, --try-remount         尝试重新安装文件系统的只读的,即使失败也要继续
-R, --exclude-root        检查所有文件系统时排除root
-F, --format=formatname   检查特定格式的配额文件
-a, --all                 检查所有
-h, --help                帮助
[[email protected] test]#quotacheck -cug /test/  #初始化,创建用户和组的配置文件
[[email protected] test]#ll
-rw-------. 1 root root  6144 Jun 19 04:19 aquota.group   #这个是组的配置文件
-rw-------. 1 root root  6144 Jun 19 04:19 aquota.user   #这个是用户的配置文件
drwx------. 2 root root 16384 Jun 19 03:08 lost+found

注:如果提示不能创建文件、权限不足,有很大的可能是SELinux安全机制引起的,用setenforce 0命令把它关闭就行了,用getenforce检查SELinux的状态

开启磁盘配额:quotaon:

quotaon [-guvp] [-F quotaformat] [-x state] -a
quotaon [-guvp] [-F quotaformat] [-x state] filesys    #命令格式
-a, --all                为所有文件系统启用配额
-f, --off                关闭配额
-u, --user               对用户配额进行操作
-g, --group              对组配额进行操作
-p, --print-state        查看分区配额状态
-x, --xfs-command=cmd    对xfs分区进行操作
-F, --format=formatname  按照具体配额格式操作
-v, --verbose            详细信息
-h, --help               帮助
[[email protected] test]#quotaon /test/ #开启/test/的配额功能
[[email protected] test]#quotaon -p /test/     #检查状态
group quota on /test (/dev/sdb1) is on      #已经开启
user quota on /test (/dev/sdb1) is on

关闭磁盘配额:quotaoff 参数和quotaon的一样:

[[email protected] test]#quotaoff -p /test/        #检查状态
group quota on /test (/dev/sdb1) is off         #关闭状态
user quota on /test (/dev/sdb1) is off

编辑配额 edquota:

[[email protected] test]#edquota -h        #查看帮助
edquota [-rm] [-u] [-F formatname] [-p username] [-f filesystem] username ...
edquota [-rm] -g [-F formatname] [-p groupname] [-f filesystem] groupname ...
edquota [-u|g] [-F formatname] [-f filesystem] -t
edquota [-u|g] [-F formatname] [-f filesystem] -T username|groupname ...
#选项
-u, --user                    编辑用户
-g, --group                   编辑组
-r, --remote                  通过RPC编辑远程配额
-m, --no-mixed-pathnames      从NFSv4挂载点修剪主斜线
-F, --format=formatname       编辑具体格式的配额
-p, --prototype=name          复制其他用户或组的配置
    --always-resolve          总是尝试解析名称,即使它只是由数字组成
-f, --filesystem=filesystem   仅在特定文件系统上编辑数据
-t, --edit-period             编辑宽限期
-T, --edit-times              编辑单个用户或组的宽限时间
-h, --help                    查看帮助
-V, --version                 查看版本信息
[[email protected] ~]#edquota admin   #编辑admin用户
Disk quotas for user admin (uid 500):
  Filesystem       blocks       soft       hard     inodes     soft     hard
  /dev/sdb1           0          0          0          0        0        0

直接修改数字就可以配置配额了,可以用M、G:

Filesystem:文件系统名
blocks    :已经使用的块数,一块等于1K
soft      :数据块的软限制,0表示禁用
hard      :数据块的硬限制,0表示禁用,可用
inodes    :已经创建的文件个数,如果有*表示超出软限制
soft      :创建的文件个数的软限制,0表示禁用
hard      :创建的文件个数的硬限制,0表示禁用
Disk quotas for user admin (uid 500):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sdb1                         0         800      1M           0        8       10
#因为是测试所以只给了1M空间,和10文件,软限制给了800k和8个文件

实验文件大小限制:

[[email protected] test]$dd if=/dev/zero of=/test/1 bs=1M count=1  #创建1个1M的文件
sdb1: warning, user block quota exceeded.
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.00242046 s, 433 MB/s #创建成功
[[email protected] test]$ll -h  #查看/test目录下的文件详细信息
-rw-rw-r--. 1 admin admin 1.0M Jun 19 06:09 1 #刚刚创建的文件
-rw-------. 1 root  root  7.0K Jun 19 06:08 aquota.group
-rw-------. 1 root  root  7.0K Jun 19 06:13 aquota.user
drwx------. 2 root  root   16K Jun 19 03:08 lost+found
[[email protected] test]$dd if=/dev/zero of=/test/2 bs=1M count=1 #在创建1个文件2
sdb1: write failed, user block limit reached.
dd: writing `/test/2': Disk quota exceeded #提示超过了磁盘配额
1+0 records in
0+0 records out
0 bytes (0 B) copied, 0.00140152 s, 0.0 kB/s #创建失败

验证创建文件个数的限制:

[[email protected] test]$touch {1..8} #创建8个文件
[[email protected] test]$ll
-rw-rw-r--. 1 admin admin     0 Jun 19 06:26 1
-rw-rw-r--. 1 admin admin     0 Jun 19 06:26 2
-rw-rw-r--. 1 admin admin     0 Jun 19 06:26 3
-rw-rw-r--. 1 admin admin     0 Jun 19 06:26 4
-rw-rw-r--. 1 admin admin     0 Jun 19 06:26 5
-rw-rw-r--. 1 admin admin     0 Jun 19 06:26 6
-rw-rw-r--. 1 admin admin     0 Jun 19 06:26 7
-rw-rw-r--. 1 admin admin     0 Jun 19 06:29 8
-rw-------. 1 root  root   7168 Jun 19 06:26 aquota.group
-rw-------. 1 root  root   7168 Jun 19 06:26 aquota.user
drwx------. 2 root  root  16384 Jun 19 03:08 lost+found
[[email protected] test]$touch 9 #创建第九个文件
sdb1: warning, user file quota exceeded. #触发了软限制,提示将超出用户文件配额.
[[email protected] test]$ll #查看文件
-rw-rw-r--. 1 admin admin     0 Jun 19 06:26 1
-rw-rw-r--. 1 admin admin     0 Jun 19 06:26 2
-rw-rw-r--. 1 admin admin     0 Jun 19 06:26 3
-rw-rw-r--. 1 admin admin     0 Jun 19 06:26 4
-rw-rw-r--. 1 admin admin     0 Jun 19 06:26 5
-rw-rw-r--. 1 admin admin     0 Jun 19 06:26 6
-rw-rw-r--. 1 admin admin     0 Jun 19 06:26 7
-rw-rw-r--. 1 admin admin     0 Jun 19 06:29 8
-rw-rw-r--. 1 admin admin     0 Jun 19 06:29 9 #但是还是创建成功了
[[email protected] test]$touch 10 #创建第十个文件
[[email protected] test]$touch 11 #创建第十一个文件
sdb1: write failed, user file limit reached.
touch: cannot touch `11': Disk quota exceeded #报警,不能创建,超过了磁盘配额

直接在shell中编辑配额:setquota, 它和quotaon只有两个不同选项:

-b, --batch #从标准输入读取限制
-c, --continue-batch #在出现错误的情况下继续输入处理
Disk quotas for user admin (uid 500):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sdb1                      1024        800       1024         10        8       10
[[email protected] ~]#setquota admin 1M 2M 10 20 /test/    #设置软限制1M,硬限制2M.文件软10个,硬限制20个
[[email protected] ~]#edquota admin 查看
Disk quotas for user admin (uid 500):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sdb1                      1024       1024       2048         10       10       20
#配额设置被更改了
  

用一个用户的权限来为另一个用户创建配额权限:

[[email protected] ~]#useradd test1 #创建一个yoghurt
[[email protected] ~]#edquota -p admin test1 #复制admin的权限给test1
[[email protected] ~]#quota test1  #查看test1用户的权限
Disk quotas for user test1 (uid 501):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sdb1                         0       1024       2048          0       10       20

查看用户权限,除了进入配置文件还可以用quota 用户名来查看:

[email protected] test]$quota admin
Disk quotas for user admin (uid 500):
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace
      /dev/sdb1       0    1024    2048              10*      8      10   6days

注:上面可以看到已创建文件的后面的*号,表示超过的软限制.软限制:用户可以在超过,一般用来提示用户即将超过限制,但是软限制是有宽限期,如果过了宽限期用户也是不能创建文件的.宽限期管理员可以用edquota的t和T选项来设置.

Tag:

相关推荐