![如何更改 WSL2 的 DNS 設定?](https://rvso.com/image/1618338/%E5%A6%82%E4%BD%95%E6%9B%B4%E6%94%B9%20WSL2%20%E7%9A%84%20DNS%20%E8%A8%AD%E5%AE%9A%EF%BC%9F.png)
據我所知,在 Windows 10 上更改 WSL2 使用哪個 DNS 伺服器的官方流程如下所示。 (WSL2 是適用於 Linux 的 Windows 子系統的新版本,它在 Windows 10 上的輕量級 VM 中運行實際的 Linux 核心。)
然而,這個過程對我來說不起作用 - 我最終得到的系統只是超時並且無法解析任何地址。
我究竟做錯了什麼?
這是我到目前為止將其拼湊起來的過程:
1. 關閉生成/etc/resolv.conf
使用 Linux 提示字元(我使用的是 Ubuntu),使用以下內容修改(或建立)/etc/wsl.conf
[network]
generateResolvConf = false
(顯然當前版本中存在一個錯誤,這些行上的任何尾隨空格都會導致問題。)
2. 重新啟動 WSL2 虛擬機
退出所有 Linux 提示字元並執行以下 Powershell 命令
wsl --shutdown
3. 建立自訂/etc/resolv.conf
打開一個新的 Linux 提示符號並 cd 到/etc
如果resolv.conf
軟鏈接到另一個文件,請刪除該鏈接
rm resolv.conf
新建一個,resolv.conf
內容如下
nameserver 1.1.1.1
4. 重新啟動 WSL2 虛擬機
與步驟 #2 相同
5. 啟動新的 Linux 提示字元。
利潤!
2020 年 3 月 18 日更新
我的/etc/hosts
文件包含以下內容:
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateHosts = false
127.0.0.1 localhost
127.0.1.1 [redacted] bearps-desktop
10.168.244.140 host.docker.internal
10.168.244.140 gateway.docker.internal
127.0.0.1 kubernetes.docker.internal
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
該文件/etc/switch.conf
不存在。
該工具resolvectl
不在我的道路上;它不存在於/usr/bin/resolvectl
。
同樣systemd-resolve
不在我的道路上;它不存在於/usr/bin/systemd-resolve
。
答案1
我上面記錄的過程是正確的 - 這是如何變更 WSL2 下的 DNS 設定。
我的錯誤是使用 CloudFlare (1.1.1.1) 和 Google (8.8.8.8 & 8.8.4.4) 的眾所周知的公共 DNS 伺服器進行測試。事實證明,我的本地網路阻止我使用公共 DNS。
當我使用正確的內部 DNS 伺服器 IP 位址測試上述過程時,一切正常。
答案2
在這裡添加其他答案也想要設定resolv.conf
不可變。
sudo chattr +i /etc/resolv.conf
就我而言,即使使用wsl.conf
.
出於這個原因,我創建了這個小程序,可以用一個 shell 來自動執行所有操作:
答案3
我必須在家中執行此操作(冠狀病毒),最終編寫了一個 Powershell 管理員提示腳本來啟動我的發行版(debian)。另請注意,這將啟動 WSL 指定的預設發行版,如果不是這種情況,您需要更改最後一行。如果您的版本不是也稱為 Debian,請尋找,\\wsl$\[distro name]
只需在 Windows 資源管理器路徑中鍵入第一部分即可到達 WSL2,即在資源管理器頂部欄中輸入\\wsl$\
,它將列出您的發行版。
這將從 Windows 主機中提取 DNS 伺服器,建立新的 resolv.conf,並將其以 Unix 格式寫入 WSL2 執行個體。因此,您確實需要將 /etc/wsl.conf 編輯為:
[network]
generateResolvConf = false
因此,超級用戶嘗試將其更改為 resolv.config;如果該檔案存在是因為您使用的是不同的 Linux 發行版,那麼我想它就是您想要的檔案。所以相應修改。儘管我從未在任何文件中看到過該文件,並且我不確定我的帖子的編輯是否正確。
然後啟動 PowerShell 管理提示字元並像 ./debian.ps1 一樣運行(如果這就是您命名檔案的名稱):
# Filename: debian.ps1
#
# Get the DNS server of the Windows machine, save into variable nameserver
$nameserver = Get-WmiObject -Namespace root\cimv2 -Query "Select dnsserversearchorder from win32_networkadapterconfiguration" | where {$_.DNSServerSEarchOrder -ne $null} | select -ExpandProperty DNSServerSearchOrder
# Convert nameserver object into a string
$nameserver = Out-String -InputObject $nameserver
# Run Set-Contents (sc) to write the resolv.conf file in a public location as it has DOS formatted line endings written by PowerShell, not readable by Linux
sc -Path 'c:\Users\Public\Documents\resolv.conf' -Value ('nameserver ' + $nameserver) -Encoding utf8
# Convert the DOS formatted file into UNIX format for WSL2 and write it in the proper place (\etc\resolv.conf, its primary location is \\wsl$\[distro_name] from Windows)
[string]::Join( "`n", (gc 'c:\Users\Public\Documents\resolv.conf')) | sc '\\wsl$\debian\etc\resolv.conf'
# Launch the default WSL2 distribution - if you want to use another one, specify in the line below
wsl.exe
這已經在 Debian Buster 和 Zscaler(企業)VPN 下進行了測試。這是一個很好的起點,希望它可以幫助別人解決我在公司 PC 的 WSL2 執行個體上取得 DNS 存取時遇到的麻煩!
答案4
關於更改它的非常詳細的答案。我只是想使用預設值並繼續使用它。遵循了一些指南(例如第一個答案),但對我來說不起作用。我只是改變了/etc/wsl.conf
,它又可以工作了。
sudo nano /etc/wsl.conf
在此文件中插入或更改:
[network]
generateResolvConf = true
在 Powershell/CMD 中插入:
wsl --shutdown
或者:
wsl --terminate <<Linux distro>>
完成了,完成了。讓我知道這是否對你有用。
我的設定很混亂,因為當我啟動它時 Tailscale 改變了它。現在它又開始工作了。