Friday, 23 March 2012

Linux Disk Quota Implementation

Linux User Disk Quota Implementation
What is disk quota?
Ans : Disk quota is  restricting the disk-space usage to the users. We have to remember one  thing when we are dealing with disk quota i.e. Disk Quota can be applied only on disks/partitions not on files and folders.

how we can implement disk quota?
Disk quota can be implemented in two ways


What is an INODE?
Ans : In Linux every object is consider as file, every file will be having an inode number associated and this is very much easy for computer to recognize where the file is located.

Inode stands for Index Node, and is the focus of all file activities in the UNIX file-system.
Each file has one inode that defines the file’s type (regular, directory, device etc), the location on disk, The size of the file, Access permissions, Access times.
Note that the file’s name is not stored in the inode.

So how to know what is your file Inode number?
Ans : Its just simple execute ls -i on your file.
ls -i test.txt
13662 test.txt
I think now you got what is INODE? Lets move on to BLOCK.
BLOCK A block usually represents one least size on a disk, usually one block equal to 1kb. Some terms in Disk quota.

Soft limit: This is the disk limit where the user gets just a warning message saying that your disk quota is going to expire. This is just a warning, no restriction on data creation will occur at this point.

Hard limit : This is the disk limit where user gets error message, I repeat user gets error message stating that unable to create data.

Implementing QUOTA :
Step1 : Select/prepare the partition for quota, most of the time disk quota is implemented for restricting users not to create unwanted data on servers, so we will implement disk quota on /home mount point.

#vi /etc/fstab
Edit the /home mount point as follows
Before editing
/dev/hda2 /home ext3 defaults 0 0

after editing
/dev/hda2 /home ext3 defaults,usrquota 0 0
Step2 : Remounting the partition(this is done because the mount table should be updated to kernel). Other wise you can reboot the system too for updating of mount table, which is not preferred for live servers.

#mount -o remount,rw /home
Here -o specifies options, with remounting /home partition with read and write options.
Step3 : Creating quota database
#quotacheck -cu /home
The option -c for creating disk quota DB and u for user
Check for user database is created or not when you give ls /home you have to see auota.user file in /home directory,which contains user database.
Step4 : Switching on quota
#quotaon /home
Now get the report for default quota values for user sadeek
#repquoata -a | grep sadeek
sadeek_mohd --    4       0               1     0     0
sadeek_m --       4       0       0       1     0     0
sadeek_test --   16       0       0       4     0     0

Step5 : Now implementing disk quota for user sadeek_moh on /home mount point(/dev/hda2)
#setquota -u sadeek_mohd 100 110 0 0 /dev/hda2
Step6 : Checking quota is implemented or not login to user sadeek_mohd and execute this command
#repquota -a
Step7 : Keep creating data, once 100MB is reached user will get an warning message saying, and when he reaches 110MB he cannot create any more data.
Hint : To create a data file you can use seq command as below
#seq 1 10000 > test.txt
this command will create a file with 10000 lines with numbers in it.
Removing quota :
To do this one, all the users should log out from the system so better do it in run level one.
Step8 : Stop the disk quota
#quotaoff /home

Step9 : Removing quota database which is located /home
#rm /home/aquota.user

Step10 : Edit fstab file and remove usrdata from /home line
#vi /etc/fstab

Before editing
/dev/hda2 /home ext3 defaults,usrquota 0 0

After editing
/dev/hda2 /home ext3 defaults 0 0
Step11 : Remount the /home partition

#mount -o remount,rw /home
That’s it you are done with Disk Quota Implementation in Linux. Now test yourself in creating Linux user disk quota on your own.


  1. While applying quota on partition Might you w'll get below mention error, then follw these steps.

    [root@node ~]# quotacheck -cug /data/
    quotacheck: Cannot create new quotafile /data/ Permission denied
    quotacheck: Cannot initialize IO on new quotafile: Permission denied

    Then go ahead and change selinux content for folder
    [root@node ~]# ls -ldZ /data/

    By default :-
    drwxr-xr-x. root root system_u:object_r:file_t:s0 /data/

    After :-
    chcon unconfined_u:object_r:user_home_dir_t:s0 data/

    [root@node /]# ls -ldZ data/
    drwxr-xr-x. root root unconfined_u:object_r:user_home_dir_t:s0 data/

    [root@node /]# quotacheck -cu data/

    You can check file in /data folder

    [root@node /]# cd /data/

    [root@node data]# ls
    aquota.user lost+found

    njoy 