Introduction
In this lab, you will learn how to use the Linux quota command to set and manage disk quotas for users on a file system. The lab covers the key concepts of disk quota, including soft and hard limits, and how to monitor and manage disk usage. You will learn how to set disk quotas for a user, and how to monitor and manage the quota usage. This lab provides practical examples to help you understand and apply the quota command effectively in a Linux environment.
Understand the Concept of Disk Quota
In this step, we will explore the concept of disk quota in Linux. Disk quota is a system that allows system administrators to set limits on the amount of disk space that a user or group can consume on a file system.
To understand the concept of disk quota, let's first check the current disk usage on the system:
df -h
Example output:
Filesystem Size Used Avail Use% Mounted on
overlay 39G 15G 22G 41% /
tmpfs 64M 0 64M 0% /dev
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
shm 64M 0 64M 0% /dev/shm
/dev/sda1 39G 15G 22G 41% /
tmpfs 3.9G 0 3.9G 0% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
As you can see, the file system has a total capacity of 39GB, with 15GB used and 22GB available. This is the overall disk usage on the system.
Now, let's say we want to limit the disk space usage for a specific user. This is where disk quota comes into play. Disk quota allows us to set a limit on the amount of disk space that a user or group can consume.
The key concepts of disk quota are:
Soft Limit: The soft limit is the maximum amount of disk space that a user can consume before receiving a warning. If the user exceeds the soft limit, they will be able to continue using the file system, but they will receive periodic warnings.
Hard Limit: The hard limit is the absolute maximum amount of disk space that a user can consume. If a user reaches the hard limit, they will not be able to allocate any more disk space, and any further attempts to do so will fail.
Inodes: In addition to limiting the amount of disk space, disk quota can also limit the number of files (inodes) that a user can create. This is useful for preventing users from creating an excessive number of small files, which can also consume a significant amount of disk space.
By understanding these concepts, you will be able to effectively manage disk space usage on your Linux system using the quota command, which we will explore in the next step.
Set Disk Quota for a User
In this step, we will learn how to set disk quota for a user on the Linux system.
First, let's create a new user named quota_user and add them to the sudo group:
sudo useradd -m quota_user
sudo usermod -aG sudo quota_user
Now, we need to enable disk quota for the file system where the user's home directory is located. In this case, it's the root file system (/):
sudo quotacheck -cug /
sudo quotaon /
The quotacheck command scans a file system, builds a table of the current disk usage, and enables quota accounting. The quotaon command then turns on quota enforcement for the file system.
Next, we'll set the soft and hard limits for the quota_user:
sudo setquota -u quota_user 500M 1G 0 0 /
This command sets the soft limit to 500MB and the hard limit to 1GB for the quota_user on the root file system (/). The last two 0 values are for the inode (file count) soft and hard limits, which we're not setting in this example.
To verify the quota settings for the quota_user, we can use the repquota command:
sudo repquota /
Example output:
*** Report for user quotas on device /
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 15360 0 0 1 0 0
quota_user 0 500M 1G 0 0 0
As you can see, the quota_user has a soft limit of 500MB and a hard limit of 1GB for disk space usage.
Now, let's test the quota by logging in as the quota_user and trying to create a file larger than the soft limit:
sudo -u quota_user touch ~/large_file.txt
sudo -u quota_user dd if=/dev/zero of=~/large_file.txt bs=1M count=600
The first command creates an empty file named large_file.txt, and the second command fills the file with 600MB of data. Since this exceeds the soft limit of 500MB, the user should receive a warning message.
Monitor and Manage Disk Quota Usage
In this final step, we will learn how to monitor and manage disk quota usage on the Linux system.
First, let's check the current disk quota usage for the quota_user:
sudo repquota /
Example output:
*** Report for user quotas on device /
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 15360 0 0 1 0 0
quota_user 600M 500M 1G 0 0 0
As you can see, the quota_user is currently using 600MB of disk space, which exceeds the soft limit of 500MB.
To monitor the disk quota usage, we can use the quota command:
sudo -u quota_user quota
Example output:
Disk quotas for user quota_user (uid 1001):
Filesystem blocks quota limit grace files quota limit grace
/ 614400 512000 1048576 [EXCEEDED] 0 0 0
This command shows the current disk usage, soft limit, and hard limit for the quota_user on the root file system (/).
If the user exceeds the soft limit, they will receive periodic warning messages. To manage the disk quota usage, we can either:
- Increase the soft and/or hard limits using the
setquotacommand, as we did in the previous step. - Ask the user to delete some files to reduce their disk usage.
Let's try increasing the soft limit for the quota_user:
sudo setquota -u quota_user 1G 2G 0 0 /
This command sets the soft limit to 1GB and the hard limit to 2GB for the quota_user on the root file system (/).
Now, let's verify the updated quota settings:
sudo repquota /
Example output:
*** Report for user quotas on device /
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 15360 0 0 1 0 0
quota_user 600M 1G 2G 0 0 0
As you can see, the soft limit for the quota_user has been increased to 1GB, and the hard limit has been increased to 2GB.
Summary
In this lab, we first explored the concept of disk quota in Linux, which allows system administrators to set limits on the amount of disk space that a user or group can consume on a file system. We learned about the key concepts of disk quota, including soft limits, hard limits, and inodes. We then proceeded to set disk quota for a user, monitoring and managing their disk quota usage. This hands-on approach provided a practical understanding of how to effectively implement and manage disk quota in a Linux environment.



