fdisk 指令:MBR 分割區建立與刪除、fdisk -l 確認方法
fdisk 指令是查看磁碟分割表,並建立或刪除分割區的 Linux 指令。在 1 GiB 練習用映像檔上建立 MBR 分割表,切出兩個主要分割區、延伸分割區,以及其中的兩個邏輯分割區後用 w 儲存,再用 fdisk -l 確認已記錄五個分割區。
下方伺服器磁碟的 fdisk -l 輸出中,磁碟名稱、大小、型號與識別碼已換成範例值;fdisk 第一個畫面中 util-linux 2.37 後面的修補編號,以及工作資料夾 /root/work 也是範例值。建立、刪除與儲存分割區的指令,全都是在用 truncate 建立的練習用映像檔上執行。
1️⃣ fdisk 指令基本用法與 fdisk -l 分割區確認
fdisk 有兩種執行方式。加上 -l 時,只輸出磁碟與分割區清單就結束。不加 -l 而給出裝置名稱時,會開啟修改分割區的互動畫面。
不加引數執行 fdisk -l,會輸出連接到伺服器的所有磁碟。
fdisk -lDisk /dev/nvme0n1: 40 GiB, 42949672960 bytes, 83886080 sectors
Disk model: Example Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 6F1C2A90-3B7E-4D52-9A1E-2C8B5D0F7E41
Device Start End Sectors Size Type
/dev/nvme0n1p1 2048 4095 2048 1M BIOS boot
/dev/nvme0n1p2 4096 413695 409600 200M EFI System
/dev/nvme0n1p3 413696 2510847 2097152 1G Linux filesystem
/dev/nvme0n1p4 2510848 83886046 81375199 38.8G Linux filesystem第一行是磁碟名稱、總容量與磁區數,[Disklabel type] 這一行是分割表格式。這顆磁碟是 [gpt],MBR 分割表則顯示為 [dos]。GPT 磁碟也可以用 gdisk 指令分割。本文以 MBR(dos)分割表為準進行。
分割區清單各欄位的意義如下表。GPT 磁碟的清單中沒有 [Boot] 與 [Id] 欄。
| 欄位 | 說明 |
|---|---|
| Device | 分割區的裝置名稱。磁碟名稱後面接上分割區編號(nvme 磁碟則像 p1 一樣,在 p 後面接編號) |
| Boot | 在已開啟開機旗標的分割區顯示 *。只有 MBR(dos)分割表才有的欄位 |
| Start · End | 分割區起始與結束的磁區編號 |
| Sectors | 分割區的磁區數。乘以磁區大小 512 bytes 即為以位元組計的大小 |
| Size | 換算成易讀單位的大小 |
| Id | 分割區類型編號(16 進位)。只有 MBR(dos)分割表才有的欄位 |
| Type | 分割區類型名稱 |
練習用映像檔是用 truncate 建立的 1 GiB 空白檔案。fdisk 除了裝置名稱也接受檔案路徑,因此可以不碰實際磁碟,用相同的畫面練習。
truncate -s 1G /root/work/disk.imgfdisk -l /root/work/disk.imgDisk /root/work/disk.img: 1 GiB, 1073741824 bytes, 2097152 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這個檔案還沒有分割表,因此只出現四行磁碟資訊。
🔲 fdisk 指令選項
| 選項 | 複製 | 說明 |
|---|---|---|
| -l, --list | 只輸出磁碟與分割區清單後結束 | |
| -o, --outputlist | 以逗號串接欄位名稱,只輸出想要的欄位 |
| 選項 | 複製 | 說明 |
|---|---|---|
| -b, --sector-sizesectorsize | 指定磁碟的磁區大小。值為 512、1024、2048、4096 | |
| -B, --protect-boot | 建立新分割表時,不清除磁碟第一個磁區的開頭部分。GPT 與 MBR 支援 | |
| -c, --compatibility[=mode] | 將相容模式指定為 dos 或 nondos。預設值為 nondos | |
| -h, --help | 輸出說明後結束 | |
| -L, --color[=when] | 在輸出中加上顏色。when 為 auto、never、always | |
| -x, --list-details | 與 --list 相同,但輸出更詳細的資訊 | |
| --lock[=mode] | 對裝置或檔案使用獨占的 BSD 鎖定。mode 為 yes、no、nonblock | |
| -n, --noauto-pt | 不在空白裝置上自動建立預設分割表 | |
| -s, --getsz | 以 512 bytes 磁區數輸出區塊裝置的大小。已棄用(建議使用 blockdev) | |
| -t, --typetype | 只支援指定類型的分割表,不支援其餘類型 | |
| -u, --units[=unit] | 輸出分割表時,以 sectors 或 cylinders 為單位顯示大小。預設值為 sectors | |
| -C, --cylindersnumber | 指定磁碟的磁柱數 | |
| -H, --headsnumber | 指定磁碟的磁頭數。這是寫入分割表的值,與實體磁頭數無關,適合用 255 或 16 | |
| -S, --sectorsnumber | 指定每個磁軌的磁區數。適合的值為 63 | |
| -w, --wipewhen | 清除裝置上的檔案系統、RAID、分割表簽章。when 為 auto、never、always | |
| -W, --wipe-partitionswhen | 清除新建立分割區上的檔案系統、RAID、分割表簽章。when 為 auto、never、always | |
| -V, --version | 輸出版本資訊後結束 |
2️⃣ fdisk 建立分割區與邏輯分割區
要建立分割區時,不加 -l,只給路徑來開啟互動畫面。
fdisk /root/work/disk.imgWelcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xf638ab13.因為是沒有分割表的檔案,fdisk 在記憶體中建立了新的 DOS(MBR)分割表。如提示文字所說,在這裡所做的變更,在用 w 儲存之前都不會寫入磁碟。
在 [Command (m for help):] 提示中輸入一個字母來選擇作業。輸入 m 會出現指令清單。
Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
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
u change display/entry units
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本文使用的指令如下表。
| 指令 | 作用 |
|---|---|
| m | 輸出指令清單 |
| p | 輸出目前的分割表 |
| n | 建立新分割區 |
| d | 刪除分割區 |
| t | 變更分割區類型編號 |
| a | 開啟或關閉開機旗標 |
| l | 輸出類型編號清單 |
| F | 輸出沒有分割區的空閒空間 |
| o | 新建空白的 DOS(MBR)分割表 |
| w | 將變更寫入磁碟後結束 |
| q | 捨棄變更後結束 |
用 n 建立第一個分割區。選擇主要分割區(p)與編號 1,第一個磁區按 Enter 採用預設值,再在最後一個磁區的位置輸入 +300M,設為 300 MiB。
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-2097151, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2097151, default 2097151): +300M
Created a new partition 1 of type 'Linux' and of size 300 MiB.第一個磁區的預設值 2048 是 1 MiB(2048 × 512 bytes)的位置。fdisk 手冊說明,預設值與 +300M 這類大小指定值會配合裝置特性對齊,因此建議採用預設值。
用同樣的方法建立 200 MiB 的主要分割區 2,剩下的空間全部分配給延伸分割區 3。最後一個磁區按 Enter 略過時,會一直使用到剩餘空間的結尾。
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (616448-2097151, default 616448):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (616448-2097151, default 2097151): +200M
Created a new partition 2 of type 'Linux' and of size 200 MiB.Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p): e
Partition number (3,4, default 3): 3
First sector (1026048-2097151, default 1026048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1026048-2097151, default 2097151):
Created a new partition 3 of type 'Extended' and of size 523 MiB.MBR 分割表最多只能記錄四個主要分割區。要分割得更多時,把其中一個位置建立為延伸分割區,再在其中建立邏輯分割區。邏輯分割區編號從 5 開始。
延伸分割區已佔用所有剩餘空間,因此輸入 n 時,fdisk 不詢問類型,直接新增了邏輯分割區 5。在 100 MiB 的 5 之後用剩餘空間建立 6,由於已沒有空閒磁區,7 沒有建立。
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (1028096-2097151, default 1028096):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1028096-2097151, default 2097151): +100M
Created a new partition 5 of type 'Linux' and of size 100 MiB.Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 6
First sector (1234944-2097151, default 1234944):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1234944-2097151, default 2097151):
Created a new partition 6 of type 'Linux' and of size 421 MiB.Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 7
No free sectors available.用 p 確認分割的結果。延伸分割區 3(Id 5)是容納邏輯分割區 5 與 6 的區段,因此其 [Start] 與 [End] 範圍涵蓋兩者。
Command (m for help): p
Disk /root/work/disk.img: 1 GiB, 1073741824 bytes, 2097152 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: dos
Disk identifier: 0xf638ab13
Device Boot Start End Sectors Size Id Type
/root/work/disk.img1 2048 616447 614400 300M 83 Linux
/root/work/disk.img2 616448 1026047 409600 200M 83 Linux
/root/work/disk.img3 1026048 2097151 1071104 523M 5 Extended
/root/work/disk.img5 1028096 1232895 204800 100M 83 Linux
/root/work/disk.img6 1234944 2097151 862208 421M 83 Linux確認後輸入 w,將分割表寫入映像檔。
Command (m for help): w
The partition table has been altered.
Syncing disks.出現 [Syncing disks.] 之後,fdisk 結束並回到 shell 提示字元。
3️⃣ fdisk 用 w 儲存與用 q 取消
在 fdisk 中所做的變更只存在記憶體中,輸入 w 的瞬間才寫入磁碟。用 fdisk -l 重新查看以 w 儲存的映像檔,五個分割區都照樣出現。
fdisk -l /root/work/disk.imgDisk /root/work/disk.img: 1 GiB, 1073741824 bytes, 2097152 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: dos
Disk identifier: 0xf638ab13
Device Boot Start End Sectors Size Id Type
/root/work/disk.img1 2048 616447 614400 300M 83 Linux
/root/work/disk.img2 616448 1026047 409600 200M 83 Linux
/root/work/disk.img3 1026048 2097151 1071104 523M 5 Extended
/root/work/disk.img5 1028096 1232895 204800 100M 83 Linux
/root/work/disk.img6 1234944 2097151 862208 421M 83 Linuxq 會捨棄變更後結束。用 d 刪除分割區 2,用 p 確認它已從清單消失後,再用 q 結束。
fdisk /root/work/disk.imgWelcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): d
Partition number (1-3,5,6, default 6): 2
Partition 2 has been deleted.
Command (m for help): p
Disk /root/work/disk.img: 1 GiB, 1073741824 bytes, 2097152 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: dos
Disk identifier: 0xf638ab13
Device Boot Start End Sectors Size Id Type
/root/work/disk.img1 2048 616447 614400 300M 83 Linux
/root/work/disk.img3 1026048 2097151 1071104 523M 5 Extended
/root/work/disk.img5 1028096 1232895 204800 100M 83 Linux
/root/work/disk.img6 1234944 2097151 862208 421M 83 Linux
Command (m for help): q用 q 結束後重新查看清單,刪除過的分割區 2 仍然保留著。
fdisk -l /root/work/disk.imgDisk /root/work/disk.img: 1 GiB, 1073741824 bytes, 2097152 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: dos
Disk identifier: 0xf638ab13
Device Boot Start End Sectors Size Id Type
/root/work/disk.img1 2048 616447 614400 300M 83 Linux
/root/work/disk.img2 616448 1026047 409600 200M 83 Linux
/root/work/disk.img3 1026048 2097151 1071104 523M 5 Extended
/root/work/disk.img5 1028096 1232895 204800 100M 83 Linux
/root/work/disk.img6 1234944 2097151 862208 421M 83 Linux在實際磁碟上輸入 w,該磁碟的分割表會立即變更。因此儲存前,請再用 fdisk -l 確認一次裝置名稱與大小。
4️⃣ fdisk 變更分割區類型與刪除
新分割區以類型編號 83(Linux)建立。要當作 swap 或 LVM 使用的分割區,用 t 變更類型編號。分割區 2 改為 82,6 改為 8e。
fdisk /root/work/disk.imgWelcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): t
Partition number (1-3,5,6, default 6): 2
Hex code or alias (type L to list all): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'.Command (m for help): t
Partition number (1-3,5,6, default 6): 6
Hex code or alias (type L to list all): 8e
Changed type of partition 'Linux' to 'Linux LVM'.輸入 l 會出現完整的類型編號清單。Linux 伺服器常用的編號與別名如下,別名可以在 [Hex code or alias] 提示中代替編號輸入。
| 編號 | 類型 · 別名 |
|---|---|
| 83 | Linux · 別名 linux。新分割區的預設類型 |
| 82 | Linux swap / Solaris · 別名 swap |
| 8e | Linux LVM · 別名 lvm |
| 5 | Extended · 別名 extended。容納邏輯分割區的延伸分割區 |
| fd | Linux raid autodetect · 別名 raid |
| ef | EFI (FAT-12/16/32) · 別名 uefi |
a 會開啟與關閉分割區的開機旗標。對 1 號分割區開啟後用 p 查看,1 號的 [Boot] 欄出現 *,2 號與 6 號的 [Id] 與 [Type] 也已變更。這個結果用 w 儲存。
Command (m for help): a
Partition number (1-3,5,6, default 6): 1
The bootable flag on partition 1 is enabled now.Command (m for help): p
Disk /root/work/disk.img: 1 GiB, 1073741824 bytes, 2097152 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: dos
Disk identifier: 0xf638ab13
Device Boot Start End Sectors Size Id Type
/root/work/disk.img1 * 2048 616447 614400 300M 83 Linux
/root/work/disk.img2 616448 1026047 409600 200M 82 Linux swap / Solaris
/root/work/disk.img3 1026048 2097151 1071104 523M 5 Extended
/root/work/disk.img5 1028096 1232895 204800 100M 83 Linux
/root/work/disk.img6 1234944 2097151 862208 421M 8e Linux LVM
Command (m for help): w
The partition table has been altered.
Syncing disks.d 會刪除分割區。用 fdisk 重新開啟已儲存的映像檔,輸入不存在的編號 9 後,出現 [Value out of range.] 訊息並再次詢問編號。刪除延伸分割區 3 後,其中的邏輯分割區 5 與 6 也一起消失。
fdisk /root/work/disk.imgWelcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): d
Partition number (1-3,5,6, default 6): 9
Value out of range.
Partition number (1-3,5,6, default 6): 3
Partition 3 has been deleted.
Command (m for help): p
Disk /root/work/disk.img: 1 GiB, 1073741824 bytes, 2097152 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: dos
Disk identifier: 0xf638ab13
Device Boot Start End Sectors Size Id Type
/root/work/disk.img1 * 2048 616447 614400 300M 83 Linux
/root/work/disk.img2 616448 1026047 409600 200M 82 Linux swap / Solaris這次刪除只用 p 確認結果,再用 q 結束,沒有儲存。
🔲 fdisk 指令模擬器
在下方模擬器中,可以對練習用映像檔 disk.img 直接輸入 fdisk。用 fdisk disk.img 開啟,用 n 劃分分割區,用 w 儲存後,再用 fdisk -l disk.img 確認結果。
5️⃣ fdisk 指令實務範例
🔲 fdisk 建立與刪除分割區範例
依照為新磁碟分割的順序進行。用 truncate 新建 3 GiB 映像檔 disk.img 來使用。執行 fdisk,用 o 建立空白的 MBR 分割表後,用 n 建立兩個主要分割區,並在最後一個磁區的位置輸入 +1G 與 +500M。
truncate -s 3G disk.imgfdisk disk.imgCommand (m for help): o
Created a new DOS disklabel with disk identifier 0x2dcf4156.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-6291455, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-6291455, default 6291455): +1G
Created a new partition 1 of type 'Linux' and of size 1 GiB.
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2099200-6291455, default 2099200):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2099200-6291455, default 6291455): +500M
Created a new partition 2 of type 'Linux' and of size 500 MiB.輸入 w 之前先用 p 查看清單,確認 1 GiB 與 500 MiB 分割區都以輸入的大小出現後才儲存。
Command (m for help): p
Disk disk.img: 3 GiB, 3221225472 bytes, 6291456 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: dos
Disk identifier: 0x2dcf4156
Device Boot Start End Sectors Size Id Type
disk.img1 2048 2099199 2097152 1G 83 Linux
disk.img2 2099200 3123199 1024000 500M 83 Linux
Command (m for help): w
The partition table has been altered.
Syncing disks.刪除時再次執行 fdisk,在 d 輸入 2,用 p 確認只剩 1 號分割區後,再用 w 儲存。
fdisk disk.imgCommand (m for help): d
Partition number (1,2, default 2): 2
Partition 2 has been deleted.
Command (m for help): p
Disk disk.img: 3 GiB, 3221225472 bytes, 6291456 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: dos
Disk identifier: 0x2dcf4156
Device Boot Start End Sectors Size Id Type
disk.img1 2048 2099199 2097152 1G 83 Linux
Command (m for help): w
The partition table has been altered.
Syncing disks.🔲 使用 fdisk 時的常見錯誤
初次使用時容易犯的錯誤,是用 q 而不是 w 結束。用 n 再次新增 500 MiB 分割區,在 p 清單中確認到 2 號分割區後用 q 結束,結果 fdisk -l 只出現 1 號分割區。
fdisk -l disk.imgDisk disk.img: 3 GiB, 3221225472 bytes, 6291456 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: dos
Disk identifier: 0x2dcf4156
Device Boot Start End Sectors Size Id Type
disk.img1 2048 2099199 2097152 1G 83 Linux指定到其他磁碟的錯誤,可以在作業前先用 fdisk -l 確認大小與識別碼來避免。與建立了一個 2 GiB 分割區的 5 GiB 映像檔 backup.img 一起輸出後,從 [Disk backup.img: 5 GiB] 這一行與 [Disk identifier: 0xd201d03b] 這一行,就能和 disk.img 區分。
fdisk -l disk.img backup.imgDisk disk.img: 3 GiB, 3221225472 bytes, 6291456 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: dos
Disk identifier: 0x2dcf4156
Device Boot Start End Sectors Size Id Type
disk.img1 2048 2099199 2097152 1G 83 Linux
Disk backup.img: 5 GiB, 5368709120 bytes, 10485760 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: dos
Disk identifier: 0xd201d03b
Device Boot Start End Sectors Size Id Type
backup.img1 2048 4196351 4194304 2G 83 Linux🔲 確認 fdisk -l 輸出的變化
用 o 建立分割表並用 w 儲存後,fdisk -l 輸出中多了 [Disklabel type: dos] 與 [Disk identifier: 0x2dcf4156] 這兩行。識別碼與輸入 o 時 [Created a new DOS disklabel with disk identifier 0x2dcf4156.] 這一行中出現的值相同。
在這份清單中,2 號分割區從 1 號分割區 [End] 2099199 的下一個磁區 2099200 開始。刪除 2 號後,清單減少為 disk.img1 一行。
n、d、w、q 這類指令可以在 fdisk 指令模擬器中直接輸入試試。
🔢 FAQ & 推薦內容
ℹ️ 聯盟行銷說明
本站內容包含聯盟行銷連結。訪客透過這些連結購買商品或服務時,本站會從賣家獲得佣金。在這個過程中,購買者支付的金額(遇到活動折扣時金額會降低↓)並不會因此提高。刊登的價格、折扣與庫存資訊以撰寫當時為準,可能與實際情況不同,購買前請向賣家做最後確認。商品的挑選與評價依本站自訂的標準撰寫,是否支付佣金不會影響介紹順序或評價內容。