简体   繁体   English

如何在 WSL2 中以 sudo 模式运行 VScode?

[英]How to run VScode in sudo mode in WSL2?

I was configuring the new Windows subsystem for Linux 2 developing environment.我正在为 Linux 2 开发环境配置新的 Windows 子系统。 I have installed VSCode in Windows and WSL2 distribution of Ubuntu 18.04.我已经在 Windows 和 Ubuntu 18.04 的 WSL2 发行版中安装了 VSCode。 I can open VSCode in Ubuntu(accessed through Windows Terminal) normally when logged as normal user, using command as follows:当以普通用户身份登录时,我可以在 Ubuntu 中正常打开 VSCode(通过 Windows 终端访问),使用命令如下:

simp1e@ZhuXian:/mnt/c/WINDOWS/system32$ code . 

But when I add sudo command in the front, the error occurs as follows:但是当我在前面添加sudo命令时,出现如下错误:

simp1e@ZhuXian:/mnt/c/WINDOWS/system32$ sudo code .
sudo: code: command not found                                                                                                                                                           

At first, the problem remains after I switched to root user.起初,我切换到root用户后问题仍然存在。 Later I found that the PATH of root user didn't contain VSCode.后来发现root用户的PATH中没有VSCode。 So I add the VSCode path to /etc/environment.所以我将 VSCode 路径添加到 /etc/environment。 After that root user can open VSCode normally, but sudo code .之后 root 用户可以正常打开 VSCode,但是sudo code . still have the problem.仍然有问题。 What should I do?我应该怎么办? Is this still related to the PATH?这仍然与PATH有关吗?

The answer here helped me: VSCode in WSL: how to sudo a root file so I can edit it这里的答案对我有帮助: WSL 中的 VSCode:如何对根文件进行 sudo,以便我可以对其进行编辑

For some reason starting as root vs logging in and then switching to root worked for me出于某种原因,从 root 开始 vs 登录然后切换到 root 对我有用

I think the Remote WSL extension pack is what you are looking for.我认为远程 WSL 扩展包正是您要寻找的。 Install that on your local (eg host) version of VS Code, and then from either the command pallet (Ctrl + Shift + p or F1) or from the "Open Remote Window" and select the "New WSL Window" option (screen shot below from command pallet).将其安装在本地(例如主机)版本的 VS Code 上,然后从命令托盘(Ctrl + Shift + p 或 F1)或“打开远程窗口”并选择“新 WSL 窗口”选项(屏幕截图从命令托盘下方)。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM