Technology Encyclopedia Home >How to set disk quota for file system in Linux?

How to set disk quota for file system in Linux?

To set disk quota for a file system in Linux, you need to follow these steps:

1. Enable Disk Quota Support

  • Ensure the file system is mounted with quota support. This requires modifying the /etc/fstab file to include the usrquota and/or grpquota options for the desired file system.

Example:

/dev/sda1  /home  ext4  defaults,usrquota,grpquota  0  2

After editing /etc/fstab, remount the file system or reboot the system:

mount -o remount /home

2. Install Quota Tools

  • Install the necessary tools to manage quotas. On most Linux distributions, you can use:
sudo apt-get install quota  # For Debian/Ubuntu
sudo yum install quota      # For RHEL/CentOS

3. Initialize Quota for the File System

  • Run the quotacheck command to scan the file system and create quota files (aquota.user and aquota.group):
sudo quotacheck -cug /home
  • Enable quota enforcement:
sudo quotaon /home

4. Set Quota for Users or Groups

  • Use the edquota command to set quotas for a specific user or group. For example, to set a quota for user john:
sudo edquota john

This opens an editor where you can set:

  • Soft limit: The maximum disk space the user can use before warnings are issued.
  • Hard limit: The absolute maximum disk space the user can use.
  • Inode limits: Limits on the number of files the user can create.

Example configuration in the editor:

Disk quotas for user john (uid 1001):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sda1                      1024       5000      10000        100       200       500

Save and exit the editor.

5. Verify Quota Settings

  • Use the quota command to check the quota for a user:
sudo quota -u john
  • Use repquota to view quotas for all users on the file system:
sudo repquota /home

6. Automate Quota Checks (Optional)

  • To ensure quotas are enforced regularly, you can add a cron job to run quotacheck periodically.

Example Use Case:

If you are managing a shared file system for a team and want to limit each user's disk usage to 10GB, you would set the soft and hard limits to 10GB (10240 blocks, assuming 1 block = 1KB) for each user using edquota.

Tencent Cloud Recommendation:

If you are using Tencent Cloud's Cloud Virtual Machine (CVM) or CBS (Cloud Block Storage) services, you can apply these disk quota settings to the file systems on your CVM instances. For scalable storage needs, consider using Tencent Cloud File Storage (CFS), which supports quota management and is suitable for shared file systems.