How to create disk quota in Linux

Home > Search > How-to
  by

Use apt-get or yum to install quota.

[john.doe@server1 ~]# yum install quota

 


Set a quota on a directory

Edit the /etc/fstab, adding usrquota and grpquota to the directories you want to apply quota to. In this example, user quota and group quota are applied to the /var directory.

/dev/mapper/centos-root   /var    ext4    defaults,usrquota,grpquota    1   2

 

You can either reboot the OS, or you can unmount and then remount the filesystem for the quota to be activated on the filesystem.

[john.doe@server1 ~]# reboot
[john.doe@server1 ~]# umount /var
[john.doe@server1 ~]# mount /dev/mapper/centos-root /var

 

Use the quotaon command to turn on quotas.

[john.doe@server1 ~]# quotaon /var

 

 


Determine the quota for a directory

The repquota command to view a summary of quota.

[john.doe@server1 ~]# repquota -a

 

Use the quotacheck command with the -m (force), -c (create), -u (user) and -g (group) options on the directory.

[john.doe@server1 ~]# quotacheck -mcug /var

 

Use the ls command to verify that quotas have been created. There should be two new files; aquota.user and aquota.group.

[john.doe@server1 ~]# ls /var
aquota.user aquota.group

 


Edit quota settings

The edquota command can be used to edit the quota settings.

[john.doe@server1 ~]# edquota -u user1 /var

 

Use repquota again to verify the block size is set to 1.

[john.doe@server1 ~]# repquota -a

 

To verify the quota limit will function properly, switch to another user account, and attempt to create a new file.  

[john.doe@server1 ~]# su - jane.doe
[jane.doe@server1 ~]# echo "Hello World" > example.file
Error writing filename: Disk quota exceeded

 


Math

Before we set a quota that limits how much data can be stored in the /var directory, we need to determine the total block size of /var.  Use the df command to view the total block size of /var. Let's say the the total block size of /var is 300000000. 300000000 bytes is 300 GB. This means that the /var directory can contain a total of 300 GB of data.

df

 

If repquota -a lists 4 users, to be very safe, we can divide 300,000,000 by 4, which makes 75,000,000. We should set a hard limit of 75,000,000 for each user, and a soft limit of 70,000,000. This will ensure the /var directory is never completely used.

Use the edquota command to edit the quota limit for the WebAdmins group.

edquota -u user1

 

The default limits will be 0.  A hard limit of 250000000 is reasonable. We could also set the soft limit at 2000000000 so that an alert appears before reaching 250000000. Use the repquota command to verify that the quota has taken effect.

repquota -a

 

To test quota to ensure it is working, we can set a hard limit that is 1 block. For example, lets say the used block size is currently 123,456. We can set both the hard limit and soft limit to be 123,457. This should prevent us from being able to add aditional files to /var. Use the touch command to create a new file in /var, and then add junk data to the new file.

  1. In Terminal, type cd /var
  2. Type touch test
  3. Type nano test
  4. Type a bunch of mumbo jumbo, such as adlsfkalvjalvjapiovajvoiajv[olasdvnjal'dvnas'vdlknavl'kajsvlakjva'lvjkal'kj
  5. Press Ctrl O (to save)
  6. Press Ctrl X (to exit the nano editor)

 

Use the df command to ensure the block size has not exceeded 123,456.

df

 

Use the repquota command again. If - - or - + or + - is displayed, the quota has been exceeded.

repquota -a

 

Soft limit can also be set for a user using the -T (time) option.

edquota -u username -T

 

Use the quotacheck command with the -mavug option to update the quota database.

  • -m option (run the command even if the filesystem is being used by other processes)
  • -a option (check /etc/fstab)
  • -v (verbose)
  • -u (all users)
  • -g (all groups)
quotacheck -mavug

 

A CRON job should be create to run quotacheck -mavug on an hourly basis, to resolve issues with mounting errors or filesystem errors.

  1. In Terminal, switch to the root user - su root
  2. TBD - need to create a BASH script in /etc/cron.hourly
  3. Type crontab -e
  4. TBD

 

An email can be sent to the WebAdmins once a day with a summary of the total size, used and available space on the /var directory. 

  1. In Terminal, switch to the root user - su root
  2. Type cd /etc/cron.daily
  3. Type nano diskusage and press Enter
  4. Type the following:
#!/bin/bash
sender="From: John Doe"
recipient="webadmins@example.com"
subject="Disk Usage Report"
(printf "$(df -h)") | mailx -a "$sender" -s "$subject" $recipient
  1. Press Ctrl O (to save)
  2. Press Ctrl X (to exit the nano editor)
  3. Type crontab -e
  4. Type the following: 0 1 * * * bash /etc/cron.daily/diskusage
  5. Press Ctrl O (to save)
  6. Press Ctrl X (to exit the nano editor)

 

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments