Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run VScode in sudo mode in WSL2?

I was configuring the new Windows subsystem for Linux 2 developing environment. I have installed VSCode in Windows and WSL2 distribution of Ubuntu 18.04. I can open VSCode in Ubuntu(accessed through Windows Terminal) normally when logged as normal user, using command as follows:

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

But when I add sudo command in the front, the error occurs as follows:

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

At first, the problem remains after I switched to root user. Later I found that the PATH of root user didn't contain VSCode. So I add the VSCode path to /etc/environment. After that root user can open VSCode normally, but sudo code . still have the problem. What should I do? Is this still related to the PATH?

like image 371
Yunchou Li Avatar asked Jul 24 '19 15:07

Yunchou Li


People also ask

How do I open VS Code in Sudo mode?

sudo code . It is recommended to start vscode as a normal user. To run as root, you must specify an alternate user data directory with the --user-data-dir argument.

How do I launch VS Code from wsl terminal?

Alternatively, you can open a Remote WSL window directly from VS Code: Start VS Code. Press F1, select Remote-WSL: New Window for the default distro or Remote-WSL: New Window using Distro for a specific distro. Use the File menu to open your folder.

How use VS Code terminal Linux?

From the Command Palette (Ctrl+Shift+P), use the View: Toggle Terminal command. You can create a new terminal via the Terminal menu with Terminal > New Terminal.


2 Answers

The answer here helped me: VSCode in WSL: how to sudo a root file so I can edit it

For some reason starting as root vs logging in and then switching to root worked for me

like image 55
vee Avatar answered Oct 19 '22 19:10

vee


I think the Remote WSL extension pack is what you are looking for. Install that on your local (e.g. 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).

enter image description here

like image 1
Baza86 Avatar answered Oct 19 '22 19:10

Baza86