在 VMware Workstation/Fusion/Player 中運行 Linux 虛擬機,因授權綁定、網路衝突、測試需求等原因需修改 MAC 位址。本文提供三種層級不同的修改方案,按推薦程度排序。
關閉虛擬機 → 用文字編輯器開啟 虛擬機名稱.vmx → 依下列兩種模式二擇一修改 → 儲存 → 開機。
ethernet0.addressType = "static"
ethernet0.address = "00:50:56:0A:0B:0C" # 必須以 00:50:56 開頭 (VMware OUI)
# 開機若詢問 UUID 衝突,選擇 "Keep Always" (保留此 MAC)
優點:相當於在虛擬網卡 ROM 燒錄新 MAC,Linux 核心啟動即識別為永久硬體地址,無需任何系統層面設定。
ethernet0.addressType = "generated"
uuid.location = "56 4d dc f1 ff aa 75 ea - f1 b9 ee 0d 68 9c 65 5c"
uuid.bios = "56 4d ed 23 13 8c 96 91 - 7c 68 b2 09 8b aa bb cc"
ethernet0.generatedAddress = "00:0C:29:AA:BB:CC" # 後 6 碼可自訂,前綴 00:0C:29 為 VMware OUI
# 修改 uuid.bios 最後 6 位元組與 generatedAddress 最後 6 位元組為相同值,可固定產出特定 MAC
優點:符合 VMware 自動產生邏輯,避免 UUID 衝突提示。
vi /etc/sysconfig/network-scripts/ifcfg-eth0
# 新增/修改行
MACADDR=00:0C:29:AA:BB:CC # 目標 MAC
# 若有 HWADDR 行,建議同步修改或註解掉
# HWADDR=00:0C:29:AA:BB:CC
:wq
reboot
適用:基於 RedHat 的發行版。Debian/Ubuntu 系請見方法三。
vi /etc/rc.local
# 在 exit 0 前加入
ifconfig eth0 down
ifconfig eth0 hw ether 00:0C:29:AA:BB:CC
ifconfig eth0 up
# 或使用 ip 指令 (現代)
ip link set dev eth0 down
ip link set dev eth0 address 00:0C:29:AA:BB:CC
ip link set dev eth0 up
# 確保 rc.local 有執行權限
chmod +x /etc/rc.local
systemctl enable rc-local
systemctl start rc-local
# 建立服務單元
cat > /etc/systemd/system/macspoof@.service <<'EOF'
[Unit]
Description=MAC address change for %i
Before=network-pre.target
Wants=network-pre.target
[Service]
Type=oneshot
ExecStart=/sbin/ip link set dev %i down
ExecStart=/sbin/ip link set dev %i address 00:0C:29:AA:BB:CC
ExecStart=/sbin/ip link set dev %i up
[Install]
WantedBy=network-pre.target
EOF
# 啟用 (以 eth0 為例)
systemctl enable macspoof@eth0
systemctl start macspoof@eth0
# 圖形介面:設定 → 網路 → 選擇連線 → 齒輪圖示 → 身分識別 → MAC 位址 → 輸入 → 套用
# 命令列:
nmcli connection modify "Wired connection 1" 802-3-ethernet.cloned-mac-address 00:0C:29:AA:BB:CC
nmcli connection up "Wired connection 1"
ip link show eth0 / ifconfig eth0 → 確認 link/ether 為目標 MAC。建議優先採用 方法一 (.vmx),一次設定、永久生效、跨作業系統可攜,最符合基礎設施即代碼 (IaC) 精神。