One-day study of disk quotas on Linux RHEL6

Source: Internet
Author: User
In a production or work environment, you may encounter the following situation: in a multi-person, multi-task environment, multiple people share a disk space, some people may put a lot of or a lot of files in the same space, resulting in a sharp decrease in disk space. This will inevitably lead to other people's rapid space... information & nbs

 

In a production or work environment, you may encounter the following situation: in a multi-person, multi-task environment, multiple people share a disk space, some people may put a lot of or a lot of files in the same space, resulting in a sharp decrease in disk space. This will inevitably lead to the rapid reduction of space for others. To give everyone equal permission to use disk space, we need to use the disk quota. I will restrict the use of space by users. In Linux, quota is used to implement this function.

 

In addition to the above situations, it is generally used in the following scenarios

In some spaces that provide web services, the size of each person's webpage space is limited.

In the mail service, the email space of each person is also limited.

In the file sharing service of the company, the hard disk space used by each person also needs to be limited.

For Linux hosts, if there are multiple users, the user and user group restrictions must also be taken into account.

 

Note:

1. the disk quota is loaded into the kernel.

2. it is only applicable to the ETX2/EXT3/EXT4 file system and does not work for directories. Note that although the mount point is a directory, but it is actually a file system device.

3. only applicable to users or user groups, except root

4. the user group quota refers to the limit used by all members of a user group, not the limit used by each user.

 

Before starting the operation, let's take a look at the content related to the quota configuration.

Inode: limit the number of files that can be created by users

Block: limits the user's disk capacity, in KB

Soft: soft restrictions. After this value is exceeded, the user is reminded, but the user's permissions are not limited.

Hard: hard limit. This is the highest limit, and the user or group will never exceed this limit. If the limit is exceeded, the user's permission is limited.

Grace time: grace time. This is used to remind users when the soft limit is reached and the hard limit is not reached. If you do not perform any disk management operations within the number of days of the warning, the soft limit value immediately replaces the hard limit as the limit for quota, even if you do not reach the hard limit, it cannot be used.

 

Next let's start the demo operation.

 

[Root @ yufei ~] # Df

Filesystem 1K-blocks Used Available Use % Mounted on

/Dev/sda1 15118728 7242960 7107768 51%/

Tmpfs 255784 0 255784 0%/dev/shm

My RHEL6 system is a virtual machine, so there are not many settings, there is only one root partition, there is no choice, we will implement disk quota for the root partition. Note that there must be at least two users in your system, because the disk quota does not work for ROOT users.

 

Step 1: remount the file system or modify the/etc/fstab file

Because remount only works for the moment. if you want to enable the next boot, you need to write the quota parameter to the fstab file.

 

[Root @ yufei ~] # Mount

/Dev/sda1 on/type ext4 (rw)

Useless information omitted

We can see that there is no quota parameter under/partition, so we need to re-mount.

[Root @ yufei ~] # Mount-o usrquota, remount/

[Root @ yufei ~] # Mount

/Dev/sda1 on/type ext4 (rw, usrquota)

Now, the user quota is enabled.

 

Step 2: Create a disk quota database

[Root @ yufei ~] # Ls/| grep quota

[Root @ yufei ~] # Quotacheck-cvum/dev/sda1

Quotacheck: Your kernel probably supports journaled quota but you are not using it. Consider switching to journaled quota to avoid running quotacheck after an unclean shutdown.

Quotacheck: Scanning/dev/sda1 [/] done

Quotacheck: Cannot stat old user quota file: No such file or directory

Quotacheck: Old group file not found. Usage will not be substracted.

Quotacheck: Checked 12514 directories and 98071 files

Quotacheck: Old file not found.

[Root @ yufei ~] # Ls/| grep quota

Aquota. user

After we see the configuration file of the user aquota. user, it indicates that we have successfully created

Here we will introduce the functions of several parameters.

-C: Create a disk quota database file

-V: view the creation process.

-U: Create a user's disk quota database file

-G: Create a disk quota database file for the user group.

-A: create a quota database file for all disks. When this parameter is used, no device is needed.

-M: clears the previous disk quota information. this parameter is required when you create a pair/partition.

 

Step 3: Enable Disk quota

[Root @ yufei ~] # Quotaon/dev/sda1

Step 4: Edit the disk quota file

[Root @ yufei ~] # Edquota yufei

Disk quotas for user yufei (uid 500 ):

Filesystem blocks soft hard inodes soft hard

/Dev/sda1 1060 0 0 159 0 0

What are the restrictions behind the content in this main file? as we have already mentioned above, this is not the case here ?? Aluminum player? Why? Mechanical? Locks and inodes refer to the existing files and sizes of this user, in the unit of K!

