Linux Admin教程

Linux Admin 配额管理

CentOS 磁盘配额可以同时启用;在超出磁盘容量之前提醒系统管理员并拒绝用户进一步访问磁盘存储。当磁盘已满时,取决于重新在磁盘的两侧,整个系统可能会突然停止,直到恢复。
在 CentOS Linux 中启用配额管理基本上是一个 4 步过程-
第 1 步-在/etc/fstab 中为组和用户启用配额管理。 第 2 步-重新挂载文件系统。 第 3 步-创建配额数据库并生成磁盘使用表。 第 4 步-分配配额政策。

在/etc/fstab 中启用配额管理

首先,我们要备份我们的/etc/fstab 文件-
[root@centosLocal centos]# cp-r /etc/fstab ./
我们现在在当前工作目录中有一个 已知工作/etc/fstab 的副本。
# 
# /etc/fstab 
# Created by anaconda on Sat Dec 17 02:44:51 2016 
# 
# Accessible filesystems, by reference, are maintained under '/dev/disk' 
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
# 
/dev/mapper/cl-root        /         xfs     defaults                      0 0
UUID = 4b9a40bc-9480-4     /boot     xfs     defaults                      0 0
/dev/mapper/cl-home        /home     xfs     defaults,usrquota,grpquota    0 0
/dev/mapper/cl-swap        swap      swap    defaults                      0 0
我们在 /etc/fstab 的选项部分对卷或标签进行了以下更改,以便为用户和组应用配额。
usrquota grpquota
如您所见,我们使用的是 xfs 文件系统。使用 xfs 时,涉及额外的手动步骤。 /home 与/在同一个磁盘上。进一步调查显示/设置为 noquota,这是一个内核级安装选项。我们必须重新配置我们的内核启动选项。
root@localhost rdc]# mount | grep ' / ' 
/dev/mapper/cl-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@localhost rdc]#

为 XFS 文件系统重新配置内核启动选项

此步骤仅在两种情况下才需要-
当我们启用配额的磁盘/分区使用 xfs 文件系统时 当内核在启动时将 noquota 参数传递给/etc/fstab 时
第 1 步-备份/etc/default/grub。
cp /etc/default/grub ~/
第 2 步-修改 /etc/default/grub
这是默认文件。
GRUB_TIMEOUT=5 
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" 
GRUB_DEFAULT=saved 
GRUB_DISABLE_SUBMENU=true 
GRUB_TERMINAL_OUTPUT="console" 
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet" 
GRUB_DISABLE_RECOVERY="true"
我们要修改以下行-
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet" 
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=cl/root rd.lvm.lv 
=cl/swap rhgb quiet rootflags=usrquota,grpquota"
注意-我们逐字复制这些更改很重要。重新配置 grub.cfg 后,如果配置中出现任何错误,我们的系统将无法启动。请在非生产系统上尝试本教程的这一部分。
第 3 步-备份您的工作 grub.cfg
cp /boot/grub2/grub.cfg /boot/grub2/grub.cfg.bak
创建一个新的 grub.cfg
[root@localhost rdc]# grub2-mkconfig-o /boot/grub2/grub.cfg 
Generating grub configuration file ... 
Found linux image: /boot/vmlinuz-3.10.0-514.el7.x86_64 
Found initrd image: /boot/initramfs-3.10.0-514.el7.x86_64.img 
Found linux image: /boot/vmlinuz-0-rescue-dbba7fa47f73457b96628ba8f3959bfd 
Found initrd image: /boot/initramfs-0-rescuedbba7fa47f73457b96628ba8f3959bfd.img 
done
[root@localhost rdc]#
重启
[root@localhost rdc]#reboot
如果所有修改都是精确的,我们就不应该可以向 xfs 文件系统添加配额。
[rdc@localhost ~]$ mount | grep ' / ' 
/dev/mapper/cl-root on / type xfs (rw,relatime,seclabel,attr2,inode64,usrquota,grpquota)
 
[rdc@localhost ~]$
我们已经通过 grub 传递了 usrquotagrpquota 参数。
现在,再次编辑 /etc/fstab 以包含/因为 /homeon 相同的物理磁盘。
/dev/mapper/cl-root/xfs
defaults,usrquota,grpquota        0 0
现在让我们启用配额数据库。
[root@localhost rdc]# quotacheck-acfvugM
确保启用配额。
[root@localhost rdc]# quotaon-ap 
group quota on / (/dev/mapper/cl-root) is on 
user quota on / (/dev/mapper/cl-root) is on 
group quota on /home (/dev/mapper/cl-home) is on 
user quota on /home (/dev/mapper/cl-home) is on 
[root@localhost rdc]#

