Linux fdisk 命令及实用示例

LinuxLinuxBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,你将学习如何使用 Linux 的 fdisk 命令来管理磁盘分区。fdisk 命令是一个强大的工具,允许你在 Linux 系统上创建、删除和调整磁盘分区的大小。本实验涵盖了 fdisk 命令的用途和语法,以及创建、删除和调整分区大小的实际示例。本实验适合需要管理 Linux 系统存储空间的用户。

Linux 命令速查表


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux(("Linux")) -.-> linux/UserandGroupManagementGroup(["User and Group Management"]) linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/SystemInformationandMonitoringGroup -.-> linux/dd("File Converting/Copying") linux/UserandGroupManagementGroup -.-> linux/sudo("Privilege Granting") subgraph Lab Skills linux/ls -.-> lab-422678{{"Linux fdisk 命令及实用示例"}} linux/dd -.-> lab-422678{{"Linux fdisk 命令及实用示例"}} linux/sudo -.-> lab-422678{{"Linux fdisk 命令及实用示例"}} end

理解 fdisk 命令的用途和语法

在这一步中,你将学习 Linux 中 fdisk 命令的用途和语法。fdisk 是一个命令行工具,用于创建、删除和管理磁盘分区。

首先,让我们了解 fdisk 命令的用途。fdisk 命令用于创建、删除和修改磁盘分区。它允许你创建新分区、调整现有分区的大小以及更改分区的类型。这在需要管理 Linux 系统存储空间时非常有用。

接下来,我们来看一下 fdisk 命令的基本语法:

sudo fdisk [options] [device]

以下是命令中各个部分的含义:

  • sudo:用于以提升的权限运行命令,因为 fdisk 命令需要 root 权限。
  • fdisk:命令的名称。
  • [options]:这些是可选的标志,用于自定义 fdisk 命令的行为。
  • [device]:这是你要操作的磁盘设备的名称,例如 /dev/sda

fdisk 命令的一些常见选项包括:

  • -l:列出指定设备的分区表。
  • -u:使用扇区作为测量单位,而不是柱面。
  • -c=dos:将兼容模式设置为 DOS。

示例输出:

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start      End  Sectors  Size Id Type
/dev/sda1        2048 41943039 41940992   20G 83 Linux

在这个示例中,fdisk -l /dev/sda 命令列出了 /dev/sda 设备的分区表,这是一个 20 GB 的虚拟磁盘,包含一个 Linux 分区。

使用 fdisk 命令创建新分区

在这一步中,你将学习如何使用 fdisk 命令在 Linux 系统上创建一个新分区。

首先,让我们列出 /dev/sda 设备上的当前分区:

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start      End  Sectors  Size Id Type
/dev/sda1        2048 41943039 41940992   20G 83 Linux

如你所见,当前只有一个分区 /dev/sda1,它占用了整个 20 GB 的磁盘空间。

现在,让我们使用 fdisk 命令创建一个新分区:

$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2):
First sector (2048-41943039, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039): +10G

Created a new partition 2 of type 'Linux' and of size 10 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

在这个示例中,我们:

  1. 通过运行 sudo fdisk /dev/sda 进入 fdisk 交互模式。
  2. 按下 n 选择创建一个新分区。
  3. 选择主分区 (p) 作为分区类型。
  4. 接受默认的分区编号 2
  5. 接受默认的起始扇区 2048
  6. 指定结束扇区为 +10G,以创建一个 10 GB 的分区。
  7. 按下 w 将更改写入磁盘。

示例输出:

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1        2048  41943039  41940992    20G 83 Linux
/dev/sda2    41943040  61071359  19128320    10G 83 Linux

如你所见,新的分区 /dev/sda2 已经创建,大小为 10 GB。

使用 fdisk 命令删除和调整分区大小

在这一步中,你将学习如何使用 fdisk 命令删除和调整分区大小。

首先,让我们列出 /dev/sda 设备上的当前分区:

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1        2048  41943039  41940992    20G 83 Linux
/dev/sda2    41943040  61071359  19128320    10G 83 Linux

现在,让我们删除 /dev/sda2 分区:

$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

在这个示例中,我们:

  1. 通过运行 sudo fdisk /dev/sda 进入 fdisk 交互模式。
  2. 按下 d 选择删除一个分区。
  3. 指定分区编号 2 以删除 /dev/sda2 分区。
  4. 按下 w 将更改写入磁盘。

现在,让我们调整剩余的 /dev/sda1 分区大小,使其占用整个磁盘:

$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): d
Partition number (1,2, default 1): 1

Partition 1 has been deleted.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039):

Created a new partition 1 of type 'Linux' and of size 20 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

在这个示例中,我们:

  1. 按下 d 并输入分区编号 1 以删除 /dev/sda1 分区。
  2. 按下 n 创建一个新的主分区 (p)。
  3. 接受默认的分区编号 1
  4. 接受默认的起始扇区 2048
  5. 接受默认的结束扇区 41943039,以使用整个 20 GB 磁盘。
  6. 按下 w 将更改写入磁盘。

示例输出:

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1        2048  41943039  41940992    20G 83 Linux

如你所见,/dev/sda1 分区现在占用了整个 20 GB 磁盘。

总结

在本实验中,你学习了 Linux 中 fdisk 命令的用途和语法。fdisk 是一个命令行工具,用于创建、删除和管理磁盘分区。你探索了 fdisk 命令的基本语法,包括使用 sudo 以提升的权限运行命令、可用的不同选项,以及如何列出特定设备的分区表。此外,你还学习了如何使用 fdisk 命令创建新分区,以及如何删除和调整现有分区的大小。

Linux 命令速查表