The content shown above roughly means

User yufei (uid 500)

The space occupied by/dev/sda1 is 1060 KB. there is no limit on the number of files, and there is no limit on the number of files.

In fact, the above editing method calls vi, so you can edit it freely!

We add all the restrictions.

 

Disk quotas for user yufei (uid 500 ):

Filesystem blocks soft hard inodes soft hard

/Dev/sda1 1060 5120 10240 159 162 165

Save and exit

 

Step 5: query your disk quota

[Root @ yufei ~] # Quota yufei

Disk quotas for user yufei (uid 500 ):

Filesystem blocks quota limit grace files quota limit grace

/Dev/sda1 1060 5120 10240 159 162 165

From the query results, yufei users have up to 10 MB space. The number of files is 162, but the maximum number is 165.

 

Step 6: test

This step is not actually done. we just want to demonstrate it. Of course, you can also test it in practice.

 

[Root @ yufei ~] # Su-yufei

[Yufei @ yufei ~] $ Mkdir test

[Yufei @ yufei ~] $ Cd test/

[Yufei @ yufei test] $ dd if =/dev/zero of = file1 bs = 1 M count = 2

2 + 0 records in

2 + 0 records out

2097152 bytes (2.1 MB) copied, 0.008217 s, 255 MB/s

[Yufei @ yufei test] $ dd if =/dev/zero of = file2 bs = 1 M count = 2

2 + 0 records in

2 + 0 records out

2097152 bytes (2.1 MB) copied, 0.00576349 s, 364 MB/s

[Yufei @ yufei test] $ dd if =/dev/zero of = file3 bs = 1 M count = 2

2 + 0 records in

2 + 0 records out

2097152 bytes (2.1 MB) copied, 0.00753033 s, 278 MB/s

[Yufei @ yufei test] $ ls-l

Total 6144

-Rw-r --. 1 yufei 2097152 May 29 file1

-Rw-r --. 1 yufei 2097152 May 29 file2

-Rw-r --. 1 yufei 2097152 May 29 file3

I created 3 2 MB files and added a test Directory. now let's take a look at the user quota.

 

[Yufei @ yufei test] $ quota

Disk quotas for user yufei (uid 500 ):

Filesystem blocks quota limit grace files quota limit grace

/Dev/sda1 7208*5120 10240 6 days 163*162 165 6 days

At this time, the information we see is different from the previous one. if we exceed the soft limit, we will carry the * number. The grace period is 6 days. In the current situation, I can create up to 2 files and have a maximum space of 2 MB.

Next we will continue to add files.

 

[Yufei @ yufei test] $ touch touchfile1

[Yufei @ yufei test] $ touch touchfile2

[Yufei @ yufei test] $ touch touchfile3

Touch: cannot touch 'touchfile3': Disk quota exceeded

No. when we create the third file, the system prompts that the disk quota has been exceeded.

 

[Yufei @ yufei test] $ ls touchfile *

Touchfile1 touchfile2

The third file is not created successfully !!

Because there is no space, I can continue to add data to it. But the file cannot be added. Next we will test the data space limit.

 

[Yufei @ yufei test] $ rm-fr touchfile *

[Yufei @ yufei test] $ dd if =/dev/zero of = file4 bs = 1 M count = 2

2 + 0 records in

2 + 0 records out

2097152 bytes (2.1 MB) copied, 0.00672323 s, 312 MB/s

There is less than 1 MB space in this time. let's create another 2 MB file to see what will happen.

 

[Yufei @ yufei test] $ dd if =/dev/zero of = file5 bs = 1 M count = 2

Dd: writing 'file5': Disk quota exceeded

1 + 0 records in

0 + 0 records out

1007616 bytes (1.0 MB) copied, 0.0059845 s, 168 MB/s

Pay attention to the top line and prompt that the disk quota is exceeded.

 

[Yufei @ yufei test] $ ls-l

Total 9176

-Rw-r --. 1 yufei 2097152 May 29 file1

-Rw-r --. 1 yufei 2097152 May 29 file2

-Rw-r --. 1 yufei 2097152 May 29 file3

-Rw-r --. 1 yufei 2097152 May 29 file4

-Rw-r --. 1 yufei 1007616 May 29 file5

We also found that 5th files were created, but the size was only 1 MB. if we say that this is a 2 MB data file and only copied 1 MB, it is useless.

 

At this time, all the restrictions have reached the highest level. Users can use them normally only after cleaning.

Let's take a look at our quota.

 

[Yufei @ yufei test] $ quota

Disk quotas for user yufei (uid 500 ):

Filesystem blocks quota limit grace files quota limit grace

/Dev/sda1 10240*5120 10240 6 days 165*162 165 6 days

 

