Liunx disk management Command records and liunx disk Command records

Source: Internet
Author: User

Liunx disk management Command records and liunx disk Command records


Linux disk management is directly related to the performance of the entire system.

Linux disk management commonly uses three commands: df, du, and fdisk.

  • Df: list the total disk usage of the file system
  • Du: Check disk space usage
  • Fdisk: used for disk partitioning


Today, let's talk about how to use the fdisk and parted partitioning tools for hard disk partitioning. Next I will talk about how to use these two hard disk partitioning tools in Linux:
---------- Fdisk partitioning tool ----------
In my other article has already said the concept of Hard Disk Partition in Linux system (, so here I don't say much, have don't understand can look at that article
I. The command format of the fdisk partitioning tool is described below:
Fdisk is a traditional hard disk partitioning tool in Linux and one of the most commonly used hard disk partitioning tools in Linux. Its command format is as follows: 740) this. width = 740; "border = 0> the common command options are described as follows:
-B sectorsize: defines the disk sector size. Valid values include 512, 1024, and 2048. This option is only valid for earlier Linux versions.
-C cyls: defines the number of cylinders of a disk. Generally, you do not need to define it. Use the default one.
-H heads: defines the number of disks used by the partition table, generally 255 or 16.
-S sects: defines the number of sectors of each track, usually 63.
-L: This is the most common command option. It displays the partition table information of the specified disk device. For example, to view the partition table information of the disk device/dev/sdb1, it is: fdisk-l/dev/sdb1
-U: When the partition table is displayed, the slice replaces the cylinder as the display unit.
-S partition: displays the partition size in block units in the standard output.
-V: displays the fdisk version.
Device: the device name of the disk. The device name of the IDE type is [dev/hd [a-h]. the name of a SCSI or SATA disk device is [dev/sd [a-p].
If I want to view the partition table information of the first disk on my computer (I just had a hard disk), run the following command: fdisk-l/dev/sda (my hard disk is SATA type) display information 740) this. width = 740; "border = 0>
Then we can take a look at their respective disk device files:
Use the command ll/dev/sda * to view 740) this. width = 740; "border = 0> you can try other commands on your own. Due to time issues, I will not talk about them here ~! Next let's take a look at how to use the fdisk tool to partition hard disks.
Ii. fdisk interaction mode:
After the command "fdisk + device name" is used, we can enter the interaction mode of the fdisk program, in interactive mode, you can input the Command provided by the fdisk program to complete the corresponding operation. The result is 740) this. width = 740; "border = 0> when entering this mode, we can run the fdisk command to manage the corresponding disk partitions, enter m to obtain the fdisk Command help information: 740) this. width = 740; "border = 0> let's take a look at the instructions:
A: Set the bootable flag.
B: Modify the bsd disk label.
C: Set the DOS operating system compatibility mark
D: delete a partition.
L: display the known partition types, of which 82 are Linux swap partitions and 83 are Linux partitions.
M: displays help information.
N: Add a new partition.
O: Create a new blank DOS partition table
P: displays the current Partition Table of the disk.
Q: exit the fdisk program without saving any modifications.
S: Create a new blank Sun disk tag
T: Change the system number of a partition (for example, change the Linux Swap partition to a Linux partition)
U: Change the display record Unit
V: Verify the disk partition table
W: Save the Modification result and exit the fdisk program.
X: special functions, not recommended for beginners
With the meaning of the above commands, we can say that we will partition a hard disk. The following is an example:
First, I add a hard disk of 170 GB to the virtual machine, and then partition it:
1. first, enter the "fdisk-l" command to check whether the hard disk is added, and then enter the command "fdisk/dev/sdb" to enter the fdisk program interaction mode, such as: 740) this. width = 740; "border = 0>
2. enter the command "n" to create a new partition. At this time, the system will ask you to enter whether to create a new primary partition or an extended partition. I select primary partition (p ), then, enter the Partition Number (because I am a new hard disk and have not been partitiOned, So input 1 indicates the first primary partition), and then start the cylindrical disk. Select the default one. Finally, enter the size of your partition. Here I am dividing about 50 GB! You can run the p command to check the partition, for example, 740) this. width = 740; "border = 0>
3. this time I create another extended partition. First, enter the command n to create a new partition, then enter e to select the extended partition, and then enter the Partition Number (2 ), then the following options are selected by default! Run the command p to check the setting. 740) this. width = 740; "border = 0>
4. next, I will create two logical partitions in the extended partition. First, enter the command n, then select the start column of the partition type as l (logical partition), select the default column, and then enter the size of the logical partition, I just got 40 GB. Finally, enter the command p to check whether the creation is successful, 740) this. width = 740; "border = 0>
5. Create another logical partition in the same step. Next, let's take a look at how to modify the type of a partition.
6. enter the command t to modify the partition type (you only need to enter the ID number corresponding to the partition type), and then enter the partition number to be modified, here is the modified sdb5 (that is to say, input 5 is OK ). Finally, enter the ID number of the partition type. If you do not know the ID number of the partition type, you can enter the "l" command to view the ID number of the partition type. 740) this. width = 740; "border = 0> 7. after finding the ID number of the partition type, you can enter the corresponding ID number to modify the partition type. Here I will change the Linux partition to the Linux swap type, we can see that the ID of the Linux swap type is 82. Enter 82 and press enter to complete the modification. You can use the command p to check it, 740) this. width = 740; "border = 0>
8. the above is the entire process of partitioning the hard disk using the fdisk partitioning tool. Oh, don't forget the last step. You have to enter the command w to save your operations, after saving, you can use fdisk-l/dev/sdb to check whether the partition is successful. 9. We have partitioned the hard disk. After partitioning the hard disk, it cannot be used directly. It must be formatted before it can be used. Below we will format the hard disk. Enter "mkfs. ext3" to format the hard disk into an ext3 file system. For more information about how to use the mkfs command, you can use the man mkfs command. 10. input mkfs. the ext3/dev/sdb1 command is used to format the hard disk. Only one partition can be formatted. A special partition is swap, run mkswap/dev/sdb5 to format the swap partition. For example: 740) this. width = 740; "border = 0> 740) this. width = 740;" border = 0>
11. After formatting the file system, you need to mount the disk before it can be used properly. The mount command is in the following format:
Mount [-t file system type] storage device mount point (usually the file system type can be omitted and automatically recognized by the System)
12. before mounting, you need to create a new mount point, that is, where you want to mount the disk, and then you can set up the mount point. For example, in the above example, if I want to mount the/dev/sdb1 partition, I will first create a new mount point: mkdir/media/sdb1, and then execute: mount/dev/sdb1/media/sdb1 mount point names can be set as needed. After mounting, you can write something into the disk, 740) this. width = 740; "border = 0>
13. for convenience, we can also modify the relevant configuration files so that the system can automatically mount a partition, specifically by modifying the/etc/fstab configuration file, in the preceding example, the/dev/sdb1 partition is configured to be automatically mounted to/media/sdb1 each time the system is started. You can modify the configuration file 740) this. width = 740; "border = 0>
14. As shown above, when the system starts every time, it will automatically mount the/dev/sdb1 partition to/media/sdb1, instead of manually mounting each time.
15. If you do not want to detach a partition, run the "umount + device mount point" command to detach it. For example, run the umount/media/sdb1 command to uninstall the SDK.
Note: Sometimes uninstallation may fail. The reason for this is that other users or processes are accessing the file system. In Linux, you are not allowed to detach a file system that is being accessed. Only when all users or processes accessing the file system complete the operation and exit, this file system can be uninstalled normally. We have two methods to deal with this situation. One is to run the "lsof + mount point" command to check which processes are accessing the file system, run the kill command to kill the process and uninstall it. The second is to use the command "fuser-k + mount point ". The "-k" option is used to find out the processes that are accessing the file system using the command fuser. The "-k" option is used to find and kill the processes. The above two methods can be used to uninstall some file systems that cannot be detached.
---------- Parted partition tool ----------
Parted is another partition software that comes with RHEL5. It is more convenient to use than fdisk, and it also provides the function of dynamically adjusting the partition size. Next I will show you how to create and delete Disk Partitions through parted, view partition tables, change the partition size, create a file system, and use the parted interactive mode.
1. Introduction to parted:
Parted is another commonly used partition software in linux. It supports a wide range of partition types, including ext2, ext3, linux-swap, FAT, FAT32, reiserfs, HFS, jsf, ntfs, ufs, and xfs. The command format is as follows:
Parted [options] [device [command [options...]
The Command Options are described as follows:
-H: displays help information.
-I: interactive mode.
-S: Script Mode.
-V: displays the version of parted.
Device: the name of the disk device, for example,/dev/sda.
Command: parted command. If no command is set, parted enters the interaction mode.
Ii. parted interaction mode:
Similar to fdisk, parted can use the command "parted + device name" to enter the interaction mode. After entering the interactive mode, you can manage Disk Partitions using various commands of parted, which is very similar to fdisk. I also take the hard disk on my computer as an example. It is the interface for entering the parted interactive mode and entering the "help" Command help :-_-! Khan, there is a picture missing here. Maybe I didn't copy it... You only need to enter "parted/dev/A device" in the terminal to enter the parted interaction mode.
Various operation commands and detailed descriptions of parted are as follows:
Check NUMBER to check the file system
Cp [FROM-DEVICE] FROM-number to-NUMBER: copy the file system TO another partition.
Help [COMMAND] displays all help information or the help information of a specified COMMAND.
Mklabel and mktable LABEL-TYPE create a new disk LABEL in the Partition Table
Mkfs NUMBER FS-TYPE creates a file system of the specified type on the partition
Mkpart PART-TYPE [FS-TYPE] start end creates a partition
Mkparts PART-TYPE [FS-TYPE] start end creates a partition and creates the specified file system on the partition
Move number start end move Partition
Name number name name the partition NUMBER with the specified NAME
Print [free | NUMBER | all]: Display partition tables, specified partitions, or all devices
Quit exits the parted program.
Rescue start end repairs lost partitions
Resize number start end change partition size
Rm NUMBER Delete Partition
Select DEVICE select the DEVICE to be changed
Set number flag state Change partition FLAG
Toggle [NUMBER [FLAG] sets or removes the partition tag
Version: displays the version of parted.
Iii. Partition Management
1. View partitions
In parted interactive mode, enter the print command to view the partition (equivalent to the p command of fdisk), 740) this. width = 740; "border = 0> 2. Create a partition
In parted mode, enter the mkpart command to create a disk partition. If you want to create a logical partition with the starting position of 50 GB, the ending position of 100 GB, and the file system type of ext3, you can use the following command to create an instance:
Mkpart logical ext3 50G 100G 740) this. width = 740; "border = 0>
Of course, you can also enter only the mkpart command without any parameters. parted will prompt the user step by step to enter the relevant information and finally complete partition creation, 740) this. width = 740; "border = 0> 3. create a File System
After creating a partition, you can use the mkfs command to create a file system (that is, format) on the partition. Currently, parted only supports ext2 file systems, but does not support ext3, 740) this. width = 740; "border = 0>
4. Change the partition size
You can use the resize command to change the size of a specified partition. To change the size of a partition, the file system must have been created. Otherwise, the system will prompt: Error: cocould not detect file system. below I will take dev/sdb6 as an example to narrow down 22 GB, because I have used up the disk, so I cannot add it! Reduce it! 740) this. width = 740; "border = 0>
5. delete partitions
You can use the rm command to delete a specified disk partition. You must detach the partition before deleting it, for example, if you want to delete/dev/sdb6, enter the "rm" command in parted interaction mode and the partition number to be deleted. 740) this. width = 740; "border = 0> ...... Shit! It's not easy. I'm exhausted. -_-! Go to bed! If you have any questions, please leave me a message ......~~~~~~~~~~~~~~~~~

