分享最新优惠信息
购买主机更加划算

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

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

fdisk在为新分区创建空间、为新驱动器划分空间、重新划分旧驱动器以及将数据复制或移动到新磁盘方面非常有用,而且还可以根据系统中硬盘的大小创建最多四个新的主分区和多个逻辑(扩展)分区。

fdisk命令

在本文当中,小编将简单介绍基于Linux系统中管理分区表的10个基本fdisk命令需要注意的是,必须以root用户才能运行fdisk命令,否则将出现“command not found”的错误。

注意– 不要轻易创建、删除或修改分区,除非你是Linux大神级人物!

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

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

[root@idc ~]# 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等等..

[root@idc ~]# 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@idc ~]# 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@idc ~]# 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@idc ~]# 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@idc ~]# fdisk /dev/sda

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

[root@idc ~]# 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@idc ~]# fdisk /dev/sda

进入命令模式后,现在按“ n ”命令在/dev/sda下创建一个具有特定大小的新分区,这可以在以下给定输出的帮助下得到验证:

[root@idc ~]# 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

创建新分区时,它会询问你两个选项“extended”或“primary”分区创建。按“ e ”表示扩展分区,按“ p ”表示主分区。然后它会要求你输入以下两个输入。

  1. 要创建的分区的第一个柱面号。
  2. 要创建的分区的最后一个柱面号(最后一个柱面,+柱面或 +size)。

可以在最后一个柱面输入“ +6000M ”柱面尺寸这里,’ + ‘ 表示添加,6000M表示新分区的大小(即6000MB)。请记住,创建新分区后,应该运行“ 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@idc ~]# mkfs.ext4 /dev/sda4

8、在Linux中检查分区的大小

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

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

9、修复分区表顺序

如果你删除了逻辑分区并再次重新创建它,可能会注意到“partition out of order分区乱序)”问题或错误消息,例如“Partition table entries are not in disk order”。

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

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

[root@idc ~]# 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@idc ~]# fdisk /dev/sda

按 ‘ p ‘ 命令查看当前分区表,这时会看到/dev/sda1磁盘上有一个引导标志(橙色的星号 ( * ) 符号),如下所示。

[root@idc ~]# 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仍然包含各种其它Expert命令,可以通过输入“ x ”来使用它们。有关更多详细信息,请从终端查看“ man fdisk ”命令了解

在租用Linux服务器的时候,磁盘空间管理是一项最基本也是最重要的工作,每个Linux服务器管理员都应该熟悉掌握。而了解和熟用fdisk命令,可以帮助我们更好的管理Linux磁盘空间。

未经允许不得转载:惠主机 » 简单整理管理Linux磁盘分区的10个fdisk命令

相关文章