[Yufei @ yufei test] $ rm-f file5

[Yufei @ yufei test] $ rm-f file4

[Yufei @ yufei test] $ rm-f file3

Delete three files. let's check the quota.

 

[Yufei @ yufei test] $ quota

Disk quotas for user yufei (uid 500 ):

Filesystem blocks quota limit grace files quota limit grace

/Dev/sda1 5160*5120 10240 6 days 162 162

At this time, only the disk space is still exceeded, and the number of files is normal. at this time, the number of days allowed for the number of files is blank. In addition, the * number after the number of files does not exist.

 

Quota command

OK. our test is complete. Of course, the user group quota settings are the same as the user's settings, except that the edquota user is changed to the edquota group. However, the quota for user groups is the entire user group, rather than a single member. Next we will introduce you to quota.

 

1. directly use commands to configure the file without edquota editing

The format is as follows:

Setquota user blocks restrict inode to file systems

Setquota username 4096 5120 40 50/home

The demo is as follows:

 

[Yufei @ yufei test] $ quota

Disk quotas for user yufei (uid 500 ):

Filesystem blocks quota limit grace files quota limit grace

/Dev/sda1 5160*5120 10240 6 days 162 162

[Root @ yufei ~] # Exit

[Root @ yufei ~] # Setquota yufei 10240 20280 170 200/dev/sda1

[Root @ yufei ~] # Quota yufei

Disk quotas for user yufei (uid 500 ):

Filesystem blocks quota limit grace files quota limit grace

/Dev/sda1 5160 10240 20280 162 170 200

2. copy the disk quota configuration file

Format

Edquota-p user1 user2 user3

Copy user1 information to multiple users

 

[Root @ yufei ~] # Useradd user2

[Root @ yufei ~] # Quota user2

Disk quotas for user user2 (uid 501): none

[Root @ yufei ~] # Edquota-p yufei user2

[Root @ yufei ~] # Quota user2

Disk quotas for user user2 (uid 501 ):

Filesystem blocks quota limit grace files quota limit grace

/Dev/sda1 32 10240 20280 9 170 200

3. set the grace period

Edquota-t is not followed by a parameter. this is to set the global

Edquota-T user/uid for a user

 

[Root @ yufei ~] # Edquota-t

Grace period before enforcing soft limits for users:

Time units may be: days, hours, minutes, or seconds

Filesystem Block grace period Inode grace period

/Dev/sda1 7 days 7 days

You can see from this file. The default number of days is 7 days!

The preceding time can only be days, hours, minutes, or seconds.

 

4. View disk quota reports

 

Repquota-a query quota reports of all users in the system

[Root @ yufei ~] # Repquota-

* ** Report for user quotas on device/dev/sda1

Block grace time: 7 days; Inode grace time: 7 days

Block limits File limits

User used soft hard grace

----------------------------------------------------------------------

Root +-6608676 661659 661680 6 days 98931 0 0

Daemon -- 8 0 0 3 0 0

Rpc -- 12 0 0 4 0 0

Abrt -- 68 0 0 17 0 0

Haldaemon -- 8 0 0 2 0 0

Nslcd -- 4 0 0 1 0 0

Postfix -- 76 0 0 42 0 0

Avahi -- 8 0 0 3 0 0

Ntp -- 8 0 0 2 0 0

Rpcuser -- 16 0 0 5 0 0

Pulse -- 4 0 0 1 0 0

TPD -- 256 0 0 35 0 0

Yufei -- 5160 10240 20280 162 170

Smmsp -- 24 0 0 6 0 0

Apache -- 8 0 0 2 0 0

User2 -- 32 10240 20280 9 170 200

#7155 -- 194108 0 0 8328 0 0

We didn't impose any restrictions on the root user. Although the system gave root restrictions by default, the above results show that the ROOT user does not work at all. Because this user is God ^_^

 

5. if you set the group quota, you can set an administrator for the group.

 

Vim/etc/quotagrpadmins

The content is detailed and can be understood at a glance.

 

6. disable disk quota

 

Quotaoff-

7. warning letter

The warnquota command can send a warning message to users who have exceeded the quota. The Mail content is set in/etc/warnquota. conf. You also need to install sendmail on your system and run it. This command needs to be executed manually. after the command is executed, an email will be sent to the excess user, and an email will be sent to the ROOT user. If you do not want to manually execute the task, you can add it to the scheduled task for automatic execution.

 

From: root

Reply-To: root@myhost.com

Subject: NOTE: You are exceeding your allocated disk space limits

To: yufei@localhost6.localdomain6

Http: root@localhost6.localdomain6

Compare/etc/warnquota. conf with your mali, and you will know how to set

From: Yufei blog

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.