For management of Windows systemsIt is easy for network administrators to configure disk quotas, so how to configure disk quotas under Linux and what to look for when configuring disk quotas may be unclear to some readers, and this article will introduce you to the knowledge and skills of disk quotas under Linux.
Linux systems, by default, do not limit the size of disk space used by each user, and if a user inadvertently or maliciously fills a disk, the system cannot write or even crash.
We can do a disk full test, a Linux system that does not have a disk quota set, log in with a regular user, execute the command:
$ dd If=/dev/zero of=quotatest bs=1024k count=10240000
command will generate a fixed-size file. where "if" meansInput, pseudo-device/dev/zero constantly generate the string "0", initialization file; "of" means output, this example generates the file "Quotatest" in the user's current directory, "BS" specifies the block size for "block size", set to "1024k" or 1MB; " Count "is the size of the file (" Quotatest ") generated for the total, and if" BS "is 1MB," 10240000 "is 10000GB.
Most of the server disk space is not so large, the above command did not run out of space to consume the light. The author uses a general configuration of the notebook computer testing, write speed of about 60mb/s, a minute can write 3GB, if your server is 100GB, half an hour after the disk space will reach 100%, the user cannot create a file, the administrator cannot add the user, the system cannot write to the log ... Even after the system restarts, it will not start properly because the disk space is full and temporary files cannot be generated during startup.
If you do the above test, the system will not start normally, you can use the Linux installation CD-ROM boot, the installation interface input "Linux rescue" into the repair mode and delete the file "Quotatest", can be restored.
This shows that if you do not set disk quotas, even if you do notHackers, any ordinary user can destroy our system at any time.
Basic settings for disk quotas
When you set a disk quota, "a user can only use 50MB of disk space in the system", this limit is not possible, only set "a user can use 30MB in the/home partition, 20MB can be used in the/backup partition". Disk quotas are set up in partitions, and the Quota throttling feature is enabled for the partition before users can be set up, and other systems, such as UNIX, Windows, are the same as Linux.
Disk quotas are set up in two ways: hard limits and soft limits. The hard limit is an absolute limit on space use, and in no case does the user allow the user to exceed this limit; soft limits allow users to be in a certain timeframe (by default, one week, you can use the command "EDQUOTA-T" Reset, the time unit can be days, hours, minutes, seconds) than its limit, You can continue to use space without exceeding the hard limit, and the system will issue a warning (the warning message setting file is "/etc/warnquota.conf"), but the system will no longer allow the user to use more space if the user reaches the limit until the time period has been freed.
There are two ways to use disk quotas to limit space, namely, inode and block respectively. Disk quotas can limit the amount of space a user can use in a partition (blocks), or you can limit the number of files (inodes) that users are able to create in a partition, as long as the user creates more files than they can use, even if the files are empty. His behavior of creating files again will also be limited.
Here is a description of the inode concept. When a file is created in Linux, a unique inode is assigned to the file, and the inode of the file is used to access the properties of the file. That is, each file in the Linux system must correspond to an inode, the popular point is that the Linux kernel does not know the letter, so the user should have UID, user group to have GID, process to have the PID ..., file to have inode, once the inode lost files will not be accessible.
Use the command "Ls-i" to find the inode number of the file.
# Ls-i
163523 inittab 163524 Services 163525 a B
A hard-link file in a Linux system is the same size as the original file and can be updated synchronously, because a hard link is a pointer to a file that is the same as the inode of the original file.
If you have a special name file in your system, such as "a B", you cannot manipulate it with the RM command (since RM will determine that you want to delete two files "a" and "B"), you can take advantage of the inode principle, assuming that the inode of "a B" is 163535, and view the details execution:
# Find. -inum 163535-exec ls-l {} \;
-rw-r--r--1 root root 0 Nov 15:21. A b
Delete this file execution:
# Find. -inum 163535-exec rm {} \;
The option "-inum" is to find the inode for the file, and the option "-exec" is the connector for the Find command.
In addition, before you deploy disk quotas, consider which partitions need to be set. Generally, the partition that holds the user's host directory (typically/home) must set the disk quota, and the partition of the public store user data to set disk quotas (such as the company's network backup application server).
To set disk quotas
Having understood the basic concepts above, we can configure disk quotas.
To set disk quotas:
(1) Start partition quota function;
(2) Generate quota information files;
(3) Set up user and group quotas;
(4) Start disk quota service.
To start the partition quota feature
Log in to the system as an administrator, edit the settings option for the appropriate partition in the system configuration file/etc/fstab (for example, the/home partition), and if you enable the user quota Add option "Usrquota", the user group quota adds the option "Grpquota", separated by commas.
Label=/home/home ext3 Defaults,usrquota 1 2
User group quotas are used to limit the amount of space a group of users can use, typically with fewer applications, and this example only starts the user quota.
The partition quota function takes effect requires a reboot of the system, or simply, re-mount the partition for its new parameters to take effect:
# Mount-o Remount/home
Generate Quota Information file
Detects current user space usage in the partition and generates a database file that holds quota information:
# quotacheck-f Vfsv0-uv/home
Quotacheck:scanning/dev/hda3 [/home] quotacheck:cannot stat old user quota file:no such file or directory
Quotacheck:old group file not found. Usage is not being substracted.
Done
Quotacheck:checked 5 directories and 9 files
Quotacheck:old file not found.
The option "-F vfsv0" Specifies the software version implemented by the disk quota feature, the current popular Linux version is basically quota 2.0, you can use the command "man Quotacheck" to view this information, do not have to like most articles and book materials in the Manual "Touch" created.
The option "-U" specifies the build user quota information file "Aquota.user" (Quota 1.0 is Quota.user), and if user group quotas are set, add the option "-g" to generate the quota information file "Aquota.group" with the option "-V" Indicates that details are displayed at execution time. The space usage of partition/home is automatically reported in the execution results, and the quota information file "Aquota.user" is automatically generated with a permission of 600.
If older Linux systems (Quota 1.0) are to be upgraded to Quota 2.0, you can use the command "Convertquota" to convert old format files "Quota.user" and "Quota.group" to the new format "Aquota.user" and " Aquota.group ".
Set user and group quotas
Use the Edquota command to set disk quotas for each user, as in the following example, set quotas for the user sam:
# Edquota Sam
The system calls Vim/vi to generate a temporary file in which the user Sam quota information can be edited, with the default file contents as follows:
Disk quotas for user Sam (UID 500):
Filesystem blocks soft hard inodes soft hard
/dev/hda3 5 0 0 5 0 0
Where the "Filesystem" field indicates the device name of the partition that uses disk quotas, "blocks" and "inodes" indicate the amount of disk space (in kilobytes) that the user is currently using and the number of files established (this limit is generally less applied), "soft" and "hard" This is the soft limit and hard limit mentioned earlier (most apps don't set soft limits).
Edit this file, set the soft and hard limit to the appropriate value, save exit, you can see the "aquota.user" file size changes, indicating that the user's quota has been stored. Set up group quotas you can use the command "EDQUOTA-G group name".
Set the disk quotas for a large number of users, and if the quotas assigned to users are the same, you can use the "-P" option in command Edquota to copy quota settings between users, such as copying the quota information for user Sam to Helen and Jack:
# edquota-p Sam Helen Jack
You can copy the quota template user settings to any number of users, or you can use this command to write a shell script for bulk copying. In addition, you can use the command "Setquota" to set disk quotas, such as:
# setquota Mary 0 20480 0 0/home
Limit the user Mary can use space 20MB in the/home partition, the specific usage is not detailed here, can see its help for more information.
Start Disk quota Service
Use the command Quotaon to activate the quota system and allow quota checking:
# Quotaon/home
If you close the quota service, use the command "Quotaoff".
After the partition's disk quota feature is set, each boot will automatically load and start the service, which should be set separately in the startup script.
Disk quota test
In the third step, set the user Sam quota to 10MB, as follows:
Disk quotas for user Sam (UID 500):
Filesystem blocks soft hard inodes soft hard
/dev/hda3 5 0 10240 5 0 0
Switch to User Sam and do disk full test:
# dd If=/dev/zero of=quotatest bs=1024k count=10240000
Hda3:write failed, user block limit reached.
Dd:writing ' quotatest ': Disk quota exceeded
1+0 Records in
0+0 Records out
1036288 bytes (1.0 MB) copied, 0.0174559 seconds, 59.4 MB/s
User Sam reached the limited disk space, the above prompt message, he will no longer create files, effectively control the use of user disk space.
View disk quotas
Users can view their current quota information by using the command "quota":
$ quota
Disk quotas for user Sam (UID 500):
Filesystem blocks Quota limit grace files quota limit Grace
/dev/hda3 1022 0 1024 6 0 0
Where the "quota" field is a soft limit, the Limit field is hard, and the "grace" fields are soft-restricted to a post-grace period (the time set by "Edquota-t" above).
If the administrator views the disk quota usage for individual users, you can use the command "Repquota":
# Repquota/home
Report for user quotas on Device/dev/hda3
Block Grace Time:7days; Inode Grace Time:7days
Block Limits File Limits
User used soft hard grace used soft hard grace
-----------------------------------------------------------------
Root--10545 0 0 4 0 0
Sam--1024 0 1024 8 0 0
Helen--5 0 10240 5 0 0
............
The user's quota usage can be easily viewed from the results. If you want to get more information you can also execute the command "Quotastats", see Help.
The introduction of how disk quotas are implemented on Linux systems is covered, and all this work is tested on red Hat Fedora Core 6.
Recognize disk quotas for Linux