Managing Disk Usage With Quota

In this article, we are going to discuss how to Manage disk usage with quota. Disk Quota is a nice feature in Linux that allows the administrator to control the disk usage from users. With the help of quota, we can apply restrictions on disk space usage and can allow the user to use a fixed amount of disk space. Quota can only be created for Partitions.

For example Suppose We have a common shared drive in our organization where all users storing their data, in that case, some users may misuse the disk space and keeps their personal data’s like Movies, Music..etc, to avoid such things we can allocate the user to use a fixed amount of disk space like 4 GB, If user crosses the allocated limit then the administrator will get a notification about that disk usage.

The Five very important terms we should know about while configuring quotas in Linux :

  • Soft Limit: The amount of data allowed to the user to store data on the File Server.
  • Hard Limit:  After exceeding the soft limit there is an amount of extra disk space allowed by the System Administrator to the user which totally depends on the Grace Period.

For Example, we allowed soft limit to the user is 10MB, the hard limit is 12 MB and the Grace Period is 7 Days, In that case, if the user crossed Soft Limit i.e. 10MB then he/she can store 2MB of extra data on the disk as we allow Hard Limit as 12MB that also for 7 Days. After 7 days user cannot store data on disk even the remaining 2MB is not utilized. in another case, if the user utilizes the hard limit within 7 days then also he will deny storing data on disk.

Note: User cannot Exceed the Hard Limit.

  • Grace Period: After crossed the Soft Limit quota allows 7 days of Grace period to use the allowed Hard Limit, After 7 days the user will be denied storing data.
  • Blocks: Restrict the disk usage of the user by Size.

For Example, if the Administrator allowed the user to utilize 10MB then the user will get an “Access Denied” error after cross the 10MB Limit.

  • Inodes: Restrict the disk usage of the user by file/directory number.

For Example, if the Administrator allowed to create 10 inodes for a user then the user can create 10 files and after crossing the assigned file limit the user will get an error message i.e. “Access Denied”. even the created files/directories are 0 in Size.

Managing Disk Usage With Quota
Managing Disk Usage With Quota

Follow the below Steps to Implementing Disk Quota: Managing Disk Usage With Quota-

Step:1 Create a Partition

Let’s first create a Partition, Follow the below steps.

Use the below command to list the already created Partitions and available free disk spaces.

   [root@localhost ~]# fdisk -l   # List Disk Partitions and Free Disks

   Disk /dev/sda: 21.4 GB, 21474836480 bytes
   255 heads, 63 sectors/track, 2610 cylinders
   Units = cylinders of 16065 * 512 = 8225280 bytes

      Device Boot      Start         End      Blocks   Id  System
   /dev/sda1   *           1          38      305203+  83  Linux
   /dev/sda2              39        2480    19615365   83  Linux
   /dev/sda3            2481        2610     1044225   82  Linux swap / Solaris

   Disk /dev/sdb: 10.7 GB, 10737418240 bytes
   255 heads, 63 sectors/track, 1305 cylinders
   Units = cylinders of 16065 * 512 = 8225280 bytes

      Device Boot      Start         End      Blocks   Id  System
   [root@localhost ~]# 

As we can see above we have one free Harddisk i.e. “/dev/sdb” so let’s create a partition.

So follow the below steps to create a New Partition.

   [root@localhost ~]# fdisk /dev/sdb     # To initiate the Partition Creation 

   The number of cylinders for this disk is set to 1305.
   There is nothing wrong with that, but this is larger than 1024,
   and could in certain setups cause problems with:
   1) software that runs at boot time (e.g., old versions of LILO)
   2) booting and partitioning software from other OSs
      (e.g., DOS FDISK, OS/2 FDISK)

   Command (m for help): n     # "n" for New Partition
   Command action
      e   extended
      p   primary partition (1-4)
   p                                    # "p" for Primary Partition
   Partition number (1-4): 1            # Select the available Partition Number
   First cylinder (1-1305, default 1):  # Just Press Enter to Select the Default Cylinder
   Using default value 1
   Last cylinder or +size or +sizeM or +sizeK (1-1305, default 1305): +5G     # Give the Partition Size

   Command (m for help): w     # "w" for Save the Partition Table
   The partition table has been altered!

   Calling ioctl() to re-read partition table.
   Syncing disks.
   [root@localhost ~]# partprobe /dev/sdb     # To avoid Reboot
   [root@localhost ~]# 

