quota - linux磁盘配额管理

quota - linux磁盘配额管理,第1张

概述磁盘管理系列 linux磁盘管理系列一:磁盘配额管理   http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_040_quota.html linux磁盘管理系列二:软RAID的实现  http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_041_raid.html linux磁盘管理 磁盘管理系列

linux磁盘管理系列一:磁盘配额管理   http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_040_quota.html

linux磁盘管理系列二:软RAID的实现  http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_041_raid.html

linux磁盘管理系列三:LVM的使用        http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_042_lvm.html

1 前言

在linux系统中,由于是多用户、多任务的环境,如果有少数几个用户大量使用磁盘空间,导致其他用户的正常使用,因此需要对各个用户的磁盘空间进行管理和限定。

2 quota的用途

限制某一个用户的最大磁盘配额

3 quota的使用限制 仅能针对整个文件系统 内核必须支持 只对一般用户生效 这里提供一个样例,针对样例对quota的配置管理做个描述 4 案例讲解 4.1案例描述 创建5个用户user1,user2,user3,user4,user5,密码和用户名相同,初始组为usergrp组。 5个用户都可以取得300M的磁盘使用空间,文件数量不限。超过250M,给于提示。 usergrp这个组内成员最大使用空间1GB。 如果有用户超过soft限制,给14天的宽限时间。 4.2 准备磁盘

[[email protected] ~]# fdisk -l      #查看磁盘情况disk /dev/sda: 42.9 GB,42949672960 bytes,83886080 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesdisk label type: dosdisk IDentifIEr: 0x000bd275   Device Boot      Start         End      Blocks   ID  System/dev/sda1   *        2048     2099199     1048576   83  linux/dev/sda2         2099200    83886079    40893440   8e  linux LVMdisk /dev/sdb: 10.7 GB,10737418240 bytes,20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesdisk /dev/mapper/cl-root: 39.7 GB,39720058880 bytes,77578240 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesdisk /dev/mapper/cl-swap: 2147 MB,2147483648 bytes,4194304 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes[[email protected] ~]# fdisk /dev/sdb     #对sdb这个盘进行分区,这里就分一个区Welcome to fdisk (util-linux 2.23.2).Changes will remain in memory only,until you decIDe to write them.Be careful before using the write command.Device does not contain a recognized partition tableBuilding a new DOS disklabel with disk IDentifIEr 0xbcd17d69.Command (m for help): nPartition type:   p   primary (0 primary,0 extended,4 free)   e   extendedSelect (default p): pPartition number (1-4,default 1): 1First sector (2048-20971519,default 2048): Using default value 2048Last sector,+sectors or +size{K,M,G} (2048-20971519,default 20971519): Using default value 20971519Partition 1 of type linux and of size 10 GiB is setCommand (m for help): pdisk /dev/sdb: 10.7 GB,20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesdisk label type: dosdisk IDentifIEr: 0xbcd17d69   Device Boot      Start         End      Blocks   ID  System/dev/sdb1            2048    20971519    10484736   83  linuxCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.[[email protected] ~]# mkfs.ext4 /dev/sdb1mke2fs 1.42.9 (28-Dec-2013)filesystem label=OS type: linuxBlock size=4096 (log=2)Fragment size=4096 (log=2)StrIDe=0 blocks,Stripe wIDth=0 blocks655360 inodes,2621184 blocks131059 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=215167795280 block groups32768 blocks per group,32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks:     32768,98304,163840,229376,294912,819200,884736,1605632Allocating group tables: done                            Writing inode tables: done                            Creating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: done [[email protected] ~]# mkdir /mnt/home          #创建一个目录[[email protected] ~]# mount /dev/sdb1 /mnt/home   #测试挂载下

4.4.创建用户 

