Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

dd 命令詳解:從基本參數到實戰應用全攻略

編輯:Linux 教學

dd 命令核心概念

dd 是 Linux 系統中強大的底層資料複製工具,能以指定區塊大小讀取輸入檔案並寫入輸出檔案,同時支援轉換操作。其名稱源自 "Data Definition",常用於磁碟備份、鏡像建立、裝置複製等場景。

關鍵參數速查表

參數說明
if=檔案輸入檔案(來源),預設為標準輸入
of=檔案輸出檔案(目的),預設為標準輸出
bs=位元組同時設定讀寫區塊大小(單位位元組)
count=區塊數僅複製指定數量的區塊
skip=區塊數從輸入檔案開頭跳過指定區塊
seek=區塊數從輸出檔案開頭跳過指定區塊
conv=轉換指定資料轉換方式(如 ascii、ebcdic、lcase、ucase、noerror、sync 等)

單位後綴:b=512、c=1、k=1024、w=2、M=1024k、G=1024M

15 個實戰應用範例

1. 磁碟整盤備份

dd if=/dev/hdb of=/dev/hdd

2. 磁碟備份為映像檔

dd if=/dev/hdb of=/root/image

3. 從映像檔還原磁碟

dd if=/root/image of=/dev/hdb

4. 備份並壓縮(搭配 gzip)

dd if=/dev/hdb | gzip > /root/image.gz

5. 解壓並還原

gzip -dc /root/image.gz | dd of=/dev/hdb

6. MBR 備份與還原

# 備份前 512 位元組
dd if=/dev/hda of=/root/mbr.bin count=1 bs=512
# 還原
dd if=/root/mbr.bin of=/dev/hda

7. 軟碟備份

dd if=/dev/fd0 of=disk.img count=1 bs=1440k

8. 記憶體內容轉存

dd if=/dev/mem of=/root/mem.bin bs=1024

9. 光碟製作 ISO

dd if=/dev/cdrom of=/root/cd.iso

10. 建立 Swap 檔案

# 建立 256MB 檔案
dd if=/dev/zero of=/swapfile bs=1024 count=262144
# 格式化為 swap
mkswap /swapfile
# 啟用
swapon /swapfile
# 開機自動掛載寫入 /etc/fstab
/swapfile swap swap defaults 0 0

11. 磁碟資料銷毀

dd if=/dev/urandom of=/dev/hda1

12. 硬碟讀寫速度測試

# 寫入測試
dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file
# 讀取測試
dd if=/root/1Gb.file bs=64k | dd of=/dev/null

13. 尋找最佳區塊大小

dd if=/dev/zero bs=1024 count=1000000 of=/root/test
dd if=/dev/zero bs=2048 count=500000 of=/root/test
dd if=/dev/zero bs=4096 count=250000 of=/root/test
dd if=/dev/zero bs=8192 count=125000 of=/root/test

比較各命令執行時間,耗時最短者即為最佳區塊大小。

14. 磁碟修復(長期閒置磁碟)

dd if=/dev/sda of=/dev/sda

利用讀寫重新磁化磁通量點,可能恢復邊緣扇區。

15. 遠端備份(搭配 netcat)

# 來源主機
dd if=/dev/hda bs=16065b | netcat  1234
# 目的主機接收
netcat -l -p 1234 | dd of=/dev/hdc bs=16065b
# 目的主機接收並壓縮
netcat -l -p 1234 | bzip2 > partition.img
netcat -l -p 1234 | gzip > partition.img

進階技巧:修改巨大檔案中特定位元組

echo A | dd of=bigfile seek=$i bs=1 count=1 conv=notrunc

將第 i 個位元組改為 0x41(大寫 A)。

Copyright © Windows教程網 All Rights Reserved