7.2.1 hard disk partitioning

In the past, PC hard disks have been using 32-bit PC-BIOS partition tables and Master Boot records (MBR), so the partition size cannot exceed 2 TB, and each hard disk can only contain up to 4 Master partitions. Using extended partitions breaks the limit of four primary partitions. To address the limit of 2 TB, GPT (GUID Partition Tables, GUID Partition Table) is gradually used instead of PC-BIOS Partition table.

Fdisk was a standard command for operating disk partitions. However, because fdisk cannot operate on GPT partitions, it is no longer necessary. Instead, the parted command is more powerful and supports more actively.

Note: If you prefer to use graphic interface tools to partition hard disks, adjust the partition size, and perform other hard disk operations, try gparted or qtparted partitioning tools. The commands of these two tools are the same as those of the software package.

1. Use fdisk to modify Disk Partitions

The fdisk command can list and modify disk partitions. Remember, modifying or deleting a partition may remove important data. You must be aware of the changes you have made before writing data to the disk. Enter the following command as root to list information about the hard disk partition:

The command output shows that the 80 GB hard disk is divided into three partitions. The first (/dev/sda1) is a small/boot partition and is configured as the Linuxext3 File System (Id 83 ). Note: The asterisk (*) indicates that the partition is a bootable partition. The second partition is the root file system partition, which also uses the ext3 file system. The last partition is the swap partition (Linux swap ).

