Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Windows 與 Linux 修改 IP 位址指令完整教學 (含批次檔自動化)

編輯:系統常見問題解答

整理 Windows (netsh) 與 Linux (ifconfig/ip) 修改靜態 IP、閘道、DNS 及 MAC 位址的指令,並提供 Windows 批次檔自動化範本。

Windows: netsh 指令

單次手動修改

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

切換回 DHCP

netsh interface ip set address name="%NIC%" source=dhcp
netsh interface ip set dns name="%NIC%" source=dhcp

Linux: ifconfig / ip 指令

臨時修改 (重開機失效)

# 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

永久修改 (各發佈版路徑不同)

  • CentOS/RHEL:/etc/sysconfig/network-scripts/ifcfg-eth0
  • Debian/Ubuntu:/etc/network/interfaces 或 Netplan /etc/netplan/*.yaml
  • Arch:/etc/systemd/network/ 或 netctl

修改 MAC 位址

ifconfig 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。

Copyright © Windows教程網 All Rights Reserved