Disk Quota on Linux Machine

Yesterday I was discussing Disk Quota on Linux Machine with my students. I know that no one is master, so we have to share our knowledge with other. I decided to write this post who are not good in Linux or learning Linux Administration.

Sometimes Disk space can be limited, and users can fill your disk. Disk Space can be restricted by implementing Disk Quota. It will more beneficial for administrators and as well as users to inform/alert that they are about to consume disk space. Administrators can take necessary actions.9s

You can configure Disk Quota for users as well as groups also. It can control a number of blocks consumed by the user also a number of Inodes.

1.1 First of all check, that quota package is installed on your machine or not.
[code][root@controller ~]#yum list quota
Installed Packages
quota.x86_64 1:4.01-14.el7 @base[/code]
or

[code][root@controller ~]# rpm -q quota
quota-4.01-14.el7.x86_64
[/code]
Implement Disk Quota using following steps – 

1.2 Configure Disk Quota

  1. Enable Disk Quota per file system by modifying /etc/fstab file
  2. Remount the file system(s)
  3. Create the quota database files and generate the disk usage table.
  4. Assign quota policies.

1.2.1 Enable Disk Quota per file system(s) by modifying /etc/fstab file

I assume that you are already working with user root, if you no, switch to root user or login through the root user.

Use any editor (vi / nano) to edit /etc/fstab file and add usrquota,grpquota to the filesystem that require quota.

[code]# /etc/fstab
# Created by anaconda on Sat Apr 29 18:10:07 2017
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk’
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=8222ccb4-f05a-4926-a15c-a52c67197752 /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 1
/dev/mapper/centos-swap swap swap defaults 0 0
/srv/loopback-device/swiftloopback /srv/node/swiftloopback ext4 noatime,nodiratime,nobarrier,loop,user_xattr 0 0
/dev/sdb1 /DATA ext4 defaults 1 2[/code]

I want to enable disk quota on new added disk /dev/sdb1 which is mounted on /DATA directory. Change line to

now save the file.

[code]/dev/sdb1 /DATA ext4 defaults,usrquota,grpquota 1 2[/code]

1.2.2 remount the filesystem(s)

After adding the usrquota and diskquota in /etc/fstab file, remount the filesystem(s) using following

if the file system is not used by any process then issue umount command then mount command

[code][root@controller ~]#umount /dev/sdb1

[root@controller ~]#mount -a[/code]

or use

[code][root@controller ~]#mount -o remount /DATA[/code]

You remounted the filesystem(s), nowsystem is ready / capable to work file quotas but filesystem is not ready to work with disk quota.

1.2.3 Create the quota database files and generate the disk usage table

Now use

[code][root@controller ~] quotacheck -cug /DATA[/code]

  • -c option of the quotacheck command creates quota files (aquota.user and aquota.group) 

it examines quota enabled filesystem and builds a table of current disk usage per file system.

if you are not specifying -u and -g options then only user quota file is created. if you specify -g option only then only group quota file is created. 

1.2.4 Assign quota per user

to configure quota for the user, execute the command

[code]edquota username[/code]

suppose you have multiple users in system and want to enable user quota for of all them. You have to repeat the the task. You username is linuxuser, then

[code]#edquota linxuuser[/code]

after executing the command, you will see following –

[code]Disk quotas for user linuxuser (uid 1003):
Filesystem blocks     soft      hard      inodes      soft     hard
/dev/sdb1     0             0          0            0             0          0[/code]

As you know that we have enabled userquota on /dev/sdb1.

While setting quota for the user you can use size in KiloBytes for Blocks, soft and hard values. Suppose you want to set that user can use only 100 MB on file system use 102400 for the blocks. If you are setting soft limit (92160) means you will be warned when reaching to this limit, means now your are in the grace period. You can consume upto 100 MB. After that system will denied to writing on file system.

column details

  • Fist column is the name of the file system.
  • Second column shows that how much blocks consumed by the user.
  • Third and Fourth columns are used to set soft and hard block limits for the user.
  • Fifth column shows that how many numbers of inodes the user is currently using.
  • Sixth and Seventh columns show soft and hard inode limits for the user on the file system.

to verify quota for the user has been set, execute the command

[code]#quota linuxuser[/code]

Assign quota for the Groups

Group quota can be assigned on the group level. You have group project and you want to enable group quota on it, execute the command

[code]#edquota -g project[/code]

[code]Disk quotas for group project (uid 1004):
Filesystem blocks     soft      hard      inodes      soft     hard
/dev/sdb1     0             0          0            0             0          0[/code]

verify group quota

[code]#quota -g project[/code]

Quota implementation required maintenance because it is possible that your users will exceed the limits. In that situation, you can talk to the user and try to understand them how much data they storing or you can increase the quota for the users.

Enabling and Disabling disk quota

You can turn off user and group quota by executing this command

[code]#quotaoff -augv[/code]

if you are not specifying any option with quotaoff command then only user quotas are disabled.  The -v switch causes verbose status information to display as the command executes.

To enable quotas for a specific file system, such as /home, use the following command:

[code]#quotaon /DATA[/code]

Reporting on Disk Quotas

Repoting of disk quota is quitely important. You can create report of disk quota using this command

[code]# repquota /DATA
*** 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 — 20 0 0 2 0 0
linux +- 102400 92160 102400 6days 2 0 0
testuser +- 102416 92160 102400 6days 6 0 0
linuxuser — 4 0 0 1 0 0[/code]

To view the disk usage report for all (option -a) quota-enabled file systems, use the command:

[code]repquota -a[/code]

 

if the file system is running properly or not , quotacheck command is necessary. You have run this command on regular basis. You can take cron utility to run tasks automatically.

You can schedule quota check and put your script in the following directories.

  • /etc/cron.hourly
  • /etc/cron.daily
  • /etc/cron.weekly
  • /etc/cron.monthly

You can my other post for the Automating Task on Linux machine.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">