[[email protected] ~]# vim adduserbat.sh  #创建一个添加用户的脚本[[email protected] ~]# cat adduserbat.sh  #确认下脚本#!/bin/bashgroupadd usergrpfor user in user1 user2 user3 user4 user5do     useradd -g usergrp -b /mnt/home $user    echo $user |passwd --stdin $userdone[[email protected] ~]# sh adduserbat.sh    #运行脚本去创建用户useradd: warning: the home directory already exists.Not copying any file from skel directory into it.Creating mailBox file: file existsChanging password for user user1.passwd: all authentication tokens updated successfully.useradd: warning: the home directory already exists.Not copying any file from skel directory into it.Creating mailBox file: file existsChanging password for user user2.passwd: all authentication tokens updated successfully.useradd: warning: the home directory already exists.Not copying any file from skel directory into it.Creating mailBox file: file existsChanging password for user user3.passwd: all authentication tokens updated successfully.useradd: warning: the home directory already exists.Not copying any file from skel directory into it.Creating mailBox file: file existsChanging password for user user4.passwd: all authentication tokens updated successfully.useradd: warning: the home directory already exists.Not copying any file from skel directory into it.Creating mailBox file: file existsChanging password for user user5.passwd: all authentication tokens updated successfully.[[email protected] ~]# finger user1             #查看用户信息,确保家目录在/dev/sdb1的挂载目录上。Login: user1                      name: Directory: /mnt/home/user1              Shell: /bin/bashNever logged in.No mail.No Plan.[[email protected] ~]# ID user1              #查看用户信息uID=2531(user1) gID=2532(usergrp) groups=2532(usergrp)

4.5.检查 *** 作系统支持

前面提到了quota仅仅针对整个文件系统来进行规划的。需要确认我们为各个用户提供存储的位置是独立的文件系统。

[[email protected] ~]# df -h /mnt/home      #查看我们的挂载点是否是独立文件系统filesystem      Size  Used Avail Use% Mounted on/dev/sdb1       9.8G   37M  9.2G   1% /mnt/home[[email protected] ~]# mount |grep /mnt/home     #查看我们的文件系统/dev/sdb1 on /mnt/home type ext4 (rw,relatime,data=ordered)
4.6.让文件系统支持quota设置

[[email protected] ~]# mount -o remount,usrquota,grpquota /mnt/home   #重新挂载/mnt/home 支持usrquota,grpquota[[email protected] ~]# mount |grep /mnt/home                           #确认下/dev/sdb1 on /mnt/home type ext4 (rw,quota,grpquota,data=ordered)[[email protected] ~]# tail -n 1 /etc/mtab >> /etc/fstab         #追加到/etc/fstab中去,确保开机启用quota[[email protected] ~]# cat /etc/fstab                            #确保fstab文件正确性## /etc/fstab# Created by anaconda on Fri Feb 10 03:56:55 2017## 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 0UUID=dd4c6743-bdf5-4899-a43b-814cbe75c618 /boot                   xfs     defaults        0 0/dev/mapper/cl-swap     swap                    swap    defaults        0 0/dev/sr0 /mnt/cdrom iso9660 ro,uID=0,gID=0,iocharset=utf8,mode=0400,dmode=0500 0 0/dev/sdb1 /mnt/home ext4 rw,data=ordered 0 0

4.7.扫描文件系统并新建quota的配置文件

[[email protected] ~]# quotacheck -avugquotacheck: 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 [/mnt/home] donequotacheck: Cannot stat old user quota file /mnt/home/aquota.user: No such file or directory. Usage will not be subtracted.quotacheck: Cannot stat old group quota file /mnt/home/aquota.group: No such file or directory. Usage will not be subtracted.quotacheck: Cannot stat old user quota file /mnt/home/aquota.user: No such file or directory. Usage will not be subtracted.quotacheck: Cannot stat old group quota file /mnt/home/aquota.group: No such file or directory. Usage will not be subtracted.quotacheck: Checked 30 directorIEs and 20 filesquotacheck: old file not found.quotacheck: old file not found.

主要参数

-a:  扫描所有在/etc/mtab内含有quota参数的文件系统 -u:  针对用户扫描文件与目录的使用情况,会新建一个aquota.user文件 -g:  针对用户组扫描文件与目录的使用情况,会新增一个aquota.group文件 -v:  显示扫描过程的信息 4.8 启用quota
[[email protected] ~]# quotaon  -avug    #启用quota/dev/sdb1 [/mnt/home]: group quotas turned on/dev/sdb1 [/mnt/home]: user quotas turned on

 这个命令(quotaon) 几乎只需要在第一次启动quota时才需要进行,因为下次等你重新启动时,系统的/etc/rc.d/rc.sysinit这个初始化脚本就会自动执行这个命令。

如果想关闭可以使用quotaoff -avug