Now Format the partition as shown below.

   [root@localhost ~]# mkfs.ext3 /dev/sdb1     # Format the Partition with ext3 Partition
   mke2fs 1.39 (29-May-2006)
   Filesystem label=
   OS type: Linux
   Block size=4096 (log=2)
   Fragment size=4096 (log=2)
   611648 inodes, 1222940 blocks
   61147 blocks (5.00%) reserved for the super user
   First data block=0
   Maximum filesystem blocks=1254096896
   38 block groups
   32768 blocks per group, 32768 fragments per group
   16096 inodes per group
   Superblock backups stored on blocks: 
           32768, 98304, 163840, 229376, 294912, 819200, 884736

   Writing inode tables: done                            
   Creating journal (32768 blocks): done
   Writing superblocks and filesystem accounting information: done

   This filesystem will be automatically checked every 21 mounts or
   180 days, whichever comes first.  Use tune2fs -c or -i to override.
   [root@localhost ~]# 

As shown on the output above we successfully created a new partition i.e. “/dev/sdb1“, now we need to mount the partition to use it.

For that create a directory and mount the partition in “/etc/fstab” as shown below (Highlighted in Blue Color).

[root@localhost ~]# mkdir /quota      # Create a Directory
Managing Disk Usage With Quota
Mount Partition in fstab

Step: 2 Enabling Quotas on Partition

Just edit the “/etc/fstab” file and add usrquota (to Enable Quota for Users) & grpquota (to Enable Quota for Groups ) next to defaults separated by comma to that partition where you want to enable quota for users and groups as shown below on the snapshot.

Managing Disk Usage With Quota
Enabling Quota on Partition

Step: 3 Remount the File systems using mount Command

After enabled the quota in the file-system by modifying the /etc/fstab file remount the file system to take effect.

Note:  Before using the below command ensure that the file system is not in use, If it is in use then simply reboot the system to take effect.

   [root@localhost ~]# mount -o remount /quota/     # Remount the FileSystem
   [root@localhost ~]#

Now run the mount command to check if User and Group quota got enabled in the file system or not.

   [root@localhost ~]# mount
   /dev/sda2 on / type ext3 (rw)
   proc on /proc type proc (rw)
   sysfs on /sys type sysfs (rw)
   devpts on /dev/pts type devpts (rw,gid=5,mode=620)
   /dev/sda1 on /boot type ext3 (rw)
   tmpfs on /dev/shm type tmpfs (rw)
   none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
   none on /proc/fs/vmblock/mountPoint type vmblock (rw)
   sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
   .host:/ on /mnt/hgfs type vmhgfs (rw,ttl=1)
   /dev/sdb1 on /quota type ext3 (rw,usrquota,grpquota)
   [root@localhost ~]# 

Or You can Try the below command to do so.

   [root@localhost ~]# mount | grep -i /quota
   /dev/sdb1 on /quota type ext3 (rw,usrquota,grpquota)
   [root@localhost ~]#

Step: 4 Create Quota Database Files for Users and Groups

After enabling the quota on the file-system we need to create a Quota database file by using quotacheck command.

   [root@localhost ~]# quotacheck -cugv /quota/ 

Where :

-c : Create Quota Database File
-u : Enable Quota for Users
-g : Enable Quota for Groups
-v : For Verbose Output

   [root@localhost ~]# cd /quota/
   [root@localhost quota]# ls
   aquota.group aquota.user  lost+found

As we can see on the above output after running the quotacheck command quota has created two database files aquota.group  ( For Groups ) and aquota.user  For Users ).

Step: 5 Enable the Quota by quotaon Command

After creating database files for users and groups using the above step now we have to enable the quota by quotaon command as shown below.

  [root@localhost ~]# quotaon -v /quota/
   /dev/sdb1 [/quota]: group quotas turned on
   /dev/sdb1 [/quota]: user quotas turned on
   [root@localhost ~]#  

