fdisk是固定磁盘或格式化磁盘,属于linux/Unix系统中常用的基于命令行的磁盘操作实用程序,用户可以实现基于文本的菜单驱动界面查看、创建、调整大小、删除、更改、复制和移动硬盘上的分区。允许用户根据系统中硬盘大小来创建最多四个新的主分区和一定数量的逻辑分区。
必须是root用户才可以运行fdisk命令,反之会提醒“命令未找到”错误。注意使用中要谨慎操作创建、删除或修改分区。查看linux中全部磁盘可用分区,-l ' 参数代表(列出所有分区),按设备名称显示如/dev/sda、/dev/sdb或/dev/sdc:
[root@tecmint.com ~]# 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
在linux中查看特定磁盘分区,请使用选项“ -l ”和设备名称。例如,以下命令将显示设备/dev/sda的所有磁盘分区。如果您有不同的设备名称,只需将设备名称写为/dev/sdb或/dev/sdc即可。
[root@tecmint.com ~]# 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
检查全部可用的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):
打印linux中的全部分区,须处于特定硬盘的命令模式,例如/dev/sda:
[root@howtoing ~]# fdisk /dev/sda
在该模式下,输入“p”将打印特定的分区表:
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):
如果需要删除linux中分区,需要处于fdisk命令模式才能执行:
[root@howtoing ~]# fdisk /dev/sda
输入“d”从系统中删除任何给定分区的名称。比如输入“d”,会提示输入要删除的分区号如继续输入“4”那就会执行删除分区是4的磁盘显示分区表中的可用空间。输入“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
删除愤怒去一定要谨慎,一旦删除就会彻底删除分区及其中数据。
如果需要创建新分区,先处于fdisk命令模式,在输入下面命令进入特定硬盘命令模式:
[root@howtoing ~]# 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): 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
[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”选主分区。需输入分区的起始和结束柱面号,或用“+5000M”这样的大小表示。创建后,用“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.
创建新分区后,不要跳过使用“ mkfs ”命令格式化新创建的分区。在终端中输入以下命令来格式化分区。这里/dev/sda4是我新创建的分区。
[root@tecmint ~]# mkfs.ext4 /dev/sda4
更多内容,继续阅读华纳云帮助中心!