liunx磁盘配额管理

author author     2022-08-01     419

关键词:

    作为运维人员,很多时候我们需要对用户使用磁盘的空间进行限定,比如用户家目录的空间使用限制,邮箱空间使用限制,共享磁盘空间的使用限制,甚至说云盘空间,都需要给每个用户做相应的空间使用限制。否则,服务器磁盘空间就无法进行管理了。要想使用配额首先必须明白两个问题,第一,就是系统内核必须支持配额功能;第二,就是配额只能作用于磁盘分区上;所以我们要想使用配额,就必须先行准备这两大条件。


一、环境检查

  第一步:检查当前系统是否支持配额功能:

    grep CONFIG_QUOTA /boot/config-[kernel-version]

    

    如果结果如下:

    CONFIG_QUOTA=y

    CONFIG_QUOTACTL=y

    恭喜你!当前的内核支持配额功用!如果出现的不是这个结果的话那必须对内核进行重新编译了,让内核启用配额功能

    示例代码:

    [[email protected] boot]# grep CONFIG_QUOTA /boot/config-3.10.0-327.el7.x86_64 

    CONFIG_QUOTA=y

    CONFIG_QUOTA_NETLINK_INTERFACE=y

    # CONFIG_QUOTA_DEBUG is not set

    CONFIG_QUOTA_TREE=y

    CONFIG_QUOTACTL=y

    CONFIG_QUOTACTL_COMPAT=y

    [[email protected] boot]# 


  第二步:准备需要启用配额功能的磁盘分区

  如果是想对用户的家目录做配额,就必须对用户家目录做单独分区,如果安装系统的时候没有做单独分区,可以重建分区,迁移家目录(具体操作在我的上一篇博文中有详解)

  如果想对用户的邮箱启用配额,就必须对用户存储邮件的目录做单独分区,共享磁盘以及其它使用场景同样


    环境展示:

    [[email protected] home]# lsblk

    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

    sda      8:0    0   20G  0 disk 

    ├─sda1   8:1    0  200M  0 part /boot

    ├─sda2   8:2    0    5G  0 part /

    └─sda3   8:3    0    5G  0 part /test

    sdb      8:16   0   20G  0 disk 

    └─sdb1   8:17   0   20G  0 part /home

    sr0     11:0    1  7.2G  0 rom  

    [[email protected] home]# 

 

  我的家目录目前已经是独立硬盘独立分区,所以我就直接用家目录分区开始演示配额操作了


