Linux fdisk命令

Linux fdisk命令:分区表管理。

Linux fdisk命令 功能描述

使用fdisk命令可以对磁盘进行分区,它采用传统的问答式界面,除此之外还可以查看磁盘分区的详细信息。

Linux fdisk命令 语法

fdisk [选项] [设备]

命令中各选项的含义如表所示。

fdisk命令选项含义

在fdisk命令的交互式操作方式下有许多子命令,如表所示。

fdisk子命令

Linux fdisk命令 示例

在磁盘/dev/sda中创建、删除、查看分区及转换分区类型

(1)进入fdisk,列出所有子命令

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

(2)显示磁盘分区信息

Command(m for help): p
Disk /dev/sda: 536.9 GB, 536870912000 bytes
255 heads, 63 sectors/track, 65270 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00034c36
  Device Boot     Start       End     Blocks    Id   System
/dev/sda1 *         1        13      102400   83    Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2          13     19595   157286400   83   Linux
/dev/sda3       19595     19856    2097152    82   Linux swap/Solaris

磁盘分区信息描述如表所示。

磁盘分区信息描述

磁盘分区的表示方法如下。
Device:磁盘分区设备名,如/dev/sda1。

  • Boot:表示引导分区,在上面的例子中/dev/sda1是引导分区。
  • Start:表示一个分区的开始柱面。
  • End:表示一个分区的结束柱面。
  • Id:一个两位的十六进制,表示分区类型。
  • System:Id所定义的分区类型。
  • Blocks(容量):表示分区容量,其单位是块,默认一个块是1KB。

(3)创建和删除主分区

在创建磁盘分区时,需要指定结束柱面,具体方法如表所示。
指定结束柱面方法

Command(m for help): n
//在此输入n,开始创建分区
Command action
  e extended                               //创建扩展分区
  p primary partition(1-4)                     //创建主分区
p
//在此输入p,开始创建主分区
Selected partition 4
First cylinder (19856-65270, default 19856):
//直接按回车键,从磁盘剩余起始柱面开始创建分区
Using default value 19856
Last cylinder, +cylinders or+size{K, M, G}(19856-65270, default 65270):30000
//在此输入30000,代表分区结束柱面数是30000
Command(m for help): p
Disk /dev/sda: 536.9 GB, 536870912000 bytes
255 heads, 63 sectors/track, 65270 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00034c36
  Device Boot     Start       End     Blocks    Id   System
/dev/sda1 *         1        13      102400   83    Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2          13     19595   157286400   83    Linux
/dev/sda3       19595     19856     2097152   82    Linux swap/Solaris
/dev/sda4       19856     30000     81488024   83    Linux
//查看分区信息,可以看到新创建的主分区为/dev/sda4
Command(m for help): n
You must delete some partition and add an extended partition first
//4个分区已满,再也无法创建第5个分区
Command(m for help): d
//在此输入d,开始删除主分区
Partition number(1-4):4
//输入分区号码,在此输入4,说明要删除/dev/sda4分区

Command(m for help): p
Disk /dev/sda: 536.9 GB, 536870912000 bytes
255 heads, 63 sectors/track, 65270 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00034c36
  Device Boot     Start       End     Blocks    Id   System
/dev/sda1 *         1       13      102400   83    Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2          13     19595   157286400   83    Linux
/dev/sda3        19595     19856     2097152   82    Linux swap/Solaris
//再次查看分区信息,之前创建的主分区/dev/sda4已经被删除

(4)创建扩展分区和逻辑驱动器

Command(m for help): n
Command action
  e extended
  p primary partition(1-4)
e
//在此输入e,开始创建扩展分区
Selected partition 4
First cylinder (19856-65270, default 19856):
//直接按回车键,从磁盘剩余起始柱面开始创建扩展分区
Using default value 19856
Last cylinder, +cylinders or +size{K, M, G} (19856-65270, default 65270):
//直接按回车键,则从第19856个柱面到最后一个柱面均为扩展分区
Using default value 65270
Command(m for help): n
//在此输入n,开始创建第一个逻辑驱动器
First cylinder (19856-65270, default 19856):
//直接按回车键,从磁盘剩余起始柱面开始创建逻辑驱动器
Using default value 19856
Last cylinder, +cylinders or+size{K, M, G}(19856-65270, default 65270): +100G
//在此输入“+100G”,代表创建容量为100GB的逻辑驱动器
Command(m for help): n
//在此输入n,开始创建第二个逻辑驱动器
First cylinder (32015-65270, default 32015):
//直接按回车键,从磁盘剩余起始柱面开始创建逻辑驱动器
Using default value 32015
Last cylinder, +cylinders or +size{K, M, G} (32015-65270, default 65270):
//直接按回车键,则从第32015个柱面到最后一个柱面均为该逻辑驱动器
Using default value 65270
Command(m for help): p
Disk /dev/sda: 536.9 GB, 536870912000 bytes
255 heads, 63 sectors/track, 65270 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00034c36
  Device Boot   Start    End   Blocks Id System
