linux fdisk分区
The fdisk command is a text-based utility for viewing and managing hard disk partitions on Linux. It’s one of the most powerful tools you can use to manage partitions, but it’s confusing to new users.
fdisk命令是基于文本的实用程序,用于在Linux上查看和管理硬盘分区。 它是可用于管理分区的最强大的工具之一,但是它使新用户感到困惑。
This tutorial will go through the basics of using fdisk to manage a partition table. After using fdisk, you’ll have to use a mkfs command to format new partitions with a file system.
本教程将介绍使用fdisk管理分区表的基础知识。 使用fdisk之后,必须使用mkfs命令来格式化文件系统中的新分区。
苏多vs苏 (Sudo vs. Su)
On Ubuntu, Linux Mint or other Ubuntu-derived distributions, the fdisk and mkfs commands must be prefixed with sudo. On distributions that don’t use sudo, use the su – command first to get a root shell, then type every command without sudo.
在Ubuntu,Linux Mint或其他Ubuntu衍生发行版上,fdisk和mkfs命令必须以sudo为前缀。 在不使用sudo的发行版上,首先使用su –命令获取root shell,然后键入每个不带sudo的命令。
列出分区 (List Partitions)
The sudo fdisk -l commands lists the partitions on your system.
sudo fdisk -l命令列出了系统上的分区。
You can add a disk’s device name to list only partitions on it. For example, use the following command to only list partitions on the first disk device:
您可以添加磁盘的设备名称,以仅列出磁盘上的分区。 例如,使用以下命令仅列出第一个磁盘设备上的分区:
sudo fdisk -l /dev/sda
须藤fdisk -l / dev / sda
进入命令模式 (Entering Command Mode)
To work on a disk’s partitions, you have to enter command mode. You’ll need the device name of a disk from the fdisk -l command. The following command enters command mode for the first disk device:
要在磁盘分区上工作,必须进入命令模式。 您需要从fdisk -l命令中获取磁盘的设备名称。 以下命令为第一个磁盘设备进入命令模式:
sudo fdisk /dev/sda
须藤fdisk / dev / sda
Don’t edit partitions while they’re in use. If you want to edit system partitions, boot from a live CD first.
在使用分区时不要编辑分区。 如果要编辑系统分区,请先从实时CD引导。
使用命令模式 (Using Command Mode)
In command mode, you use single-letter commands to specify actions you want to take. Type m and press Enter to see a list of the commands you can use.
在命令方式下,使用单字母命令来指定要执行的操作。 键入m并按Enter键以查看可以使用的命令列表。
查看分区表 (Viewing the Partition Table)
Use p to print the current partition table to the terminal from within command mode.
使用p在命令模式下将当前分区表打印到终端。
删除分区 (Deleting a Partition)
Use the d command to delete a partition. You’ll be asked for the number of the partition you want to delete, which you can get from the p command. For example, if I wanted to delete the partition at /dev/sda5, I’d type 5.
使用d命令删除分区。 系统将询问您要删除的分区号,您可以从p命令中获取该分区号。 例如,如果要删除/ dev / sda5上的分区,请输入5 。
After deleting the partition, you can type p again to view the current partition table. The partition appears deleted, but fdisk doesn’t write these changes to disk until you use the w command.
删除分区后,您可以再次键入p以查看当前分区表。 该分区显示为已删除,但是fdisk不会将这些更改写入磁盘,直到您使用w命令为止。
创建分区 ( Creating a Partition)
Use the n command to create a new partition. You can create a logical or primary partition (l for logical or p for primary). A disk can only have four primary partitions.
使用n命令创建一个新分区。 您可以创建逻辑分区或主分区( l表示逻辑分区, p表示主分区)。 磁盘只能有四个主分区。
Next, specify the sector of the disk you want the partition to start at. Press Enter to accept the default sector, which is the first free sector on the disk.
接下来,指定要启动分区的磁盘的扇区。 按Enter接受默认扇区,它是磁盘上的第一个空闲扇区。
Last, specify the last sector of the partition on the disk. If you want to use up all available space after the initial sector, just press Enter. You can also specify a specific size, such as +5G for a five gigabyte partition or +512M for a 512 megabyte partition. If you don’t specify a unit after the + sign, fdisk uses sectors as the unit. For example, +10000 results in the end of the partition being 10000 sectors after its beginning.
最后,指定磁盘上分区的最后一个扇区。 如果要耗尽初始扇区之后的所有可用空间,只需按Enter。 您还可以指定特定大小,例如对于5 GB分区为+ 5G ,对于512 MB分区为+ 512M 。 如果未在+号后面指定单位,则fdisk使用扇区作为单位。 例如, + 10000导致分区的末尾是分区开始后的10000个扇区。
系统编号 (System ID)
The n command I just ran recreated the swap partition I deleted earlier — or did it? If I run the p command again, I’ll see that the new /dev/sda5 partition is a “Linux” partition instead of a “Linux swap” partition.
我刚刚运行的n命令重新创建了我先前删除的交换分区-还是这样做? 如果再次运行p命令,将会看到新的/ dev / sda5分区是“ Linux”分区,而不是“ Linux swap”分区。
If I want to change its type, I can use the t command and specify the partition’s number.
如果要更改其类型,可以使用t命令并指定分区号。
I’ll be asked for the hex code of the type. I don’t know it, so I can type L to view a list of hex codes.
系统会要求我输入该类型的十六进制代码。 我不知道,所以我可以输入L来查看十六进制代码列表。
It says 82 is the code for Linux swap partitions, so I can type that.
它说82是Linux交换分区的代码,所以我可以输入。
This doesn’t format the partition with the file system you select. You’ll have to do that later with the appropriate mkfs command.
这不会使用您选择的文件系统格式化分区。 您稍后必须使用适当的mkfs命令执行此操作。
写作变化 (Writing Changes)
Use w to write the changes you’ve made to disk.
使用w写入对磁盘所做的更改。
Use q if you want to quit without saving changes.
如果要退出而不保存更改,请使用q 。
格式化分区 (Formatting a Partition)
You must format new partitions with a file system before you can use them. You can do this with the appropriate mkfs command. For example, this command formats the fifth partition on the first disk with the ext4 file system.
您必须先使用文件系统格式化新分区,然后才能使用它们。 您可以使用适当的mkfs命令执行此操作。 例如,此命令使用ext4文件系统格式化第一个磁盘上的第五个分区。
sudo mkfs.ext4 /dev/sda5
须藤mkfs.ext4 / dev / sda5
Use the mkswap command if you want to format a partition as a swap partition:
如果要将分区格式化为交换分区,请使用mkswap命令:
sudo mkswap /dev/sda5
须藤mkswap / dev / sda5
Fdisk contains a variety of other commands, including expert commands you can access by running the x command first. Check out fdisk’s man page with the man fdisk command for more detailed information.
Fdisk包含各种其他命令,包括您可以通过先运行x命令来访问的专家命令。 使用man fdisk命令查看fdisk的手册页,以获取更多详细信息。
翻译自: https://www.howtogeek.com/106873/how-to-use-fdisk-to-manage-partitions-on-linux/
linux fdisk分区