萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 電腦基礎知識 >  >> 電腦基本常識

Windows 指令列設定靜態 IP/DNS 完整指令碼 (netsh 介面操作)

圖形介面設定 IP 有時會失效 (如 Win10 預覽版 10049),或需在無螢幕伺服器、自動化腳本中配置網路。`netsh interface ip` 指令集提供完整的命令列解決方案,適用 Win7/8.1/10/11/Server 全系列。

單步互動模式

C:\> netsh
netsh> interface
netsh interface> ip
netsh interface ip> set address "乙太網路" static 192.168.1.100 255.255.255.0 192.168.1.1 1
netsh interface ip> set dns "乙太網路" static 8.8.8.8
netsh interface ip> quit

單行指令模式 (適合批次檔/自動化)

:: 設定靜態 IP
netsh interface ip set address "乙太網路" static 192.168.1.100 255.255.255.0 192.168.1.1 1

:: 設定靜態 DNS
netsh interface ip set dns "乙太網路" static 8.8.8.8

:: 新增第二組 DNS
netsh interface ip add dns "乙太網路" 8.8.4.4 index=2

:: 恢復 DHCP 自動取得 IP
netsh interface ip set address "乙太網路" dhcp

:: 恢復 DHCP 自動取得 DNS
netsh interface ip set dns "乙太網路" dhcp
命令提示字元執行netsh

參數說明

參數說明
"介面名稱"需與「網路連線」中顯示完全一致,支援中文,必須用英文雙引號
static指定靜態參數
IP 位址如 192.168.1.100
子網路遮罩如 255.255.255.0
預設閘道如 192.168.1.1
1 (結尾)代表確認/啟用,不可省略

查詢目前設定

netsh interface ip show config "乙太網路"
netsh interface ip show dnsservers "乙太網路"

常見問題排除

  • 「無法找到介面」:名稱不符,用 `netsh interface show interface` 列出正確名稱。
  • 「存取被拒」:需以系統管理員身分執行 CMD/PowerShell。
  • IP 衝突:確認目標 IP 未被 DHCP 伺服器分派或其他裝置佔用。
  • DNS 不生效:執行 `ipconfig /flushdns` 清除快取,或重啟 DNS Client 服務。

PowerShell 替代方案 (Win10/11 原生)

New-NetIPAddress -InterfaceAlias "乙太網路" -IPAddress 192.168.1.100 -PrefixLength 24 -DefaultGateway 192.168.1.1
Set-DnsClientServerAddress -InterfaceAlias "乙太網路" -ServerAddresses 8.8.8.8, 8.8.4.4
# 恢復 DHCP
Remove-NetIPAddress -InterfaceAlias "乙太網路" -IPAddress 192.168.1.100 -Confirm:$false
Set-DnsClientServerAddress -InterfaceAlias "乙太網路" -ResetServerAddresses
copyright © 萬盛學電腦網 all rights reserved