网站搜索

管理 Linux 磁盘分区的 10 个 fdisk 命令


fdisk 代表(“固定磁盘”或格式化磁盘“)是最常用的基于命令行的磁盘操作实用程序,用于 Linux/Unix 系统。借助 fdisk 命令,您可以使用其用户友好的基于文本的菜单驱动界面来查看、创建、调整大小、删除、更改、复制和移动硬盘驱动器上的分区。

该工具在为新分区创建空间、为新驱动器组织空间、重新组织旧驱动器以及将数据复制或移动到新磁盘方面非常有用。它允许您根据系统中的硬盘大小创建最多四个新的分区和逻辑(扩展)分区数量。

本文介绍了在基于 Linux 的系统中管理分区表的 10 个基本 fdisk 命令。您必须是root用户才能运行fdisk命令,否则您将收到“命令未找到”错误。

1、查看Linux中所有磁盘分区

以下基本命令列出系统上所有现有的磁盘分区。 “-l”参数代表(列出所有分区)与 fdisk 命令一起使用来查看 Linux 上的所有可用分区。分区按其设备名称显示。例如:/dev/sda/dev/sdb/dev/sdc

[[email  ~]# fdisk -l

Disk /dev/sda: 637.8 GB, 637802643456 bytes
255 heads, 63 sectors/track, 77541 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        2624    20972857+  83  Linux
/dev/sda3            2625        4582    15727635   83  Linux
/dev/sda4            4583       77541   586043167+   5  Extended
/dev/sda5            4583        5887    10482381   83  Linux
/dev/sda6            5888        7192    10482381   83  Linux
/dev/sda7            7193        7845     5245191   83  Linux
/dev/sda8            7846        8367     4192933+  82  Linux swap / Solaris
/dev/sda9            8368       77541   555640123+  8e  Linux LVM

2、Linux中查看具体磁盘分区

要查看特定硬盘的所有分区,请使用选项“-l”和设备名称。例如,以下命令将显示设备/dev/sda的所有磁盘分区。如果您有不同的设备名称,只需将设备名称写为/dev/sdb/dev/sdc

[[email  ~]# fdisk -l /dev/sda

Disk /dev/sda: 637.8 GB, 637802643456 bytes
255 heads, 63 sectors/track, 77541 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        2624    20972857+  83  Linux
/dev/sda3            2625        4582    15727635   83  Linux
/dev/sda4            4583       77541   586043167+   5  Extended
/dev/sda5            4583        5887    10482381   83  Linux
/dev/sda6            5888        7192    10482381   83  Linux
/dev/sda7            7193        7845     5245191   83  Linux
/dev/sda8            7846        8367     4192933+  82  Linux swap / Solaris
/dev/sda9            8368       77541   555640123+  8e  Linux LVM

3. 检查所有可用的 fdisk 命令

如果您想查看 fdisk 可用的所有命令。只需通过提及硬盘名称(例如 /dev/sda )来使用以下命令,如下所示。以下命令将为您提供类似于以下的输出。

[root@tecmint ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help):

输入“m”即可查看可在/dev/sda硬盘上操作的fdisk的所有可用命令列表。之后,我在屏幕上输入“m”,您将看到可在 /dev/sda 设备上使用的 fdisk 的所有可用选项。

[root@tecmint ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help):

4.打印Linux中所有分区表

要打印硬盘的所有分区表,您必须处于特定硬盘的命令模式,例如/dev/sda

[root@tecmint ~]# fdisk /dev/sda

在命令模式下,输入“p”,而不是像我们之前那样输入“m”。当我输入“p”时,它将打印特定的/dev/sda分区表。

Command (m for help): p

Disk /dev/sda: 637.8 GB, 637802643456 bytes
255 heads, 63 sectors/track, 77541 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        2624    20972857+  83  Linux
/dev/sda3            2625        4582    15727635   83  Linux
/dev/sda4            4583       77541   586043167+   5  Extended
/dev/sda5            4583        5887    10482381   83  Linux
/dev/sda6            5888        7192    10482381   83  Linux
/dev/sda7            7193        7845     5245191   83  Linux
/dev/sda8            7846        8367     4192933+  82  Linux swap / Solaris
/dev/sda9            8368       77541   555640123+  8e  Linux LVM

Command (m for help):

5. 如何在Linux中删除分区

如果您想从特定硬盘(例如/dev/sda)中删除特定分区(即/dev/sda9)。您必须处于 fdisk 命令模式才能执行此操作。

[root@tecmint ~]# fdisk /dev/sda

接下来,输入“d”从系统中删除任何给定的分区名称。当我输入“d”时,它会提示我输入要从/dev/sda硬盘中删除的分区号。假设我在这里输入数字“4”,那么它将删除分区号“4”(即/dev/sda4)磁盘并显示空闲分区表中的空间。输入“w”将表写入磁盘并在对分区表进行新更改后退出。新的更改只会在下次重新启动系统后才会发生。从下面的输出中可以很容易地理解这一点。

[root@tecmint ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): d
Partition number (1-4): 4

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
You have new mail in /var/spool/mail/root

警告:执行此步骤时要小心,因为使用选项“d”将从系统中完全删除分区,并可能丢失分区中的所有数据。

6. 如何在 Linux 中创建新分区

如果您的一台设备上还有剩余空间,请输入 /dev/sda 并希望在其下创建一个新分区。那么你必须处于/dev/sda的fdisk命令模式。键入以下命令进入特定硬盘的命令模式。

[root@tecmint ~]# fdisk /dev/sda

进入命令模式后,现在按“n”命令在/dev/sda下创建一个特定大小的新分区。这可以借助以下给定输出来证明。

[root@tecmint ~]# fdisk  /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e

创建新分区时,它会询问您两个选项“扩展”或“”分区创建。按“e”键进行扩展分区,按“p”键进行主分区。然后它会要求您输入以下两个输入。

  1. 要创建的分区的第一个柱面号。
  2. 要创建的分区的最后柱面编号(最后柱面、+柱面或+大小)。

您可以通过在最后一个圆柱体中添加“+5000M”来输入圆柱体的大小。这里,“+”表示加法,5000M表示新分区的大小(即5000MB)。请记住,创建新分区后,您应该运行“w”命令来更改分区表并将新更改保存到分区表中,最后重新启动系统以验证新创建的分区。

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

7. 如何在 Linux 中格式化分区

创建新分区后,不要跳过使用“mkfs”命令格式化新创建的分区。在终端中键入以下命令以格式化分区。这里/dev/sda4是我新创建的分区。

[root@tecmint ~]# mkfs.ext4 /dev/sda4

8. 如何在 Linux 中检查分区的大小

格式化新分区后,使用 fdisk 命令使用标志“s”(以块为单位显示大小)检查该分区的大小。这样您就可以检查任何特定设备的大小。

[root@tecmint ~]# fdisk -s /dev/sda2
5194304

9. 如何修复分区表顺序

如果您删除了逻辑分区并再次重新创建它,您可能会注意到“分区无序”问题或错误消息,例如“分区表条目未按磁盘顺序排列” '。

例如,当删除三个逻辑分区(例如(sda4sda5sda6))并创建新分区时,您可能期望新分区分区名称为sda4。但是,系统会将其创建为 sda5。发生这种情况的原因是,删除分区后,sda7 分区已被移动为 sda4 且可用空间移至末尾。

要解决此类分区顺序问题,并将 sda4 分配给新创建的分区,请发出“x”以输入额外功能部分,然后输入“f<”' 专家命令修复分区表的顺序,如下所示。

[root@tecmint ~]# fdisk  /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): x

Expert command (m for help): f
Done.

Expert command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

之后,运行'f'命令,不要忘记运行'w'命令保存并退出fdisk命令模式。一旦修复了分区表顺序,您将不再收到错误消息。

10. 如何禁用分区的引导标志(*)

默认情况下,fdisk 命令显示每个分区上的引导标志(即“*”)符号。如果要启用或禁用特定分区上的引导标志,请执行以下步骤。

[root@tecmint ~]# fdisk  /dev/sda

按'p'命令查看当前分区表,可以看到/dev/上有一个启动标志(橙色的星号(*)符号) sda1磁盘如下图。

[root@tecmint ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/sda: 637.8 GB, 637802643456 bytes
255 heads, 63 sectors/track, 77541 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        2624    20972857+  83  Linux
/dev/sda3            2625        4582    15727635   83  Linux
/dev/sda4            4583       77541   586043167+   5  Extended
/dev/sda5            4583        5887    10482381   83  Linux
/dev/sda6            5888        7192    10482381   83  Linux
/dev/sda7            7193        7845     5245191   83  Linux
/dev/sda8            7846        8367     4192933+  82  Linux swap / Solaris
/dev/sda9            8368       77541   555640123+  8e  Linux LVM

接下来输入命令“a”以禁用启动标志,然后在我的例子中输入分区号“1”(即/dev/sda1) 。这将禁用分区 /dev/sda1 上的启动标志。这将删除星号 (*) 标志。

Command (m for help): a
Partition number (1-9): 1

Command (m for help): p

Disk /dev/sda: 637.8 GB, 637802643456 bytes
255 heads, 63 sectors/track, 77541 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1          13      104391   83  Linux
/dev/sda2              14        2624    20972857+  83  Linux
/dev/sda3            2625        4582    15727635   83  Linux
/dev/sda4            4583       77541   586043167+   5  Extended
/dev/sda5            4583        5887    10482381   83  Linux
/dev/sda6            5888        7192    10482381   83  Linux
/dev/sda7            7193        7845     5245191   83  Linux
/dev/sda8            7846        8367     4192933+  82  Linux swap / Solaris
/dev/sda9            8368       77541   555640123+  8e  Linux LVM

Command (m for help):

我已尽力包含 fdisk 命令的几乎所有基本命令,但 fdisk 仍然包含各种其他专家命令,您可以通过输入“x”来使用它们。有关更多详细信息,请从终端查看“man fdisk”命令。如果我错过了任何重要命令,请通过评论部分与我分享。

另请阅读:

  1. Linux 中检查磁盘空间的 12 个“df”命令
  2. 10 个有用的“du”命令来查找文件和目录的磁盘使用情况

版权所有。 © Linux-Console.net • 2019-2024