Install quota in CentOS system to manage disk quota

Time:2020-11-26

In the work of network management, due to the limited resources of hard disk, it is often necessary to set Linux disk quota for multi-user servers. This function is necessary for public multi-user servers (free or charged). Quota is a tool to realize Linux disk quota under red hatlinux. Quota only supports a separate mount file system, not a directory.

1. Mount the directory and add quota

View the / home directory mount

Copy code

The code is as follows:

[[email protected] ~]# df -h /home

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3             2.0G   35M  1.8G   2% /home

Copy code

The code is as follows:

[[email protected] ~]# mount | grep home
/dev/sda3 on /home type ext4 (rw)

User and group quota file system support parameters: usrquota, grpquota

Copy code

The code is as follows:

[[email protected] ~]# mount -o remount,usrquota,grpquota /home

Check to make sure that usrquota and grpquota are added

Copy code

The code is as follows:

[[email protected] ~]# mount | grep home

/dev/sda3 on /home type ext4 (rw,usrquota,grpquota)

Edit the fstab file to mount quota when the system starts

Copy code

The code is as follows:

[ [email protected] ~]#VI / etc / fstab takes effect after restart
LABEL=/home /home ext4 defaults,usrquota,grpquota 1 2

Re mount

Copy code

The code is as follows:

[[email protected] ~]# umount /home
[[email protected] ~]# mount -a
[[email protected] ~]# mount | grep home

2. Quota installation generates usrquota and grpquota directories
The file system with usrquota and grpquota parameters is scanned by quotacheck

Copy code

The code is as follows:

[[email protected] ~]# quotacheck -avug
-bash: command not found

Copy code

The code is as follows:

[[email protected] ~]# yum install quota

Install quota

Copy code

The code is as follows:

[[email protected] ~]# quotacheck -avug

Automatically create two directories, usrquota and grpquota

Copy code

The code is as follows:

[[email protected] ~]# ll -d /home/

-rw——- 1 root root 8192 Mar  6 11:58 /home/aquota.group
-rw——- 1 root root 9216 Mar  6 11:58 /home/aquota.user
 
At this point, because of some need, or in some situation, “have to” run this command:

Copy code

The code is as follows:

[[email protected] ~]# quotacheck -avug -mf

It is necessary to make sure that the process of “reading and writing” is not in “write” mode. It is recommended to operate in single user mode.).

Copy code

The code is as follows:

[[email protected] ~]# quotaon -auvg

Launch quota
/dev/sda3 [/home]: group quotas turned on
/dev/sda3 [/home]: user quotas turned on

3. Quota limit size setting
Myquota1 account adds quota restriction. Here, the disk size used by each user is fixed to 500m

Copy code

The code is as follows:

[[email protected] ~]# useradd myquota1
[[email protected] ~]# edquota -u myquota1

 Filesystem    blocks    soft    hard  inodes  soft  hard
  /dev/sda3         80  500000  600000      10     0     0

Copy myquota1 user information to myquota2

Copy code

The code is as follows:

[[email protected] ~]# edquota -p myquota1 -u myquota2
Copy code

The code is as follows:

[[email protected] ~]# edquota -g myquotagrp

Edit the total used size of the myquotagrp group
  Filesystem    blocks    soft     hard  inodes  soft  hard
  /dev/sda3        160  10000000  12000000       20     0     0

Copy code

The code is as follows:

[[email protected] ~]# edquota -t

Modify the grace time when the user file exceeds the size

Copy code

The code is as follows:

[[email protected] ~]# quota -uvs myquota1 myquota2

Query usage
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace
      /dev/sda3      80    495M    595M              10       0       0
Disk quotas for user myquota2 (uid 501):
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace
      /dev/sda3      80    495M    595M              10       0       0

Copy code

The code is as follows:

[[email protected] ~]# quota -gvs myquotagrp

Query myquotagrp group usage

Copy code

The code is as follows:

edquota -g myquotagrp

  Filesystem    blocks    soft     hard  inodes  soft  hard
  /dev/sda3        160   1000M     1200M      20     0     0

Copy code

The code is as follows:

[ [email protected] ~]#Repquota – AUVs query all user usage

User            used    soft    hard  grace    used  soft  hard  grace
———————————————————————-
root      —      20       0       0              2     0     0
myquota1  —      32       495M    595M           10     0     0
myquota2  —      32       495M    595M           10     0     0
myquotagrp —      64      1000M   1200M          20     0     0

Statistics:
Total blocks: 7
Data blocks: 1
Entries: 4
Used average: 4.000000

4. Quota test
Use the identity of myquota1 to build a large file of 550mb and observe the result of quota!

Copy code

The code is as follows:

[[email protected] ~]# dd if=/dev/zero of=bigfile bs=1M count=550
[[email protected] ~]# repquota -auv

                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
———————————————————————-
myquota1  +-  556840  500000  600000 13days      11     0     0

You can find grace in myquota1!

Create another large file to make the total capacity more than 600m!

Copy code

The code is as follows:

[[email protected] ~]# dd if=/dev/zero of=bigfile2 bs=1M count=600
du -sk

600000. The file size has checked the set size. The operation failed

Copy code

The code is as follows:

[ [email protected] ~]#Warnquota to see if there are warnings

 
If you want to cancel the limit of disk space, you can use the following command, please see:

Copy code

The code is as follows:

[[email protected] ~]# quotaoff /home

(remove the disk space limit.)

Copy code

The code is as follows:

[[email protected] ~]# quotaon /home

(start disk space limit now.)

Sorting out the basic usage of PS: quota command

grammar
quota [ -u [ User ] ] [ -g [ Group ] ] [ -v | -q ]
describe
The root user can use the – U flag with the optional user parameter to see the restrictions of other users. Users without root user rights can view the restrictions on the groups they belong to by using the – G flag with the optional group parameter.
Note:
If a particular user has no files on the file system for which it has a quota, the command displays quota: none for that user. When the user has files in the file system, the actual quota of the user is displayed.
sign
-G displays the quota for the user group.
-U displays the user quota. This flag is the default option.
-V shows the quota on a file system without allocated storage.
-Q print a brief message that contains only information about the use of file systems that exceed the quota.
Note:
-Take precedence over the flag.
Security
Access control: this command is owned by the root user and the bin group.
Privilege control: This program is setuid to allow non privileged users to view personal quotas.
Examples
(1) . to display the quota of user Keith, enter:

Copy code

The code is as follows:

quota

The system displays the following information:
User quotas for user keith (uid 502):
Filesystem blocks quota limit grace Files quota limit grace
/u 20 55 60 20 60 65
(2) . to display the quota of user davec as root, enter:

Copy code

The code is as follows:

quota -u davec

The system displays the following information:
User quotas for user davec (uid 2702):
Filesystem blocks quota limit grace files quota limit grace
/u 48 50 60 7 60 60
file

Copy code

The code is as follows:

quota.user

Specify user quota.

Copy code

The code is as follows:

quota.group

Specify group limits.

Copy code

The code is as follows:

/etc/filesystems

Contains the file system name and location.