Note that in Fedora 7 and later versions, both IDE and SCSI disks adopt/dev/sd? Device name, where? Represents a letter (a, B, c, etc ). In RHEL5 and earlier versions of Fedora, only SCSI disks and USB disks use/dev/sd ?, /Dev/hd? Is Used for IDE hard disks ?.

If multiple disks exist, fdisk-l will list all disks, unless you explicitly specify the disk to be viewed:

When using the fdisk command, you can directly specify a disk without other options to operate the specified Disk:

After a prompt is displayed, you can use the commands listed above to operate the hard disk. Specifically, you can use p (the display result is the same as fdisk-l), n (create new partition), d (delete existing partition), and l (List known file system types) or t (change the partition file system type ). The following example describes several fdisk commands:

Unless explicitly specified, the new partition uses the Linux ext3 File System (83) by default ). The L command is equivalent to the l command, and can display the file system type and hexadecimal code. As shown above, type 82 to specify the partition as swap. Other common Linux partitions include Linux extensions (85), Linux LVM (8e), Linux software raid (fd), and EFI/GP (ee ).

Windows partition can be specified as HPFS/NTFS (7), Windows 95 FAT32 (B), FAT16 (6), or Windows 95 FAT32 LBA (c ). Other Unix file systems include Minix (be or bf), BSD/OS (e4), FreeBSD (ee), OpenBSD (ef), NeXTSTEP (f0), and Darwin UFS (f1) and NetBSD (f4 ). These file systems may be useful, for example, when you need to restore the backup storage media using the above file system.

