使用 cmd line 切換 DNS
---身為 geek,當然要打指令才帥啊
有時候,某些突發狀況,不得不更換 DNS IP,以期讓網站瀏覽更順暢。
比如我個人比較偏好使用 google DNS 甚於中華電信提供的。偶發的情況下也得互換使用了。
利用 gui 界面可能直覺一些,但就是不夠敏捷。那就改成使用 cmd 指令來進行切换,帥氣又快速。
主體指令長這樣,netsh interface ip

接著,因為是要設定 DNS,所以使用 netsh interface ip set dnsservers

PS: 後段的指令 Windows 10 與 Window 7、8 稍有不同,請自行參照。
從上圖的範例指令,可以發現 name 的設定,這個 name 很重要,設定錯誤,指令就會失效。那麼 name 要怎麼來?
我們可以執行 cmd ipconfig /all 來取得。

或是在 控制台 < 網路和網際網路 < 網路連線 看到名稱。



到這裡,我們的第一段指令-設定主 DNS 就完成了。
指令範例:
netsh interface ip set dnsservers "Wired Ethernet Connection" static 10.0.0.1 primary
接著就是要設定備用 DNS,

指令範例:
netsh interface ip add dnsservers "Wired Ethernet Connection" 10.0.0.3 index=2
最後,我們把上述二列指令另存成一個 .bat,或 .cmd 檔就可以了。
範例:
---
@echo off
netsh interface ip set dnsservers "Wired Ethernet Connection" static 10.0.0.1 primary
netsh interface ip add dnsservers "Wired Ethernet Connection" 10.0.0.3 index=2
---
最後的最後,這個 cmd 必須利用「以系統管理員身份執行」,才能成功執行。

ref:
https://technet.microsoft.com/en-us/library/bb490943.aspx
http://tweaks.com/windows/40339/configure-ip-address-and-dns-from-command-line/
0 留言