整理 Windows (netsh) 與 Linux (ifconfig/ip) 修改靜態 IP、閘道、DNS 及 MAC 位址的指令,並提供 Windows 批次檔自動化範本。
netsh interface ip set address "區域連線" static 192.168.0.2 255.255.255.0 192.168.0.1 1
netsh interface ip set dns "區域連線" static 8.8.8.8 primary
netsh interface ip add dns "區域連線" 8.8.4.4 index=2
注意:介面名稱需與「網路連線」中顯示一致 (常為「區域連線」、「乙太網路」、「Wi-Fi」)。
setip.bat,管理員執行)@echo off
set "NIC=區域連線"
set "IP=192.168.1.100"
set "MASK=255.255.255.0"
set "GW=192.168.1.1"
set "DNS1=192.168.1.1"
set "DNS2=8.8.8.8"
netsh interface ip set address name="%NIC%" source=static addr=%IP% mask=%MASK% gateway=%GW% gwmetric=1
netsh interface ip set dns name="%NIC%" source=static addr=%DNS1% register=primary
netsh interface ip add dns name="%NIC%" addr=%DNS2% index=2
echo IP 設定完成!
pause
netsh interface ip set address name="%NIC%" source=dhcp
netsh interface ip set dns name="%NIC%" source=dhcp
# ifconfig 傳統指令
ifconfig eth0 192.168.2.110 netmask 255.255.255.0 up
route add default gw 192.168.2.1 dev eth0
# ip 指令 (現代推薦)
ip addr add 192.168.2.110/24 dev eth0
ip link set eth0 up
ip route add default via 192.168.2.1 dev eth0
/etc/sysconfig/network-scripts/ifcfg-eth0/etc/network/interfaces 或 Netplan /etc/netplan/*.yaml/etc/systemd/network/ 或 netctlifconfig eth0 down
ifconfig eth0 hw ether 00:11:22:33:44:55
ifconfig eth0 up
# 或
ip link set dev eth0 address 00:11:22:33:44:55
修改後重啟網路服務:systemctl restart NetworkManager 或 /etc/init.d/networking restart。