二、配置用户家目录分区的磁盘配额功能

  第一步:修改文件系统自动挂载配置文件/etc/fstab,修改如下一行,在默认挂载选项后面添加usrquota,grpquota两个挂载选项(多选项之间用,分隔开),启用用户家目录分区的磁盘配额功能

     /dev/sdb1 /home ext4 defaults,usrquota,grpquota 0 0

  第二步:重新挂载一下家目录的独立分区,让刚刚配置的配额功能启用,免去了重启生效的麻烦(曾经看过一些教程,都写的是配置完重启,试想一下,实际生产环境中的服务器允许你随便重启吗)

    [[email protected] test]# mount -o remount /dev/sdb1

    [[email protected] test]# mount

    /dev/sda2 on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)

    /dev/sda3 on /test type ext4 (rw,relatime,seclabel,data=ordered)

    /dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)

    tmpfs on /run/user/42 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=186872k,mode=700,uid=42,gid=42)

    tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=186872k,mode=700)

    /etc/auto.misc on /misc type autofs (rw,relatime,fd=6,pgrp=8821,timeout=300,minproto=5,maxproto=5,indirect)

    -hosts on /net type autofs (rw,relatime,fd=12,pgrp=8821,timeout=300,minproto=5,maxproto=5,indirect)

    /dev/sdb1 on /home type ext4 (rw,relatime,seclabel,quota,usrquota,grpquota,data=ordered)

    [[email protected] test]# 

    当mount返回值中sdb1分区的挂载选项中有quota,usrquota,grpquota三个选项时,说明配额功能已经生效

    

    quotacheck:创建磁盘配额配置文件

    quotacheck [options] filesystem

        -v:显示扫描过程;

        -u:针对扫描情况与目录的使用情况建立aquota.user配置文件

        -g:针对用户扫描文件与使用情况建立aquota.group配置文件

         -a:扫描任何在/etc/mtab中开启quota的文档系统

    

  第三步:使用quotacheck生成aquota.user,aquota.group两个配置文件

    [[email protected] test]# quotacheck -vug /dev/sdb1

    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/sdb1 [/home] done

    quotacheck: Cannot stat old user quota file /home/aquota.user: No such file or directory. Usage will not be subtracted.

    quotacheck: Cannot stat old group quota file /home/aquota.group: No such file or directory. Usage will not be subtracted.

    quotacheck: Cannot stat old user quota file /home/aquota.user: No such file or directory. Usage will not be subtracted.

    quotacheck: Cannot stat old group quota file /home/aquota.group: No such file or directory. Usage will not be subtracted.

    quotacheck: Checked 30 directories and 21 files

    quotacheck: Old file not found.

    quotacheck: Old file not found.

    以上报错不必理会,我的sdb1分区是ext4的文件系统,但挂载选项中并没有启用日志功能,这是第一处报错内容,下面是在将要启用配额的分区的一级目录下没有找到aquota.user和aquota.group这两个配置文件,从来都没启用过配额,当然找不到了,所以这些报错,不会影响我们最终效果。只要运行quotacheck命令后,能在sdb1目录下生成这aquota.user和aquota.group这两个配置文件,就可以继续配置

    [[email protected] test]# ll /home

    total 44

    -rw-------. 1 root   root  7168 Aug 27 22:31 aquota.group

    -rw-------. 1 root   root  7168 Aug 27 22:31 aquota.user

    drwx------. 5 hacker test  4096 Aug 27 19:42 hacker

    drwx------. 2 root   root 16384 Aug 27 20:03 lost+found

    drwx------. 5 share  test  4096 Aug 27 19:43 share

    drwx------. 6 test   test  4096 Aug 26 15:31 test

    [[email protected] test]# 

    

  第四步:使用edquota命令设置具体用户的具体配额参数

    

    edquota:设定磁盘配额具体参数

        -u: 编辑user的quota

        -g: 编辑group的quota

        -t: 设定超过限定的保留时间(用户和组都有效)

        -p: 复制指定用户的quota资料到另一用户上

        -T:修改指定用户或组的宽限时间

    

    1、设定test和hacker用户使用空间限定模式

    [[email protected] test]# edquota -u test

    Disk quotas for user test (uid 1000):

    Filesystem           blocks       soft       hard     inodes     soft     hard

    /dev/sdb1                88     409600     512000         22        0        0

    [[email protected] test]#

    

    选项释义:

    blocks :用户test的家目录(在/dev/sdb1)已使用的空间,单位:KB (系统生成)

    soft :soft limit 磁盘空间警告值 单位:KB (需要设定)——超过该值发警告消息

    hard :hard limit 磁盘空间限定值 单位:KB (需要设定)——超过该值拒绝写入数据

    inodes :用户test的家目录已使用的节点数,由于inode控制不方便,所以不要修改它

    soft :soft limit 节点数警告值 (根据需要修改)——超过该值发警告消息

    hard :hard limit 节点数限定值 (根据需要修改)——超过该值拒绝新建文件

    

    soft limit :软限制,在宽限期(grace period)之内,使用空间可以超过soft limit,但必须在宽限期之内将使用容量降低到soft limit以下

    hard limit :硬限制,当用户使用空间超过hard limit时,将无法再写入数据

    grace period:宽限期,使用容量超过soft limit,宽限时间自动启动,使用者将容量降低到soft limit以下,宽限时间自动关闭,假如使用者没有在宽限时间内将容量降低到soft limit,那么他将无法再写入数据,即使使用容量没有到达hard limit

    

    复制test用户的配额参数给hacker

    [[email protected] test]# edquota -p test hacker

    [[email protected] test]# edquota -u hacker

    Disk quotas for user hacker (uid 1001):

    Filesystem           blocks       soft       hard     inodes     soft     hard

    /dev/sdb1                56     409600     512000         14        0        0

    [[email protected] test]#

    

    2、设定share用户使用节点数限定模式

    [[email protected] test]# edquota -u share

    Disk quotas for user share (uid 1002):

    Filesystem            blocks       soft       hard     inodes     soft     hard

    /dev/sdb1                 52          0          0         13       18       20

    [[email protected] test

    

    3、对用户组设定磁盘配额

    有时候对单个用户设备磁盘配额不好控制,这时就可以把同一个部门的用户放在同一个组里面,然后对组进行额磁盘配额

    [[email protected] test]# edquota -g test

         Disk quotas for group test (gid 1000):

          Filesystem         blocks       soft       hard     inodes     soft     hard

          /dev/sdb1             204    4096000    5120000         56        0        0

    和用户配置没什么区别,只是作用对象是组而已,至于每个用户能用多少空间,只要所有的用户使用的空间总计不大于hard的值时,所剩余的空间任何用户都可以使用。至于每个组需要分配多大的硬盘空间,你可以直接去咨询一下他们的部门领导。这样以来就可以很好的解决这个用户磁盘空间管理的问题了

    

    3、设定超出软限制的保留期限(用户组的默认值都是7天)

    注意:所以超过软限制的部分(space、inode都一样有效),超过宽限时间都会被自动删除,所以设置一个合理的宽限期限也是很有必须。以免造成用户不必须的数据损失

    [[email protected] test]# edquota -t

    

           Grace period before enforcing soft limits for users:

           Time units may be: days, hours, minutes, or seconds

             Filesystem             Block grace period     Inode grace period

             /dev/sdb1                     7days                  7days


三、验证配额使用效果

  第一步、用户配额参数高额完毕,正式开启home目录独立分区的磁盘配额功能

    [[email protected] test]# quotaon -vug /dev/sdb1

    quotaon: using /home/aquota.group on /dev/sdb1 [/home]: Device or resource busy

    quotaon: using /home/aquota.user on /dev/sdb1 [/home]: Device or resource busy

    [[email protected] test

    有报错,不用担心,因为home目录是sdb1分区的挂载点,所以会提示设备资源繁忙


  第二步、登录进做过配额的用户的家目录下,测试配额警告与限定


    1、首先测试使用空间限定模式的情况

    [[email protected] test]# su - test

    Last login: Sat Aug 27 20:32:53 CST 2016 on pts/0

    [[email protected] ~]$ du -h

    8.0K./.cache/abrt

    12K./.cache

    16K./.local/share/lftp

    20K./.local/share

    24K./.local

    4.0K./.config/lftp

    4.0K./.config/abrt

    12K./.config

    4.0K./.mozilla/extensions

    4.0K./.mozilla/plugins

    12K./.mozilla

    88K.

    [[email protected] ~]$ dd if=/dev/zero of=test1 bs=1M count=399

    399+0 records in

    399+0 records out

    418381824 bytes (418 MB) copied, 0.75313 s, 556 MB/s

    [[email protected] ~]$ du -sh

    400M.

    [[email protected] ~]$

    没有超过警告值,所以没有发出警告消息

    

    [[email protected] ~]$ dd if=/dev/zero of=test2 bs=1M count=1

    sdb1: warning, user block quota exceeded.

    1+0 records in

    1+0 records out

    1048576 bytes (1.0 MB) copied, 0.0110259 s, 95.1 MB/s

    [[email protected] ~]$

    在警告值400M的基础上多出了1M,警告消息立即触发

    

    [[email protected] ~]$ du -sh

    401M.

    [[email protected] ~]$ dd if=/dev/zero of=test3 bs=1M count=100

    sdb1: write failed, user block limit reached.

    dd: error writing ‘test3’: Disk quota exceeded

    100+0 records in

    99+0 records out

    104767488 bytes (105 MB) copied, 0.414018 s, 253 MB/s

    [[email protected] ~]$ du -h

    8.0K./.cache/abrt

    12K./.cache

    16K./.local/share/lftp

    20K./.local/share

    24K./.local

    4.0K./.config/lftp

    4.0K./.config/abrt

    12K./.config

    4.0K./.mozilla/extensions

    4.0K./.mozilla/plugins

    12K./.mozilla

    500M.

    [[email protected] ~]$ dd if=/dev/zero of=test4 bs=1M count=1

    dd: error writing ‘test4’: Disk quota exceeded

    1+0 records in

    0+0 records out

    0 bytes (0 B) copied, 0.00184169 s, 0.0 kB/s

    [[email protected] ~]$ 

    忽略警告,继续写入数据,只要没有超过限定值500M,是可以继续写入的,一但达到限定值,就无法写入超出限定值的数据了

    

    2、测试使用节点数限定模式的情况

    [[email protected] ~]$ quota -su share

    Disk quotas for user share (uid 1002): 

         Filesystem   space   quota   limit   grace   files   quota   limit   grace

          /dev/sdb1     52K      0K      0K              13      18      20        

    [[email protected] ~]$

    

    查看磁盘配额使用情况的选项释义:

    space:用户已使用的磁盘空间

    quota:soft limit(软限制)用户设定的磁盘空间警告值

    limit:hard limit(硬限制)用户设定的磁盘空间限定值

    grace:grace period磁盘空间超出警告部分的保留天数(默认7天)

    files:用户已创建的节点数

    quota:soft limit(软限制)用户设定的节点数的警告值

    limit:hard limit(硬限制)用户设定的节点数的限定值

    grace:grace period节点数超出警告部分的保留天数(默认7天)

    

    [[email protected] ~]$ touch file{1..5}

    [[email protected] ~]$ ll

    total 0

    -rw-r--r--. 1 share test 0 Aug 28 12:40 file1

    -rw-r--r--. 1 share test 0 Aug 28 12:40 file2

    -rw-r--r--. 1 share test 0 Aug 28 12:40 file3

    -rw-r--r--. 1 share test 0 Aug 28 12:40 file4

    -rw-r--r--. 1 share test 0 Aug 28 12:40 file5

    新建了5个文件,也即增加了5个节点,还没有超过Inodes的警告值18,所以没有发出警告信息

    

    [[email protected] ~]$ touch file6

    sdb1: warning, user file quota exceeded.

    [[email protected] ~]$ ll

    total 0

    -rw-r--r--. 1 share test 0 Aug 28 12:40 file1

    -rw-r--r--. 1 share test 0 Aug 28 12:40 file2

    -rw-r--r--. 1 share test 0 Aug 28 12:40 file3

    -rw-r--r--. 1 share test 0 Aug 28 12:40 file4

    -rw-r--r--. 1 share test 0 Aug 28 12:40 file5

    -rw-r--r--. 1 share test 0 Aug 28 12:41 file6

    [[email protected] ~]$ quota -su share

    Disk quotas for user share (uid 1002): 

         Filesystem   space   quota   limit   grace   files   quota   limit   grace

          /dev/sdb1     52K      0K      0K              19*     18      20   6days

    [[email protected] ~]$

    又增加了一个节点,刚好超过了inode的警告值18,立即触发了警告消息

    

    [[email protected] ~]$ touch file{7..8}

    sdb1: write failed, user file limit reached.

    touch: cannot touch ‘file8’: Disk quota exceeded

    [[email protected] ~]$ ll

    total 0

    -rw-r--r--. 1 share test 0 Aug 28 12:40 file1

    -rw-r--r--. 1 share test 0 Aug 28 12:40 file2

    -rw-r--r--. 1 share test 0 Aug 28 12:40 file3

    -rw-r--r--. 1 share test 0 Aug 28 12:40 file4

    -rw-r--r--. 1 share test 0 Aug 28 12:40 file5

    -rw-r--r--. 1 share test 0 Aug 28 12:44 file6

    -rw-r--r--. 1 share test 0 Aug 28 12:58 file7

    [[email protected] ~]$ quota -su share

    Disk quotas for user share (uid 1002): 

         Filesystem   space   quota   limit   grace   files   quota   limit   grace

          /dev/sdb1     52K      0K      0K              20*     18      20   6days

    [[email protected] ~]$ 

        忽略警告,继续添加节点,只要没有超过Inode限定值20,是可以继续添加的,一但超过Inode限定值,就无法继续增加节点了(等同于无法创建文件了)

    

    3、验证节点数超出配额,能否使用磁盘空间

    [[email protected] ~]$ echo test >> file1

    [[email protected] ~]$ cat file1

    test

    [[email protected] ~]$ 

    Inode不能再增加了,但空间使用没有限制,所以还可以使用硬盘空间(增加原有文件的内容


四、查询磁盘配额使用情况

  quota [options] username|groupname

      -u:查询用户的配额使用详情

      -g:查询用户组的配额使用详情

      -q:简洁方式显示查询信息

      -v:显示详细查询信息

      -s:显示单位换算后的结果


    1、查询用户的磁盘配额使用详情

    [[email protected] test]# quota -u test share

    Disk quotas for user test (uid 1000): 

    Filesystem  blocks   quota   limit   grace   files   quota   limit   grace

    /dev/sdb1      88  409600  512000              22       0       0        

    Disk quotas for user share (uid 1002): 

    Filesystem  blocks   quota   limit   grace   files   quota   limit   grace

    /dev/sdb1      60       0       0              20*     18      20   6days

    [[email protected] test]# 

    

    2、查询用户组的磁盘配额使用详情

    [[email protected] test]# quota -g test

    Disk quotas for group test (gid 1000): 

    Filesystem  blocks   quota   limit   grace   files   quota   limit   grace

    /dev/sdb1     204  4096000 5120000              56       0       0 

    [[email protected] test]# 


五、关闭磁盘配额功能

    1、关闭单个磁盘的磁盘配额

    quotaoff /dev/sd#

    

        关闭所有开启的磁盘配额的分区

        quotaoff -a

    

    2、如果不打算再次启用磁盘配额功能,可以将配置文件一并删除

    rm -rf /mount_point/aquota.*

    

    3、最后删除/etc/fstab中的自动挂载选项中的(usrquota,grpquota)两项即可


关于磁盘配额的问题,就给大家分享到这里,不足之处,还望高人指点。

本文出自 “爱情防火墙” 博客,请务必保留此出处http://183530300.blog.51cto.com/894387/1843546

配额管理

...(softlimit)和硬限制(hardlimit)5)配额是计算机中指定磁盘的储存限制,就是管理员可以为用户所能使用的磁盘空间进行配额限制,每一用户只能使用最大配额范围内的磁盘空间。实现配额的前提1)独立分区上的文件系统,不... 查看详情

磁盘配额,raid和lvm管理(代码片段)

一、磁盘配额1.磁盘配额的作用??磁盘配额就限制用户在该目录中使用空间的大小和限制用户上传文件的数量(也就是inode号)。2.举例在创建磁盘配额时,需要关闭selinux[[email protected]~]#vim/etc/selinux/config#ThisfilecontrolsthestateofSEL... 查看详情

磁盘配额

磁盘配额就是管理员可以为用户所能使用的磁盘空间进行配额限制,每一用户只能使用最大配额范围内的磁盘空间。功能作用编辑磁盘配额可以限制指定账户能够使用的磁盘空间,这样可以避免因某个用户的过度使用磁盘空间造... 查看详情

centos磁盘管理及配额管理

...ext4/xfs,开机自动挂载到系统mnt1,mnt2目录下。三.lv02开启磁盘配额功能,用来进行用户与组分配额的实验。四.在系统里添加用户tom,lisa并加入caiwu组中;五.对组和用户同时设置磁盘配额,分别切换至用户目录下写文件,验证生效... 查看详情

linux入门之磁盘管理磁盘配额

Linux入门之磁盘管理(6)磁盘配额在linux中使用向磁盘中存入数据时,如果有多个用户对同个磁盘进行操作,其中一个用户放了很大数据的文件导致硬盘容量用光,那么其他用户将无法再存入数据,这时就需要对每个用户进行存入... 查看详情

逻辑卷管理和磁盘配额

逻辑卷管理和磁盘配额实验下面为大家介绍逻辑卷的管理和磁盘配额实验的操作步骤,首先在虚拟机上创建两块磁盘,在linux桌面右下角光驱的位置右键点击设置,点击添加选择硬盘,点击下一步点击选择推荐的SCSI类型,点击下... 查看详情

磁盘配额-quota

本文内容实现从lvm创建文件系统到实现磁盘配额目的。一、LVM利用两块20G的硬盘Lvm的管理命令功能物理卷管理卷组管理逻辑卷管理Scan扫描pvscanvgscanlvscanCreate建立pvcreatevgcreatelvcreateDisplay显示pvdisplayvgdisplaylvdisplayRemove删除pvremovevgrem... 查看详情

磁盘管理之磁盘配额

  不管磁盘空间有多么大,磁盘空间不够使用似乎是一个永远的事实。加之Linux系统在默认情况下,并不限制每个用户使用磁盘空间的大小,如果某个用户疏忽或恶意将磁盘占满,将导致系统无法进行写操作甚至崩溃。当... 查看详情

逻辑卷创建与磁盘配额管理

逻辑卷创建与磁盘配额管理  环境要求:在虚拟机新建两块硬盘用来创建逻辑卷,并对其进行磁盘配额管理模拟。1.创建逻辑卷先在虚拟机设置中添加两块10G的新硬盘,并重启虚拟机使新添加的硬盘生效  输入fdisk... 查看详情

磁盘配额和文件系统管理实验

磁盘配额和文件系统管理实验在虚拟机中创建两个硬盘,步骤如下:剩下的一块硬盘如上继续创建,创建完后将会显示有二个新硬盘点击确定:在虚拟机桌面鼠标右击选‘在终端中打开’输入‘init6’重启虚拟机,重启后的硬盘... 查看详情

高级文件系统管理磁盘配额,raid

磁盘配额    quota命令显示磁盘使用情况和限额。quota命令报告文件中列出的所有文件系统的限额。如果quota命令以非零状态退出,一个或多个文件系统超过限额。1、ext系列文件系统的quota的步骤方式1.1、开启quota(e... 查看详情

lvm的管理;磁盘配额(代码片段)

setenforce0一、lvm的管理lvm的作用:动态管理磁盘,增强磁盘管理的灵活性。1、物理卷物理卷是由磁盘或分区转换得来的,由一个一个pe组成(每个pe默认大小为4MB)。分区的类型必须修改为8e创建pv:格式:pvcreate分区1[分区2分区3... 查看详情

redhatenterprise6.5磁盘分区,lvm管理及磁盘配额设置

Linux磁盘分区、LVM管理及磁盘配额设置第一部分:磁盘分区为虚拟机添加一块新的磁盘,重启生效。管理新添加的sdb磁盘。为其创建不同类型的分区,ext4、fat、及swap。2.1创建第一个主分区2.2创建第二个主分区,方法相同。2.3将... 查看详情

怎么设置磁盘配额

题目是这样为新建拥护Userpy分配磁盘配额为50M,并加人到管理组中。。Linux跟Windos各是怎么做的求高人。。可以加好友。并找师傅。不嫌弃的来注意是Linux谢谢!!!不要什么工具。没有网络可以下载什么工具的。。1、方法如下... 查看详情

lvm逻辑卷轴管理和磁盘配额实验

动态卷轴管理和磁盘配额实验首先先在虚拟机中开启两个硬盘,我在rh6-2中新添加了两个硬盘,分别为sdbc和sdbd:按照如上方法,再添加1个硬盘。则添加的磁盘如下所示:第一步:分区接下来对sdc分区(sdc只分配了一个主分区)... 查看详情

磁盘配额

        磁盘配额就是管理员可以为用户所能使用的磁盘空间进行配额限制,每一用户只能使用最大配额范围内的磁盘空间。        磁盘配额可以限制指定账户能够使用的... 查看详情

逻辑卷管理和磁盘配额(代码片段)

逻辑卷管理和磁盘配额首先在虚拟机上添加两块硬盘,init6重启识别后,我们对它们进行分区,输出命令后会出现“Command(mforhelp)”,在里面进行分区设置然后继续对/dev/sdc进行分区,步骤一样,分区完成后fdisk–l查看下分区成功... 查看详情

linux操作系统中lvm磁盘配额

....LVM概述 2. LVM机制3.LVM管理命令4. 盘配额概述5.磁盘配额设置步骤        1.LVM概述   LVM(LogicalVolumeManager)逻辑卷管理能够在保持现有数据不变的情况下动态调整磁盘容量,从而提高... 查看详情