4.9.编辑账户的的限值
[[email protected] ~]# edquota -u user1

会打开一个vi编辑器,修改我们的设置如下图。

软限制: 这个值超过了基本上没事,还是可以创建文件继续使用文件,但是在指定grace天过后就不能在创建文件了。 硬限值: 这个值不能超过。

执行如下命令将user1的设置应用到其他用户上

[[email protected] ~]# edquota -p user1 -u user2    #-p 指定参考用户,这句话的意思就是将user1的quota信息赋值给user2[[email protected] ~]# edquota -p user1 -u user3[[email protected] ~]# edquota -p user1 -u user4[[email protected] ~]# edquota -p user1 -u user5
4.10.编辑组的设置
[[email protected] ~]# edquota -g usergrp

4.11.修改宽限时间
[[email protected] ~]# edquota -t

4.12.对用户和组合quota限制查看

[[email protected] ~]# quota -uvs user1       #查看user1的限制信息disk quotas for user user1 (uID 2531):      filesystem   space   quota   limit   grace   files   quota   limit   grace      /dev/sdb1     28K    245M    293M               7       0       0        [[email protected] ~]# quota -gvs usergrpdisk quotas for group usergrp (gID 2532): #查看usergrp的限制信息     filesystem   space   quota   limit   grace   files   quota   limit   grace      /dev/sdb1      0K    879M    977M               0       0       0        

参数说明

-u:  指定用户 -g:  指定用户组 -s:  以1024为倍数来指定单位,显示M之类的单位 -v:  显示用户在文件系统的quota值 4.13对文件系统quota限制查看

[[email protected] ~]# repquota -as*** Report for user quotas on device /dev/sdb1                   #这里看到是针对/dev/sdb1的文件系统的Block grace time: 14days; Inode grace time: 7days                        Space limits                file limitsUser            used    soft    hard  grace    used  soft  hard  grace----------------------------------------------------------------------root      --     20K      0K      0K              2     0     0       zhao      --     52K      0K      0K             13     0     0       user1     --     28K    245M    293M              7     0     0       user2     --     28K    245M    293M              7     0     0       user3     --     28K    245M    293M              7     0     0       user4     --     28K    245M    293M              7     0     0       user5     --     28K    245M    293M              7     0     0       

4.14.quota测试

[[email protected] ~]$ dd if=/dev/zero of=bigfile bs=1M count=270  #先创建一个270M的文件看看 sdb1: warning,user block quota exceeded.                   #这里提示警告了。 也就是我们超过了软限制的值250了。270+0 records in270+0 records out283115520 bytes (283 MB) copIEd,0.715086 s,396 MB/s[[email protected] ~]$ dd if=/dev/zero of=bigfile2 bs=1M count=40  #这里我们创建一个40M的文件sdb1: write Failed,user block limit reached.               #提示错误了。超出限制了。dd: error writing ‘bigfile2’: disk quota exceeded23+0 records in22+0 records out24035328 bytes (24 MB) copIEd,0.1165 s,206 MB/s[[email protected] ~]$ du -sk                                      #查看两个文件占用情况300000    .

4.12脚本设置quota信息

上面我们对用户和组的设置,它会启动一个vi编辑器,修改保存才生效。需要交互。如果我们想使用script方式快速设置,那就需要使用setquota命令了。

命令使用  setquota  [  -u  | -g ] 用户名或者组名 块大小软限制 块大小硬限制  文件数量软限制 文件数量大小硬限制 文件系统

[[email protected] ~]# quota -usv user1     #查看user1的quota信息disk quotas for user user1 (uID 2531):      filesystem   space   quota   limit   grace   files   quota   limit   grace      /dev/sdb1    293M*   245M    293M  13days      14       0       0        [[email protected] ~]# setquota -u user1 400000 500000 100 200 /dev/sdb1    #使用setquota修改[[email protected] ~]# quota -usv user1                                     #再次查看quota信息disk quotas for user user1 (uID 2531):      filesystem   space   quota   limit   grace   files   quota   limit   grace      /dev/sdb1    293M    391M    489M              14     100     200        

 

时间:2019-07-01 01:09:21 阅读(14) 总结

以上是内存溢出为你收集整理的quota - linux磁盘配额管理全部内容,希望文章能够帮你解决quota - linux磁盘配额管理所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1020480.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存