重新挂载文件系统

如果分区或磁盘与主动启动的分区是分开的,我们可以在不重启的情况下重新挂载。如果在根目录/中启动的磁盘/分区上配置了配额,我们可能需要重新启动操作系统。强制重新挂载和应用更改,重新挂载文件系统的需要可能会有所不同。
[rdc@localhost ~]$ df 
Filesystem              1K-blocks     Used      Available      Use%     Mounted on
/dev/mapper/cl-root     22447404      4081860   18365544       19%         /
devtmpfs                903448        0         903448         0%          /dev
tmpfs                   919308        100       919208         1%          /dev/shm
tmpfs                   919308        9180      910128         1%          /run
tmpfs                   919308        0         919308         0%          /sys/fs/cgroup
/dev/sda2               1268736       176612    1092124        14%         /boot
/dev/mapper/cl-var      4872192       158024    4714168        4%          /var
/dev/mapper/cl-home     18475008      37284     18437724       1%          /home
tmpfs                   183864        8         183856         1%          /run/user/1000
[rdc@localhost ~]$
如我们所见,LVM 卷正在使用中。所以重启很简单。这将重新挂载 /home 并将 /etc/fstab 配置更改加载到活动配置中。

创建配额数据库文件

CentOS 现在能够处理/home 上的磁盘配额。要启用完全配额支持,我们必须运行 quotacheck 命令。
quotacheck 将创建两个文件-
aquota.user aquota.group
这些用于存储启用配额的磁盘/分区的配额信息。
以下是常见的配额检查Switch。
Switch Action
-u 检查用户配额
-g 检查组配额
-c 应该为每个具有启用配额的文件系统启用配额
-v 显示详细输出

为每个用户添加配额限制

为此,我们将使用 edquota 命令,后跟用户名-
[root@localhost rdc]# edquota centos
Disk quotas for user centos (uid 1000):  
Filesystem                   blocks       soft       hard     inodes     soft     hard 
/dev/mapper/cl-root              12          0          0         13        0        0  
/dev/mapper/cl-home            4084          0          0        140        0        0
让我们看看每一列。
Filesystem-应用到的用户的文件系统配额 blocks-用户当前在每个文件系统上使用的块数 soft-为软限制设置块。软限制允许用户在给定的时间段内携带配额 hard-为硬限制设置块。硬限制是允许的总配额 inode-用户当前使用的 inode 数量 soft-软 inode 限制 hard -硬 inode 限制
以用户身份检查我们当前的配额-
[centos@localhost ~]$ quota 
Disk quotas for user centos (uid 1000):  
Filesystem             blocks     quota      limit grace    files   quota   limit   grace 
/dev/mapper/cl-home    6052604    56123456   61234568       475     0       0       [centos@localhost ~]$
以下是超出硬配额限制时给用户的错误。
[centos@localhost Downloads]$ cp CentOS-7-x86_64-LiveKDE-1611.iso.part ../Desktop/
cp: cannot create regular file ‘../Desktop/CentOS-7-x86_64-LiveKDE-
1611.iso.part’: Disk quota exceeded
[centos@localhost Downloads]$
如我们所见,我们非常接近该用户的磁盘配额。让我们设置一个软限制警告。这样,用户将在配额限制到期之前提前收到通知。根据经验,您会在最终用户开始工作时收到他们的投诉,并且需要花费 45 分钟清理文件才能真正开始工作。
作为管理员,我们可以使用 repquota 命令检查配额使用情况。
[root@localhost Downloads]# repquota  /home  
                    Block limits                            File limits  
User            used     soft     hard     grace       used     soft     hard     grace 
----------------------------------------------------------------------------------------
root     --      0         0        0                   3        0        0        
centos   -+ 6189824  56123456 61234568                 541      520      540      6days 
[root@localhost Downloads]#
如我们所见,用户 centos 已超出其硬块配额,无法再使用 /home 上的任何磁盘空间。
-+ 表示已超出文件系统上的硬配额。
在计划配额时,有必要做一点数学计算。管理员需要知道的是:系统上有多少用户?在用户/组之间分配多少可用空间?文件系统上的块由多少字节组成?
根据与可用磁盘空间相关的块定义配额。建议在文件系统上保留一个"安全"的可用空间缓冲区,在最坏的情况下将保留:同时超出所有配额。尤其是在系统用于写入日志的分区上。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4