如何解决VS Code连接WSL失败问题?已装Remote-WSL仍报错
在WSL中执行
code .后VS Code无法建立连接的解决方案 问题现象
在WSL的Ubuntu-20.04环境中执行code .后,VS Code界面能正常打开,但无法与WSL建立连接,流程卡在“安装WSL组件”步骤,报错日志包含乱码内容,忽略错误后无法访问WSL内的文件。已安装VS Code的WSL扩展。
报错日志
[2024-12-05 13:41:52.062] Extension version: 0.88.5 [2024-12-05 13:41:52.063] L10N bundle: file:///c%3A/Users/Gaoqd/.vscode/extensions/ms-vscode-remote.remote-wsl-0.88.5/l10n/bundle.l10n.zh-cn.json [2024-12-05 13:41:52.097] authorityHierarchy: wsl+Ubuntu-20.04 [2024-12-05 13:41:52.097] WSL extension activating for a local WSL instance [2024-12-05 13:41:52.115] Resolving wsl+Ubuntu-20.04, resolveAttempt: 1 [2024-12-05 13:41:52.116] NodeExecServer run: C:\WINDOWS\System32\wsl.exe --status [2024-12-05 13:41:52.307] WSL feature installed: true (wsl --status) [2024-12-05 13:41:52.307] NodeExecServer run: C:\WINDOWS\System32\wsl.exe --list --verbose [2024-12-05 13:41:52.475] 1 distros found [2024-12-05 13:41:52.476] Starting VS Code Server inside WSL (wsl2) [2024-12-05 13:41:52.476] Windows build: 22631. Multi distro support: available. WSL path support: enabled [2024-12-05 13:41:52.476] Scriptless setup: false [2024-12-05 13:41:52.739] NodeExecServer run: C:\WINDOWS\System32\wsl.exe -d Ubuntu-20.04 -e sh -c [ -f '/tmp/vscode-distro-env.CyHss1' ] && cat '/tmp/vscode-distro-env.CyHss1' || echo '' [2024-12-05 13:41:52.829] WSL daemon log file: [2024-12-05 13:41:52.830] Probing if server is already installed: if [ -d ~/.vscode-server/bin/f1a4fb101478ce6ec82fe9627c43efbf9e98c813 ]; then printf 'install-found '; fi; if [ -f /etc/alpine-release ]; then printf 'alpine-'; fi; if [ -s ~/.vscode-server/bin/legacy-mode ]; then printf 'legacy-'; fi; uname -m; [2024-12-05 13:41:52.830] NodeExecServer run: C:\WINDOWS\System32\wsl.exe -d Ubuntu-20.04 -e sh -c if [ -d ~/.vscode-server/bin/f1a4fb101478ce6ec82fe9627c43efbf9e98c813 ]; then printf 'install-found '; fi; if [ -f /etc/alpine-release ]; then printf 'alpine-'; fi; if [ -s ~/.vscode-server/bin/legacy-mode ]; then printf 'legacy-'; fi; uname -m; [2024-12-05 13:41:53.372] Using shell environment from invoking shell: /tmp/vscode-distro-env.CyHss1, 77 variables. [2024-12-05 13:41:53.447] Probing result: install-found x86_64 [2024-12-05 13:41:53.448] Server install found in WSL [2024-12-05 13:41:53.448] NodeExecServer run: C:\WINDOWS\System32\wsl.exe -d Ubuntu-20.04 sh -c "$VSCODE_WSL_EXT_LOCATION/scripts/wslServer.sh" f1a4fb101478ce6ec82fe9627c43efbf9e98c813 stable code-server .vscode-server --host=127.0.0.1 --port=0 --connection-token=863315541-3702812910-3928786281-2457301640 --use-host-proxy --without-browser-env-var --disable-websocket-compression --accept-server-license-terms --telemetry-level=all [2024-12-05 13:41:53.762] wsl: �hKm0R localhost �NtM�n �FO*g\��P0R WSL0NAT !j_ N/ec localhost �Nt0 N�v WSL
解决方案
重置WSL VS Code Server:在WSL终端执行命令,删除可能损坏的Server安装文件,重新触发自动安装:
rm -rf ~/.vscode-server执行后重新运行
code .即可。更新WSL内核:打开管理员权限的Windows终端,执行:
wsl --update更新完成后关闭所有WSL实例:
wsl --shutdown重启Ubuntu实例后再次尝试连接。
重置WSL网络配置:若内核更新无效,在管理员终端执行以下命令重置网络,之后重启电脑:
netsh winsock reset netsh int ip reset all netsh winhttp reset proxy ipconfig /flushdns调整WSL扩展版本:打开VS Code,找到WSL扩展,尝试更新至最新版本;若最新版本存在问题,可降级到上一个稳定版本(如0.88.4)后重试。
修复WSL编码问题:终端乱码可能影响连接,在WSL中设置UTF-8编码:
echo "export LC_ALL=en_US.UTF-8" >> ~/.bashrc echo "export LANG=en_US.UTF-8" >> ~/.bashrc source ~/.bashrc配置完成后重新执行
code .。
内容的提问来源于stack exchange,提问作者Qiandi Gao
相关产品推荐
相关产品推荐

