fdisk用法
在 Linux 中,fdisk
是一个用于管理磁盘分区的命令行工具。它主要用于创建、删除、修改硬盘分区表,适用于 MBR(Master Boot Record)分区表格式的磁盘。虽然对于较新的 GPT(GUID Partition Table)分区表,fdisk
也提供了支持,但某些情况下可能需要使用更专用的工具如 gdisk
。
介绍
fdisk
是 Linux 系统中的一个交互式分区工具,广泛用于磁盘管理。它允许用户查看当前分区布局,并对分区进行操作,例如添加新分区、删除旧分区、更改分区类型等。运行 fdisk
时,通常需要指定目标磁盘(如 /dev/sda
),并且需要超级用户权限(sudo
)。
常用场景:
- 初始化新硬盘
- 调整现有分区
- 检查磁盘分区信息
⚠️: 操作
fdisk
时要谨慎,因为错误的修改可能导致数据丢失。
基本用法
1. 启动 fdisk
命令格式:
1 | sudo fdisk [磁盘设备] |
示例:
1 | sudo fdisk /dev/sda |
/dev/sda
是目标磁盘设备名,可能根据系统不同而变化(如/dev/sdb
)。- 启动后,进入交互式界面。
2. 常用交互命令
在 fdisk
的交互模式下,输入单字母命令执行操作。以下是常用命令:
p
:显示当前分区表。n
:创建新分区。d
:删除分区。t
:更改分区类型(如设置为 LVM 或 swap)。l
:列出所有支持的分区类型代码。w
:保存更改并退出。q
:不保存更改并退出。m
:显示帮助菜单。
3. 示例操作:创建一个新分区
假设你有一个新磁盘 /dev/sdb
,想创建一个主分区:
- 启动:
1
sudo fdisk /dev/sdb
- 输入
p
查看当前分区表(新磁盘可能为空)。 - 输入
n
创建新分区:- 选择分区类型(通常选
p
表示主分区)。 - 输入分区号(默认从 1 开始)。
- 指定起始和结束扇区(可按回车接受默认值)。
- 选择分区类型(通常选
- 输入
w
保存并退出。
4. 查看分区信息(非交互式)
如果你只想查看分区表而不修改,可以使用:
1 | sudo fdisk -l [磁盘设备] |
示例:
1 | sudo fdisk -l /dev/sda |
这会列出指定磁盘的分区信息,需要 root 权限。
注意事项
- 权限:必须以 root 或使用
sudo
运行,否则无法访问磁盘设备。 - 备份:操作前建议备份重要数据,分区操作有风险。
- GPT 支持:对于 GPT 分区表,
fdisk
在较新版本中支持(util-linux 2.23+),但建议熟悉gdisk
。 - 刷新分区表:修改分区后,可能需要运行
partprobe
或重启系统以让内核识别新分区。
实用示例
创建一个 swap 分区
- 启动
fdisk
:1
sudo fdisk /dev/sdb
- 输入
n
创建新分区。 - 输入
t
更改分区类型,选择82
(Linux swap)。 - 输入
w
保存。 - 格式化并启用 swap:
1
2sudo mkswap /dev/sdb1
sudo swapon /dev/sdb1
删除分区
- 启动
fdisk
。 - 输入
d
,然后选择要删除的分区编号。 - 输入
w
保存。
fdisk help
man fdisk
[root@vgt-rocky9-60 ~]# fdisk -V
fdisk from util-linux 2.37.4
[root@vgt-rocky9-60 ~]# fdisk openwrt-x86-64-generic-ext4-combined-efi.img
Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p
Disk openwrt-x86-64-generic-ext4-combined-efi.img: 1.43 GiB, 1535409664 bytes, 2998847 sectors
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: gpt
Disk identifier: 38948C92-761D-992A-2027-0CEE23BA4000
Device Start End Sectors Size Type
openwrt-x86-64-generic-ext4-combined-efi.img1 512 33279 32768 16M Linux filesystem
openwrt-x86-64-generic-ext4-combined-efi.img2 33280 2998813 2965534 1.4G Linux filesystem
openwrt-x86-64-generic-ext4-combined-efi.img128 34 511 478 239K BIOS boot
Partition table entries are not in disk order.
Command (m for help): m
Help:
GPT
M enter protective/hybrid MBR
Generic
d delete a partition ### 删除分区
F list free unpartitioned space
l list known partition types
n add a new partition ### 添加新分区
p print the partition table
t change a partition type ### 修改分区
v verify the partition table
i print information about a partition ### 查看分区信息
Misc
m print this menu
x extra functionality (experts only) ### 进入扩展功能
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit ### 写分区表,保存退出
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
Command (m for help):
Command (m for help): i
Partition number (1,2,128, default 128): 2
Device: openwrt-x86-64-generic-ext4-combined-efi.img2
Start: 33280
End: 2998813
Sectors: 2965534
Size: 1.4G
Type: Linux filesystem
Type-UUID: 0FC63DAF-8483-4772-8E79-3D69D8477DE4
UUID: 38948C92-761D-992A-2027-0CEE23BA4002
Command (m for help): x
Expert command (m for help): p
Disk openwrt-x86-64-generic-ext4-combined-efi.img: 1.43 GiB, 1535409664 bytes, 2998847 sectors
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: gpt
Disk identifier: 38948C92-761D-992A-2027-0CEE23BA4000
First LBA: 34
Last LBA: 2998813
Alternative LBA: 2998846
Partition entries LBA: 2
Allocated partition entries: 128
Device Start End Sectors Type-UUID UUID Name Attrs
openwrt-x86-64-generic-ext4-combined-efi.img1 512 33279 32768 0FC63DAF-8483-4772-8E79-3D69D8477DE4 38948C92-761D-992A-2027-0CEE23BA4001 LegacyBIOSBootable
openwrt-x86-64-generic-ext4-combined-efi.img2 33280 2998813 2965534 0FC63DAF-8483-4772-8E79-3D69D8477DE4 38948C92-761D-992A-2027-0CEE23BA4002
openwrt-x86-64-generic-ext4-combined-efi.img128 34 511 478 21686148-6449-6E6F-744E-656564454649 38948C92-761D-992A-2027-0CEE23BA4080
Partition table entries are not in disk order.
Expert command (m for help): m
Help (expert commands):
GPT
i change disk GUID
n change partition name
u change partition UUID ### 更新分区表uuid
l change table length
M enter protective/hybrid MBR
A toggle the legacy BIOS bootable flag
B toggle the no block IO protocol flag
R toggle the required partition flag
S toggle the GUID specific bits
Generic
p print the partition table
v verify the partition table
d print the raw data of the first sector from the device
D print the raw data of the disklabel from the device
f fix partitions order
m print this menu
Save & Exit
q quit without saving changes
r return to main menu ### 退回正常菜单
Expert command (m for help): r