So far, the partition table has not been changed. If you are sure that the preceding settings are correct, type w to write the preceding changes to the partition table. If you want to discard all the changes (or exit after writing the Partition Table), you can directly type q to exit the fdisk session.

2. Use sfdisk to copy a partition table

You can use sfdisk to back up or copy a disk partition table:

3. Use parted to modify Disk Partitions

Like fdisk, parted can also list or modify disk partitions. In addition, parted has some other practical functions. The following example uses parted to list partitions:

From the command output, we can see whether the traditional msdos disk label (Partition Table) or gpt label is used on the disk. Here is the msdos partition table.

Type parted, followed by the name of the storage device to be operated (such as/dev/sda), and then run parted in interactive mode. If there is only one storage device, you can directly type parted:

When using parted in interactive mode, you can enter the entire command, or you can only enter the first part of the character, and use the Tab key to complete the entire command (the same as in bash shell ). Once you are familiar with these commands, you only need to enter a certain number of letters so that parted can guess which command to enter. Similar to CiscoiOS, p indicates print, and mkl indicates mklabel.

The warning is different from that of fdisk. The changes made to the partition using parted take effect immediately without being written to the disk. In short, do not mistakenly think that all changes can be undone by directly exiting parted.

For commands in a parted session, you can either type all parameters (for example, mkpart logical ext3 10.7 GB 17.0 GB) when entering the command, or enter only the command itself (mkpart ), parted guides subsequent operations in interactive mode:

Try not to use mkpartfs. This command cannot correctly create the ext3 partition. You can use mkpart instead to create an ext3 partition (as shown above). After exiting parted, use the mkfs. ext3 command to format the partition. When creating a new partition, you often need to adjust the size of common Linux partitions, for example:

Warning If LVM is not used, the above command will destroy the entire file system.

You can use the ntfsresize command to adjust the NTFS partition size. In Fedora, this command is affiliated with ntfsprogs software package. This package also contains commands such as creating (mkfs. ntfs), repairing (ntfsfix), and obtaining NTFS partition information (ntfsinfo ).

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: 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.