×
Community Blog How to Set Up Disk Quota on Ubuntu 18.04 server

How to Set Up Disk Quota on Ubuntu 18.04 server

In this article, we will show you how to set up a disk quota for different users on your Ubuntu 18.04 server hosted on Alibaba Cloud ECS.

By Francis Ndungu, Alibaba Cloud Community Blog author.

A disk quota is a limit that sets the maximum storage space available for each user in a Linux system. Setting up such a limit prevents users from filling the entire file system in a multi-user environment and disadvantaging other users in the system.

A real-life example is when you are running a single cloud server to offer a service to different clients. A quota provides limited disk space for each user. For instance, if you are a web designer and you lease webspace to your clients, setting up a disk quota is very important.

Another example is when you are running cloud-based subscription services and you have lots of customers. In order to apply fair usage policy on the disk system depending on what you are charging your clients, setting up a disk quota is an ideal choice.

In this article, we will show you how to set up a disk quota for different users on your Ubuntu 18.04 server hosted on Alibaba Cloud Elastic Compute Service.

Prerequisites

Before you begin, make sure you have the following:

Step 1: Installing Linux quota Package

You will install the quota package available on the Ubuntu package repository. Before this, update the package information index.

$ sudo apt-get update

Then, run the command below to install the quota utility.

$ sudo apt-get install -y quota

Next, verify that the package was installed successfully using the command below:

$ dpkg -l | grep quota

You should get an output similar to the one shown below displaying the quota version number.

ii  quota                                 4.04-2ubuntu0.1                   amd6                                             4        disk quota management tools

Once you are sure the quota tool is working, you will enable the quota service as illustrated in the next step.

Step 2: Enabling quotas on the Linux File System

In Linux, you can change the file system options by editing the /etc/fstab file. The fstab utility determines the configuration of the different file systems and how to mount them depending on the settings specified in the file.

So, to instruct fstab to use the quota system, open the file using nano text editor:

$ sudo nano  /etc/fstab

Your fstab file will show the below configuration information:

# /etc/fstab: static file system information.

¡­

# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/vda1 during installation
UUID=0147b37e-c5d2-4bab-a04b-4edbb5a6501d /               ext4    errors=remount-ro  0       1

...

In the above file, we are interested in the root partition / marked with a UUID. Here, you will be appending a usrquota option in the fourth option of the entry as shown below:

UUID=0147b37e-c5d2-4bab-a04b-4edbb5a6501d /               ext4    errors=remount-ro,usrquota  0       1

Save and close the file by pressing CTRL+X, Y and Enter. Next, enter the command below to remount the partition so that the system can pick up the changes.

$ sudo mount -o remount /

You have now prepared the system to use quotas. You are now ready to start using quota commands and initialize its settings in the next step.

Step 3: Building Required Files for the quota System

To examine the file system and build the required files for the quota utility, you will use the quotacheck command.

The command will create a file named aquota.user if it is not already in the system. Run the command below:

$ sudo quotacheck -ucm /

Next, verify if the file was created in the root directory.

$ ls -ls 

You should see an output similar to the one shown below.

8 -rw-------  1 root root      7168 Oct  5 14:37 aquota.user
4 drwxr-xr-x  2 root root      4096 Jun 24 18:09 bin
4 drwxr-xr-x  3 root root      4096 Jun 24 18:10 boot
...

The output above verifies that the necessary file was created. You can now go ahead and start creating quota limits for users.

Step 4: Setting Up quota Limit for Users

Setting up a disk limit for users is very easy with the quota package already installed. The below syntax highlights the different options that you can use to set up a user disk space limit on the file system.

$ sudo setquota -u [username] [soft disk limit] [hard disk limit] [soft inode limit] [hard inode limit]  [partition]

A soft limit specifies the total amount of disk space or inodes(total number of files or directories) a user is allowed to create in the system.

On the other hand, a hard limit sets the upper disk space/inode boundary for a system user.

Users are allowed to float above the soft limit for a time specified as grace period but they can't go beyond the hard limit disk space.

Let's create a quota limit for user name francis, using the syntax above. Here, we will set a soft limit of 5GB and a hard limit of 6GB.

We will leave the inodes values intact since we don't want to set a limit for files/directories.

$ sudo setquota -u francis 5GB 6GB 0 0 /

To verify if the limit was set for user francis, run the command below

$ sudo quota -vs francis

You will see an output similar to the one shown below.

Disk quotas for user francis (uid 1000):
     Filesystem   space   quota   limit   grace   files   quota   limit   grace
      /dev/vda1     52K   5120M   6144M              15       0       0

As you can see above, setting up and checking the disk limit for a user is very easy. The next step shows you how you can review the disk limit for all users in the system.

Step 5: Reviewing Disk quotas for System Users

The Linux quota command has a special command for generating a disk quota report for all users. You can use this report to see how different users are utilizing their disk space and take specific actions where necessary.

For instance, if a user is just about to hit his/her limit, you can either instruct them to delete some unnecessary files or increase the disk limits for them if your policy allows this.

If you are running a commercial application in a server with different plans depending on disk size, you may ask customers who are about to hit their limits to upgrade their plans.

To view the disk quota usage report for users, run the command below

$ sudo repquota -s /

You will get an output similar to the one shown below:

User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --   3392M      0K      0K           120k     0     0

...

francis   --     52K   5120M   6144M             15     0     0

As you can see, the report above lists all users together with the amount of disk space and files they have created in the system.

Conclusion

In this guide, we have shown you how to install and setup a disk limit using the Linux quota command on Ubuntu 18.04 server hosted on Alibaba Cloud Elastic Compute Service. You can use the knowledge in this article to create a favorable file system environment for every user in your system.

This guide is very useful if you intend to offer a commercial service with your server or probably if you want to set up disk limits for system users working in a multi-user Ubuntu 18.04 environment.

0 0 0
Share on

francisndungu

30 posts | 5 followers

You may also like

Comments

francisndungu

30 posts | 5 followers

Related Products