/dev/sda1 *       1    13   102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2       13   19595157286400  83 Linux
/dev/sda3     19595   19856   2097152  82 Linux swap/Solaris
/dev/sda4     19856   65270364794299   5 Extended
/dev/sda5     19856   32910104862567+ 83 Linux
/dev/sda6     32911   65270259931668+ 83 Linux
//再次查看分区信息,可以看到刚才所创建的扩展分区/dev/sda4和逻辑驱动器/dev/sda5、/dev/sda6

(5)查看并转换分区类型

Command(m for help): l
 0 Empty     24 NEC DOS    81 Minix/old Lin bf Solaris
 1 FAT12     39 Plan 9    82 Linux swap/So c1 DRDOS/sec(FAT-
 2 XENIX root   3c PartitionMagic 83 Linux     c4 DRDOS/sec(FAT-
 3 XENIX usr   40 Venix 80286  84 OS/2 hidden C: c6 DRDOS/sec(FAT-
 4 FAT16<32M   41 PPC PReP Boot 85 Linux extended c7 Syrinx
 5 Extended   42 SFS      86 NTFS volume set da Non-FS data
 6 FAT16     4d QNX4.x    87 NTFS volume set db CP/M/CTOS/.
 7 HPFS/NTFS   4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
8 AIX      4f QNX4.x 3rd part 8e Linux LVM   df BootIt
 9 AIX bootable  50 OnTrack DM   93 Amoeba    e1 DOS access
 a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT   e3 DOS R/O
 b W95 FAT32   52 CP/M     9f BSD/OS    e4 SpeedStor
 c W95 FAT32(LBA)53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
 e W95 FAT16(LBA)54 OnTrackDM6   a5 FreeBSD    ee GPT
 f W95 Ext'd(LBA)55 EZ-Drive   a6 OpenBSD    ef EFI(FAT-12/16/
10 OPUS     56 Golden Bow   a7 NeXTSTEP   f0 Linux/PA-RISC b
11 Hidden FAT12  5c Priam Edisk  a8 Darwin UFS   f1 SpeedStor
12 Compaq diagnost 61 SpeedStor   a9 NetBSD    f4 SpeedStor
14 Hidden FAT16<3 63 GNU HURD or Sys ab Darwin boot  f2 DOS secondary
16 Hidden FAT16  64 Novell Netware af HFS/HFS+   fb VMware VMFS
17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs    fc VMware VMKCORE
18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap   fd Linux raid auto
1b Hidden W95 FAT3 75 PC/IX     bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 80 Old Minix   be Solaris boot  ff BBT
1e Hidden W95 FAT1
Command(m for help): t
//在此输入t,开始转换分区类型
Partition number(1-6):6
//输入分区号码,在此输入6,说明要转换/dev/sda6分区的类型
Hex code(type L to list codes): c
Changed system type of partition 6 to c (W95 FAT32 (LBA))
//转换6号分区类型为W95 FAT32 (LBA)
Command(m for help): p
Disk /dev/sda: 536.9 GB, 536870912000 bytes
255 heads, 63 sectors/track, 65270 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00034c36
  Device Boot   Start    End   Blocks Id System
/dev/sda1 *       1     13   102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2       13   19595157286400  83 Linux
/dev/sda3     19595   19856   2097152  82 Linux swap/Solaris
/dev/sda4     19856   65270364794299   5 Extended
/dev/sda5     19856   32910104862567+ 83 Linux
/dev/sda6     32911   65270259931668+  c W95 FAT32(LBA)
//再次查看分区信息,可以看到/dev/sda6分区类型为W95 FAT32 (LBA)

(6)保存分区设置信息,并退出fdisk

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: 设备或资源忙.
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)在非交互式界面下显示当前磁盘的分区信息

[root@rhel ~]# fdisk -l /dev/sda
Disk /dev/sda: 536.9 GB, 536870912000 bytes
255 heads, 63 sectors/track, 65270 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00034c36
  Device Boot     Start       End       Blocks     Id     System
/dev/sda1 *         1        13      102400     83      Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2          13     19595   157286400     83      Linux
/dev/sda3       19595     19856     2097152     82      Linux swap/Solaris
/dev/sda4       19856     65270   364794299      5      Extended
/dev/sda5       19856     32910   104862567+     83      Linux
/dev/sda6       32911     65270   259931668+      c      W95 FAT32(LBA)

(8)重启计算机系统

使用以下命令重新启动Linux系统。

[root@rhel ~]# init 6

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程