Linux user disk quota setting method

  • 2020-05-15 03:17:16
  • OfStack

1: QUOTA is supported in the kernel:


[root@localhost /]# grep CONFIG_QUOTA /boot/config-3.10.0-123.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

If there is output from the above column, quota is supported by the current kernel.

2: modified /etc/fstab to add QUOTA support:

[root@localhost /]# vim /etc/fstab
/dev/sdb1 /dvd xfs defaults,usrquota,grpquota 1 2

3: initialize QUOTA database using quotacheck:

Since quotacheck searches the file system based on /etc/mtab, you need to re-mount the newly modified /dev/sdb1


[root@localhost /]# mount /dvd -o remount    
[root@localhost /]# mount
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=926912k,nr_inodes=231728,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (rw,nosuid,nodev,noexec,seclabel,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/net_cls type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda3 on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=32,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
sunrpc on /proc/fs/nfsd type nfsd (rw,relatime)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
/dev/sdb1 on /dvd type ext2 (rw,relatime,seclabel,quota,usrquota,grpquota)
[root@localhost /]# quotacheck -avug
quotacheck: Quota for users is enabled on mountpoint /dvd so quotacheck might damage the file.
Please turn quotas off or use -f to force checking.
-a :  Scan all in /etc/mtab In the open quota File system 
-v :  Display scanning process 
-u :  Scan all user quotas  (usrquota)
-g :  Scan all group quotas (grpquota)

4: start QUOTA (start quotaon, close quotaoff)


[root@localhost /]# quotaon -avug
/dev/sdb1 [/dvd]: group quotas turned on
/dev/sdb1 [/dvd]: user quotas turned on
-a :  Open all quota Set, according to /etc/mtab set 
-v :  When open quota Display information when 

5: edit user disk quota:

The maximum capacity of user quota is set to 20M. When using 10M, the warning requirement will be reduced to 10M, which can create up to 10 files. When creating 5 files, the warning requirement will be reduced to 5 files


[root@localhost /]# edquota -u redhat ( redhat Is the system user name) 
Disk quotas for user redhat (uid 1000):
 Filesystem          blocks    soft    hard   inodes   soft   hard
 /dev/sdb1             0       0     0      0       0    0

Instead of


Disk quotas for user redhat (uid 1000):
 Filesystem          blocks    soft    hard   inodes   soft   hard
 /dev/sdb1             0     10240   20480    0     5     10

-u: edit quota of user
-g: edit quota for group
-t: editor's grace
-p: copy quota data to another user

blocks: space currently used by the user (quota:uid=5011) in /dev/hda7, unit :KB (not modified)

soft :soft limit disk space limit unit: KB (to be set)
hard :hard limit disk space limit unit: KB (to be set)

inodes: the inodes used by current users. Do not modify it because it is not easily controlled by inode.

soft :soft limit file limit number (modified as needed)
hard :hard limit file limit number (modified as needed)

soft limit: minimum capacity, within the grace period (grace period), can exceed soft limit, but must be reduced below soft limit within the grace period.

hard limit: final limit of capacity, if the user continues to write data during the grace period, hard limit will not be able to write again.

Grace time: when the usage capacity exceeds soft limit, the grace time will start automatically, and the user will reduce the capacity below soft limit. The grace time will be automatically turned off. If the user does not reduce the capacity to soft limit within the grace time, he will be unable to write data, even if the usage capacity does not reach hard limit

6. Set the extension time:


[root@localhost dvd]# 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
[root@localhost dvd]# quota -uv redhat ( -u :  According to user . -v :  According to quota value )
Disk quotas for user redhat (uid 1000): 
   Filesystem blocks  quota  limit  grace  files  quota  limit  grace
   /dev/sdb1    0  10240  20480        0      5    10    

7. Copy quota data to another user using the -p parameter:


[root@localhost dvd]# edquota -p redhat aaa 
[root@localhost dvd]# quota -uv aaa
Disk quotas for user aaa (uid 1001):
   Filesystem blocks  quota  limit  grace  files  quota  limit  grace
   /dev/sdb1    0   10240  20480        0     5   10    

 Copy to multiple users simultaneously: 

[root@localhost dvd]#edquota -p redhat`awk -F: '$3 >499 {print $1}' /etc/passwd`

This copies the disk quota information for quota to all uid > 499 users (users with uid of more than 500 are usually real users)

8. Automatically activate quota when the system starts:

Can be added to the/etc/rc d/rc sysinit or/etc/rc d/rc local, add 1 line at the end: / usr/sbin/quotaon - aug

Remember when the system shutdown/restart script (. / etc rc d/rc [0, 6] /) to add close quota statement: / usr/sbin/quotaoff - aug

9. Query disk quota:


[root@localhost dvd]# repquota -a
*** Report for user quotas on device /dev/sdb1
Block grace time: 24:00; Inode grace time: 7days
            Block limits        File limits
User      used  soft  hard grace  used soft hard grace
----------------------------------------------------------------------
root   --   20     0    0            2   0   0    

10. Plan tasks quotcheck:

quotcheck used to scan the file system disk usage, update aquota. user, aquota. group quota record files to the latest condition. It is therefore best to execute at system startup or periodically through cron:

1 time per week at 6 am: 0 7 * * 6 /sbin/ quotacheck-avug


Related articles: