DF and Du commands for Linux Disk Management

Source: Internet
Author: User
Tags uuid disk usage

DF View the total capacity of mounted disks, usage capacity, remaining capacity, etc., can not add any parameters, by default is displayed in K units

DF Common parameters are –i-h-k–m, etc.

-I display results using inodes

-h display in the appropriate unit, e.g. G

-k-m is displayed for units using K,m

A brief introduction to the relevant data you see. Filesystem represents a sector, that is, the area that you divide the disk into, 1k-blocks/1m-blocks the 1k/1m unit, used and Available are used and remaining, use% is the percentage already used, if the value is greater than 90% Then you should be aware that the disk is likely to become full soon; mounted on indicates where the partition (sector) is mounted.

du to see how large a directory occupies

Syntax: du [-ABCKMSH] [file or directory name] commonly used parameters are:

-A: All files and directory sizes are listed. If you do not add any options and parameters, only the size of the table of contents (including subdirectories) is listed.

-B: The values listed are output in bytes, with the default Kbytes

-C: Last add total

-K: Output in kilobytes

-m: Output in megabytes

-S: Only sums are listed

-H: The system automatically adjusts the unit, for example, the file is too small may be a few k, then the unit is displayed in K, if large to a few grams, it is displayed in G units. I am accustomed to using du–sh filename in this form.

" partitioning and formatting of disks "

The thing I often do is to take a brand new disk to partition and format. This also shows that as a Linux system administrator, the operation of the disk must be skilled. So please study this part of the content carefully.

The hard disk partitioning tool under Fdisk Linux

Syntax: fdisk [-l] [device name]

-L: Not following the device name directly lists all disk devices and partition tables in the system, plus the device name lists the partition table for the device.

If you do not add-L into another mode, you can partition the disk in this mode.

Just enter this mode, there will be a prompt command (M for help): At this point, press M will print out a helper list, if you are good English, I think you can not difficult to understand the function of these letters. I used to have P, N,d, W, Q.

P: Prints the partition of the current disk.

N: Re-establish a new partition.

W: Save operation.

Q: Exit.

D: Delete a partition

Because the author's Linux system is installed on the virtual machine, so I can add a new disk. The author then divides the new disk into multiple partitions.

When the fdisk-l is viewed again, a/dev/hdb device is found, and the device does not have a partition table available. Then let's split this/dev/hdb.

First check with P, and there is no partition information.

Create a new partition with N, will be prompted to establish E (extended extended partition) or P (primary partition primary partition), here the author chooses the primary partition, so press p enter, and let enter first cylinder you or directly enter or input a number, Since this disk is new and does not have any partitions, the direct carriage return is actually starting from 1. You can also customize the input, but not more than 2080, the author here enter 1 back to the car. At this point you will be prompted to divide how big, you can write a value (2-2080), you can also enter +sizek or +sizem, the latter is more intuitive and easy to understand, so I enter +100m here, that is, I divided a 100M primary partition. When you look at it again, it's really more of a partition. The author then continues to repeat the previous operation, establishing 4 primary partitions. When I enter n again to create a partition, the result is wrong.

From this you will find that you can create up to 4 primary partitions in Linux, so what if you want to create more than a few partitions? It is easy to select an extended partition when you create a fourth partition after you create the third partition.

Delete the fourth primary partition first and then set up an extended partition

When setting up an extended partition, you will be asked how much you want to give to this extended partition, the author directly Enter, that is, all the space is assigned to this extended partition. This extended partition/DEV/HDB4 cannot write data to it, it is just a shell, and we continue to create partitions in this empty shell.

When you finish building the extended partition and then press N to create a new partition, you will notice that you are no longer prompted to build P or E, because we are no longer able to create p. What you need to understand here is that HDB5 is actually just a sub-partition in the HDB4, so far the available partitions are only 4, then the author creates a 5th partition.

Then press W to save, this mode automatically exits, if you do not want to save the partition information directly press Q to exit.

Let's delete the partition we just divided and re-establish the partition. How to delete you remember, right, just press D and choose the right number. Once all the partitions have been deleted, the disk is back to the same volume.

The first partition, we set up an extended partition. and give it 200M.

When a new partition is created again, it changes, not p or E, but P or L (logical partition). Also mentioned above, an extended partition is just a shell, under the extended partition can continue to partition small partitions, this small partition is actually a logical partition.

And this logical partition by default is starting from Word number 5, because the preceding numbers are either reserved for the primary partition or reserved for the extended partition. As a result, you can create up to 4 primary partitions in Linux, and you cannot add any partitions once you have created 4 primary partitions. You can also create up to one extended partition. How much can a logical partition under an extended partition create? The IDE's hard drive (like HDA, HDB, HDC, etc.) can create up to 10 (HDB5-HDB15), which is the result of the author's experiment. Some of the information that Linux under the logical partition is unlimited, and some say the maximum can be 64, as for the right, you need to go to a closer look, we do not need to study this problem in depth, there is no point.

