Disk Quotas On Ubuntu

I’ve recently needed to add disk usage quotas to a server in order to limit how much data users can store so as not to affect the quality of service for other users.

Linux has a method called quota which can help you do this.

Ubuntu provides some packaged tools which let you manage quotas

apt-get install quota

To enable quotas on a partition the first step is to edit the /etc/fstab entry for the partition and append usrquota to it so the kernel knows to manage that partition using quotas.

/dev/sda1 / ext4 defaults,usrquota 0 0

We then need to create 2 files that manage the quota levels in the root of the partition in question

sudo touch /quota.user /quota.group
sudo chmod 600 /quota.*

To make the setting take affect we then need to remount the partition, we can either do this with a reboot or

sudo mount -o remount /

to check that it worked, investigate /etc/mtab, it should look similar to

/dev/sda1 / ext4 rw,usrquota,usrquota 0 0

remounting didn’t work for me, so i issued the reboot command!

When the disk is mounted to support quotas, the next step is to configure how the system is going to manage them!

I’m going to be managing quotas on a per user basis, each user is going to be allowed to store up to 5Gb of data! To configure a user we use the edquota command which will open up an editor

edquota -u idimmu -f /

then edit the config like so

Disk quotas for user idimmu (uid 1000):
Filesystem blocks soft hard inodes soft hard
/dev/sda1 0 5242880 5242880 0 0 0

you can see how I’ve set the hard and soft limits to be 5Gb in kilobytes! (5 * 1024 * 1024)

We can confirm the change with the quota command

root@crisps:~# quota -u idimmu
Disk quotas for user idimmu (uid 1000):
Filesystem blocks quota limit grace files quota limit grace
/dev/sda1 5242872 5242880 5242880 20 0 0

You can see that it’s also done some math to work out how many blocks to limit the user to as well!

Now we need to test it .. can the idimmu account create more than 5Gb in his home directory?

idimmu@crisps:~$ dd if=/dev/zero of=filename1 bs=1024 count=1000000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 20.8073 s, 49.2 MB/s
idimmu@crisps:~$ dd if=/dev/zero of=filename2 bs=1024 count=1000000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 25.4285 s, 40.3 MB/s
idimmu@crisps:~$ dd if=/dev/zero of=filename3 bs=1024 count=1000000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 35.7829 s, 28.6 MB/s
idimmu@crisps:~$ dd if=/dev/zero of=filename4 bs=1024 count=1000000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 18.8164 s, 54.4 MB/s
idimmu@crisps:~$ dd if=/dev/zero of=filename5 bs=1024 count=1000000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 23.2641 s, 44.0 MB/s
idimmu@crisps:~$ dd if=/dev/zero of=filename6 bs=1024 count=1000000
dd: writing `filename6': Disk quota exceeded
242813+0 records in
242812+0 records out
248639488 bytes (249 MB) copied, 10.6704 s, 23.3 MB/s

 

apparently not :D

Ubuntu Linux Bible The Ubuntu Linux Bible covers every facet of Ubuntu administration, both for the desktop and the server, as well as dealing with virtual environments and multi user setups.

About rus

Hi, I'm Rus and after 10 years of Linux administration and software development, in September 2014 I quit my day job, sold all my stuff and moved to Thailand to live the good life.My new website, http://www.ruspow.com shows how I'm making money online as well as what life can be like when you're not sat behind a desk and are free to make your own decisions!

  • microcybersoft

    I can’t run this command on my Ubuntu.

    /dev/sda1 / ext4 defaults,usrquota 0 0

    It says

    bash: /dev/sda1: Permission denied

    How to fix it?

    • jorg

      It isn’t a command. You have to edit the line into /dev/fstab

  • Joel

    I’m just a little confused. You said that you were setting the quota limit to 5 GB in bytes and then you give the example 5,242,880. That looks like 5 MB to me. Should the value in the edquota be in bytes or kilobytes?

    Other than that it seems simple enough. Thanks for the info!
    Joel

    • http://www.idimmu.net rus

      Hi Joel,

      You’re right, it’s kilobytes, not bytes :D

      I’ll amend!

banner