Thursday, October 10, 2019

Setup Filesystem Quotas on Linux / Ubuntu Server

Installing the Quota Tools

sudo apt update
sudo apt install quota

Check Quota Version :

quota --version

Installing the Quota Kernel Module
First need  to check, we will use find to search for the quota_v1 and quota_v2 modules in the /lib/modules/... directory:

find /lib/modules/`uname -r` -type f -name '*quota_v*.ko*'

Output : 
/lib/modules/4.15.0-45-generic/kernel/fs/quota/quota_v1.ko
/lib/modules/4.15.0-45-generic/kernel/fs/quota/quota_v2.ko

If you get no output from the above command, install

sudo apt install linux-image-extra-virtual
OR 
Resolve this install kernel dependencies, like this:

apt-get -y install linux-image-generic
apt-get -y install linux-headers-generic
apt-get -y install linux-image-extra-`uname -r`

After that we need to add the quota modules to start with boot:

echo quota_v1 >> /etc/modules
echo quota_v2 >> /etc/modules

Updating Filesystem Mount Options

sudo nano /etc/fstab
LABEL=/home    /home   ext2   defaults,usrquota,grpquota  0 0
Remount the filesystem to make the new options take effect:

sudo mount -o remount /home

Enabling Quotas
sudo quotacheck -ugm /home

Now we’re ready to turn on the quota system:

sudo quotaon -v /home

Configuring Quotas for a User

Using edquota to Set a User Quota

sudo edquota -u  jagdeep

# edquota ramesh

Disk quotas for user jagdeep (uid 50001):
  Filesystem           blocks       soft       hard     inodes     soft     hard
  /dev/sda3           1419352          0          0       1686        0        0
Let’s update our sammy user to have a block quota with a 100MB soft limit, and a 110MB hard limit:

# edquota ramesh

Disk quotas for user jagdeep (uid 50001):
  Filesystem         blocks       soft       hard     inodes     soft     hard
  /dev/sda3        1419352       100M          110M   1686    0        0
Save and close the file

sudo quota -vs jagdeep

Using setquota to Set a User Quota
sudo setquota -u sammy 200M 220M 0 0 /home

Generating Quota Reports

sudo repquota -s /home

# repquota /home
*** Report for user quotas on device /dev/sda3
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --  566488       0       0           5401     0     0
nobody    --    1448       0       0             30     0     0
jagdeep    -- 1419352       0       0           1686     0     0
john      --   26604       0       0            172     0     0

Configuring a Grace Period for Overages

sudo setquota -t 864000 864000 /home


Output
Block grace time: 10days; Inode grace time: 10days
. . .

Disable quota for a Linux user or group on the shell
Example for disabling the quota for the user "testuser":
setquota -u testuser 0 0 0 0 -a /home
Example for disabling quota for the group "testgroup":
setquota -g testgroup 0 0 0 0 -a /home