Through the above operation, I believe you also learned to use FDISK to partition it. It is worth to put forward is, do not idle the partition play, the operation of the danger is very high, a inattention to the data on your server all the points are not. If there is a partition operation, then please keep 200% careful, remember!

MKFS.EXT2/MKFS.EXT3/MKE2FS Formatting Linux Hard Disk Partitioning

When you query the Help documentation for these three commands with man, you will find that we see the same help document, which means that three commands are the same. The common options are:

-B: Each chunk is set to occupy space when partitioning, currently supports 1024, 2048, and 4096 bytes per block.

-I: Set inode size

-N: Sets the number of inode, sometimes using the default inode number is not enough, so you have to set the inode number.

-C: Before formatting the disk to detect whether there is a problem, plus this option will be very slow

-L: Default label for this partition label

-J: Create EXT3 format partition, if you use MKFS.EXT3, you don't have to add this option.

Direct formatting without any options/dev/hdb1

In the example above, the block size of 4096 is changed by default to 1024, and the inode size is set to 4096.

The following example partitions the label name of the custom partition when it is partitioned.

E2label a label used to view or modify a partition ( label )

This command is simple, followed by the partition number directly, you can view the label of the partition, when you want to modify the label signature, the partition number followed by the desired label name.

fsck Check if the hard drive has bad lanes

Syntax: fsck [-aar] [partition]

-A: When you add this parameter, you do not need to follow the partition name as a parameter. It automatically checks all the partitions under the/etc/fstab file (which is done once during boot);

-A: Automatic repair check to the problematic partition;

-R: When a partition with a bad path is checked, the user is asked to decide whether to fix it.

When you use fsck to check if a disk has a bad path, the user will be prompted to "run this task may cause some mounted file system corruption", so this command does not run easily. Otherwise, the system will not even start.

" Mount / unmount Disk "

In the above content of the partition and format of the disk, then after the format is finished, how to use it? This involves mounting the disk. The formatted disk is actually a block device file, type B, you might think, since this block file is the partition, then directly in that file to write data is not written in that partition? Of course not.

Before you mount a partition, you need to set up a mount point, which appears in the form of a directory. Once a partition is attached to the mount point (directory), then the data is written to this directory, then it will be written to the partition. This requires you to note that the mount point (directory) must be an empty directory before the partition is mounted. In fact, the directory is not empty and does not affect the use of the mounted partition, but once mounted, then the contents of the previous things can not be seen. You can only see this partition if you uninstall it.

Mount Mounting Devices

Build the/test1/test2 directory first, and then create a 1.txt file in the/test1 directory.

Mount the/DEV/HDB1 partition to the/test1 directory, and then view the/test1 directory, 1.txt does not exist. When the data is written to the/test1 directory, it is written to the/DEV/HDB1 partition. When it comes to the-a option of mount, it is necessary to get a look at this file first/etc/fstab

This file is the individual partitions that need to be mounted when the system starts. The first column is the label of the partition, the second column is the mount point, the third column is the format of the partition, the fourth column is the Mount parameter, and so on, we will describe in detail what the parameters are, in general, write defaults directly, and the number of the fifth column indicates whether the dump is backed up. Yes, this is 1, otherwise it is 0; The sixth column is whether the self-test disk is turned on, that is, the fsck test that I just talked about. All means detection, 0 means no detection, in the Redhat, this is also a statement,/partition must be set to 1, and the entire fstab only allow a 1, there is a priority to say. 1:2 priority high, so first detect 1, and then detect 2, if there are multiple partitions need to boot detection then all set to 2 bar, 1 testing will be at the same time to detect 2. Here are the parameters that are commonly used in the fourth column.

Async/sync:async indicates that it is not synchronized with the disk and memory, and the system writes the memory data to disk at intervals, while sync synchronizes the memory and disk data at all times;

Auto/noauto: Boot automatically mounted/not automatically mounted;

Default: Set the mount definition according to the default value of most permanent file system, it contains RW, suid, dev, exec, auto, Nouser,async;

RO: Mount by read-only permission;

RW: Mount According to readable writable permission;

Exec/noexec: Allow/Do not allow executable file execution, but do not mount the root partition as NOEXEC, then can not use the system, even mount command can not be used, at this time only to re-do the system;

User/nouser: Allow/Do not allow other users to mount the partition outside the root, for security purposes, please use nouser;

Suid/nosuid: Allow/Disallow partition has suid attribute, general setting nosuid;

Usrquota: Start Consumer disk quota mode, disk quota related content in the following chapters will be introduced;

Grquota: Start the group disk quota mode;

After learning this/etc/fstab, we can modify the file ourselves, adding a line to mount the new partition. For example, the author adds such a line

/dev/hdb1/test1 ext3 defaults 0 0

The partition will then be mounted when the system restarts.

/etc/fstab, we're going to talk about this mount,mout-a. If you run this command, all the disk partitions that appear in the/etc/fstab are mounted. So when you add a line to the/etc/fstab file, you can simply run mount-a to mount the line you added, so you don't have to restart it.

You can use the MOUNT-O option to re-mount a partition and specify the options you want at the same time (that is, those in the Fstab sixth column above)

You see, with the RO option, you can't create a new file.

Re-mount once again is normal, if there is no other option, it is defaults.

I have encountered this situation in the daily operation and maintenance work, a server installed on the bright block disk, disk A (displayed on the server as SDC) and Disk B (shown as SDD on the server), once the two disks are unplugged, and then re-inserted, restart the machine, the results of the disk number Exchange, A becomes sdd,b and becomes SDC (this is because the disk is plugged into the wrong slot), the problem comes. From the top of the study, you mount the disk is by/dev/hdb1 such a partition name to mount, if previously added to the/etc/fstab, the result of the system will mount the wrong partition after boot. So how do you avoid such a situation?

blkid This command is used to display the disk partition UUID, the UUID is actually a large string of characters, in the Linux system each partition will have a unique UUID. Speaking of which, smart you think of it, no mistake, we use the only UUID to mount the disk partition.

This command is only used to display the UUID, no other purpose, so do not do a detailed introduction, of course, you can also follow a command behind a partition, only display the UUID of the partition.

See, in fact, it is very useful. So how do you make it boot up too? Simply write the command for the mount disk you just knocked directly to the/etc/rc.d/rc.local file. By the right, I have not yet told you the role of this rc.local file. Simply put, the commands in this file are executed when the system is started. So as long as you want to run after the command of all the commands are written to the file below it.

In fact, this file is a shell script, the author will be a separate chapter to introduce it. The "#" at the beginning of the line is the meaning of the comment, which means that the line does not take effect in this script. What commands do you want the system to run and what commands to write in here. Just like the one I just mounted. You can achieve this:

Mount also has a more commonly used option is-T, the type of the specified file system behind, such as the need to specify VFAT when mounting a floppy disk, and the need to specify iso9660, but in the author's experience over the years, the current system is smart to identify the system to mount the format of the partition category. In other words, you don't have to specify it, it will be automatically judged.

Umount Uninstalling the device

Now that you have learned how to mount a device, then how to uninstall a device, which will be used to umount, this command is also simple, behind can be with mount point, can also be followed by the partition name (/DEV/HDB1)

Sometimes you may encounter more difficult to uninstall the device, just like in Windows can not remove the USB stick, teach you a special method is Umount-l/dev/hdb1, this-l option has the meaning of forced unloading, you must remember oh, very useful.

" Create a Swap file "

From the installation of the system has been exposed to this swap, the front also said it is similar to Windows virtual memory, partition when the general size of memory twice times, if your memory more than 4G, then you divide 8G seems to be unnecessary. 4 G is enough for daily exchange. However, there is also a case where virtual memory is not enough. If you do, do you want to re-split the disk? Of course not! Then we'll add a virtual disk.

The basic idea is: Build Swapfile? Format in swap format? Enable the virtual disk

Using DD to create a 419M file/tmp/newdisk out, where if represents the file from which to read,/dev/zero is a Linux-specific 0 generator, of which the output to which file, BS is the block size, count defines how many blocks.

Mkswap This command is specially formatted swap format partition, this command must be seen clearly, otherwise the other partition to format the wrong only to cry.

Free is used to view system memory and virtual memory usage, and the-M option is viewed as M. You can see the current system. And Swapon is to enable our new swap file, enable and then use the free view found more than 400M.

We can also close the enabled swap file with Swapoff.

" Disk Quotas "

Disk mates are actually allocating a certain amount of disk to each user, allowing him to use only the amount of disk space in the range. In a Linux system, a multi-user multitasking environment, so there will be a lot of people sharing a disk situation. It is necessary for each user to limit a certain amount of disk space in order to appear fair.

In Linux, the thing that is used to manage disk quotas is quota. If you do not have quota on Linux, you need to install this package quota-3.13-5.el5.rpm (in fact, how much the version is irrelevant, the key is this package). Quota is limited in practice for the entire partition. If your/dev/hda3 is mounted in the/home directory, then the/home directory will be restricted.

Quota This module is mainly divided into quota quotacheck quotaoff Quotaon quotastats edquota setquota warnquota repquota These commands, respectively, described below.

Quota is used to display a limit for a group or a user.

Syntax: quota [-guvs] [User,group]

-G: Show limits for a group

-U: Displays the limit for a user

-V: Meaning of the display

-S: Select Inod or hard disk space to display

Quotacheck is used to scan the quota space of a particular disk.

Syntax: Quotacheck [-AUVG]/path

-A: Scans all already-mount disks with quota support

-U: Scan a user's files and directories

-G: Scan a group of files and directories

-V: Shows the scanning process

-m: Force a scan

Edquota is used to edit the quota value of a user or group.

Syntax: Edquota [-u user] [-G Group] [-T]

Edquota-p user-u User

-U: Edit a user's quota

-G: Edit the quota of a group

-T: Editing a wide time limit

-P: Copy the Quta of a user or group to another user or group

When running Edquota-u user, the system will open a file and you will see that there are 7 columns in the file, each of which means:

Filesystem: Disk partitions, such as/dev/hda3

Blocks: The amount of disk capacity that the current user occupies in the current filesystem, in kilobytes. The value should not be modified.

Soft/hard: The quota value of the current user in the filesystem, soft refers to the minimum limit, can exceed this value, but must be within the grace time to reduce the disk capacity below this value. Hard refers to the maximum limit, that is, the value cannot be exceeded. When the user's disk usage is higher than the soft value, the system warns the user that it wants to lower the usage space to the soft value within the grace period.

Inodes: The state of the inode that is currently used, without modification.

Quotaon start Quta, after editing good quota, need to start to be quta effective

Syntax: Quotaon [-A] [-UVG directory]

-A: All set quota start

-U: Start a user's quota

-G: Start a quota of a group

-S: Show related information

Quotaoff Close Quota

This command often has only one case quotaoff-a close all quota

The above tells a lot of quota related commands, then the next I teach you how to do in the practical application of this disk quota. The entire execution process is as follows:

First confirm that your/home directory is not mounted separately under a partition, with DF to view. If not, you need to do it with me. Otherwise this step can be omitted.

In the author's Linux system,/home does not occupy a single partition. Therefore, the/home directory needs to be mounted under a separate partition because the quota is limited to partitions.

I use Fdisk-l to view the current/DEV/HDB disk has 5 available partitions, so the author intends to mount the/DEV/HDB1 in the/home directory

See, the current author's/home directory is already a separate partition.

1) Set up test users

First, a test user is created, and a test group is created at the same time. You can have a line in/etc/passwd with the beginning of test, where the UID and GID are 500, and then set up a Test1 account, to add it to the test group, see/etc/passwd File Discovery Test and test1 the user's GID is 500. (Perhaps you are not familiar with/etc/passwd files, adding a user and the UID and GID concepts, no relationship, in a later chapter will be introduced, here just need you to understand)

2) Open the quota function of the disk

The default Linux does not have quota support for any partition, so we need to open the quota function of the disk manually, do you remember the quota option (Usrquota, Grpquota) When you analyzed the fourth column of the/etc/fstab file in the previous content? Yes, the quota support for opening this disk needs to be modified in this fourth column. Add a line with Vim editor/etc/fstab, such as:

The VIM command will be described in more detail in the following sections, which show you how to enter edit mode and how to save the file. If your Linux system already has the/home line, modify the fourth column directly, plus usrquota,grpguota (no spaces in the middle). Next, you need to mount home/home again.

In addition, you can also re-mount home/home

How do I see if I have quota enabled?

Just check to see if the row in the/etc/mtab file is Usrguota,grpquota. The author's/dev/hdb1 has now supported the quota

3) Scan the user status of the disk and produce important aquota.group and Aquota.user

This step requires Quotacheck, Aquota.group and Aqouta.user are the configuration files that are required for the group and the user's disk quota. If you do not have these two files, the disk quotas do not take effect.

When you first use the Quotacheck command, you are prompted to "cannot stat old user quota file ..." In fact, this is a hint that you do not have Aquota.user and aquota.group two files in the/home directory. No, because the disk quotas have not been configured before, and of course there are no two files. When the Quotacheck command is executed, the two files are generated in the/home directory.

4) Start Quota quota

5) Edit User disk quotas

First set the test account quota, and then directly copy the test quota to test1. We need to use the Edquota here.

The contents of the above revision changed to

Where the unit is KB, so the soft value is about 20mb,hard value of 30Mb, save this file, save it in the same way as vim a file.

Copy the quota for test to test1. The following continues to set the grace time.

The default is 7days here we change to 1days. Here's a look at the quotas for test and test1 users.

6) Edit Group disk quotas

Set the soft quota value for group test to 50M for the 40m,hard value. Below you see the quotas for group test.

7) Set Boot start

The command to start the disk quota is Quotaon-aug, so you just need to add this command to the/etc/rc.d/rc.local file to start the boot.

DF and Du commands for Linux Disk Management

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.