Other Commands If you want to disable the Quota for a particular Partition or for all Partitions then use the below command.

   [root@localhost ~]# quotaoff /quota/    # Disable Quota for /quota Partition
   
   [root@localhost ~]# quotaoff -a     # Disable the Quota for all Partitions
  • a – Assign Quota for all Partitions
  • u – Assign Quota to Users
  • g – Assign Quota to Groups
  • v – For Verbose output.

For Example :

quotaon -a     # To Enable quota for all

quota -augv    # To Enable Quota for All Users and Groups

Step: 6 Apply Disk Quota to Users & Groups

We have successfully configured and Enabled the Quota on Partition, now it’s time to assign the Disk Quota restriction to users.

I have a user called “itsmarttricks” and want to apply disk quota restriction to that user,So let’s follow the below steps.

To Assign the quota to any user we have to follow the below Syntax :

edquota -u <Username>     # For Users

edquota -g <GroupName>   # For Groups

So as an example Let’s assign a quota to user itsmarttricks:

 
   [root@localhost ~]# edquota -u itsmarttricks # To Assign Quota to User itsmarttricks

Note: We can assign quota restriction by KB and 1 Block is equal to 1 KB

There are two options by which we can assign a quota to Users and Groups i.e. By Block & Inodes.

After running the above command use will get a screen like shown below :

Managing Disk Usage With Quota
Edited Quota Summary of User

Implementing Block Quota

Let’s first concentrate on Block Quota, So I am going to allow  Soft Limit as 100MB and Hard Limit as 110MB, See the Snapshot below :

Managing Disk Usage With Quota
Applying Block Quota to User

As we can see on the snapshot above We allowed 102400KB (100 MB) as a Soft Limit and 112640KB (120 MB) as a Hard Limit.

The calculation would be like this: 1024KB = 1MB So 102400 is equal to 100MB and 102400KB is equal to 110MB.

Now Save the configuration by press the Escape button on the Keyboard and enter:wq then press ENTER and Remount the partition to take effect.

Now we can check if the quota is assigned to the user properly or not by the below steps :

Step:1 Switch to user itsmarttricks by su command :

Step:2 copy data till 100MB after crossing soft limit you will get a warning message “Quota Exceeded“and after Hard limit, you will get the message “Limit Reached

Note: For testing purpose, you can create a blank file using dd command as shown below :

dd if=/dev/zero of=/quota/doc.txt bs=1024KB count=100

Where:

doc.txt – is a Filename i create in /quota mount point

The above command will create a zero file with a size of 100MB.

Output :

# Create a 100MB File using dd Command

   [itsmarttricks@localhost ~]$ dd if=/dev/zero of=/quota/doc.txt bs=1024KB count=100
   100+0 records in
   100+0 records out
   102400000 bytes (102 MB) copied, 0.558218 seconds, 183 MB/s

   # Check the size of the created File
   
   [itsmarttricks@localhost ~]$ ls /quota/
   aquota.group  aquota.user  doc.txt  lost+found
   [itsmarttricks@localhost ~]$ du -h /quota/doc.txt 
   98M     /quota/doc.txt    # Now file size is 98MB
   
   # Now create one more file of size 14MB
   
   [itsmarttricks@localhost ~]$ dd if=/dev/zero of=/quota/doc1.txt bs=1024KB count=14
   sdb1: warning, user block quota exceeded.   # Warning message as we crossed the Soft Limit
   sdb1: write failed, user block limit reached.
   dd: writing `/quota/doc1.txt': Disk quota exceeded
   13+0 records in
   12+0 records out
   12816384 bytes (13 MB) copied, 0.382714 seconds, 33.5 MB/s
   
   # As we can see below total file size we have created is 13+98=111MB (Soft Limit - 110MB)
   
   [itsmarttricks@localhost ~]$ du -h /quota/doc*
   13M     /quota/doc1.txt
   98M     /quota/doc.txt
   
   # Now create one more file of size 10MB

   [itsmarttricks@localhost ~]$ dd if=/dev/zero of=/quota/doc2.txt bs=1024KB count=10
   dd: writing `/quota/doc2.txt': Disk quota exceeded   # Quota Exceeded Message as we crossed the Hard Limit
   1+0 records in
   0+0 records out
   0 bytes (0 B) copied, 0.000262057 seconds, 0.0 kB/s
   
   [itsmarttricks@localhost ~]$ du -h /quota/doc*
   13M     /quota/doc1.txt
   0       /quota/doc2.txt   # The doc2.txt is 0 as we crossed the Hard Limit and Quota don't allow us to create the file.
   98M     /quota/doc.txt

Implementing Inode Quota

As I explained earlier the Inode quota is a space restriction based on file number, for Example, if i allow creating 5 Files as a Soft Limit and 7 files as Hard Limit then After crossing the Soft limit user will get a Warning message that Quota Exceeded and after crossing the Hard Limit quota will deny the user to create any of files.

Note: In the case of Inode Quota Restriction Size doesn’t matters, If the user creates 7 Blank files/Directories of Size 0KB then also he won’t be allowed to store any further data on that directory, So Let’s have look at the Steps for Implementation of Inode Quota.

To Implement Inode Quota just run the command edquota -u itsmarttricks to edit the quota restriction screen for user itsmarttricks and then enter the Inode Limit you want to give to the user.

Example :

Here I am going to allow Soft Limit as 5 Files and Hard Limit as 7 Files for Inode Quota, Follow the Snapshot below.

Managing Disk Usage With Quota
Applying Inode Quota to User

To check if the quota is applied properly just log in as itsmarttricks using the command su – itsmarttricks, then create 5 files, you can successfully able to do so as we allowed soft-limit as 5 Inodes, When you create 6th file you will get a Warning Message “Quota Exceeded” as we crossed the Soft Limit, Now Let’s create more 3 files then you will get a message “Limit Reached” So out of 3 files one file will be created and other two will be won’t as we allowed 7 Inodes as a Hard Limit, See the Output below.

Output :

   
   [root@localhost ~]# su - itsmarttricks   # Login as a itsmarttricks
   
   [itsmarttricks@localhost ~]$ cd /quota/
   [itsmarttricks@localhost quota]$ touch doc{1,2,3,4,5}.txt   # Create 5 Files
   [itsmarttricks@localhost quota]$ ls
   aquota.group  aquota.user  doc1.txt  doc2.txt  doc3.txt  doc4.txt  doc5.txt  lost+found
   
   [itsmarttricks@localhost quota]$ touch doc6.txt   # Create 6th File
   sdb1: warning, user file quota exceeded.   # Warning Message after creating 6th File and crossed the Soft Limit
   
   [itsmarttricks@localhost quota]$ touch doc{7,8,9}.txt   # Create more 3 Files
   sdb1: write failed, user file limit reached.         # Limit Reached message after crossed the Hard Limit
   touch: cannot touch `doc8.txt': Disk quota exceeded
   touch: cannot touch `doc9.txt': Disk quota exceeded
   
   # As we can see below we able create doc7.txt file and other two files are not created as we allowed 7 Inodes as HardLimit

   [itsmarttricks@localhost quota]$ ls
   aquota.group  aquota.user  doc1.txt  doc2.txt  doc3.txt  doc4.txt  doc5.txt  doc6.txt  doc7.txt  lost+found
   [itsmarttricks@localhost quota]$

We can check quota report utilized by the user by using the below command :

   
[root@localhost ~]# repquota -a
*** Report for user quotas on device /dev/sdb1
Block grace time: 7days; Inode grace time: 7days
                     Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --  140768       0       0              4     0     0       
itsmarttricks +-  112640  102400  112640  6days       3     0     0

Grace Period

After crossed the Soft Limit quota allows 7 days of Grace period to use the allowed Hard Limit, After 7 days user will denied to store data.

We change the grace period by using edquota with option -T as shown below

edquota -T
   
Times to enforce softlimit for user itsmarttricks (uid 500):
Time units may be: days, hours, minutes, or seconds
  Filesystem                         block grace               inode grace
  /dev/sdb1                         602556seconds                  unset

Note: We have to mention the Grace Period in Seconds.

Also Read – How to Setup NFS Server (Network File System) On Redhat/Centos/Fedora

That’s all, In this article, we have explained Managing Disk Usage With Quota. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.

Share this:
WhatsApp Channel Join Now
Telegram Channel Join Now
Instagram Channel Join Now

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.