简体   繁体   中英

VSCode in WSL: how to sudo a root file so I can edit it

WSL v.1 -- VSCode v1.40.1 (using 'Remote - WSL' extension 40.3)

How to open a root-owned file for edit using sudo and VSCode? (without running as root)

If I open a root file without sudo , I can't edit it (expected): $ code /etc/profile.d/custom-profile.sh $

在此处输入图像描述

But, if I try to sudo code the file, I get:

$ sudo code /etc/profile.d/custom-profile.sh
[sudo] password for xxxx:
sudo: code: command not found

Binarify's answer below shows that I can switch the default user to root , but I definitely don't want to be running as root, so I'm still looking for another solution.

You can own the file you want to edit, then give it back the ownership afterwards

sudo chown myuser /path/to/file
code /path/to/file
sudo chown root /path/to/file

I got the same error, i was not able to save any file in vscode after editing and it was resolved by the following command:

sudo chown -R <user-name> <directory-name>

It worked for me, Hope it works for you too. Thank you

Currently, the only way I was able to achieve this was to use rmate .

1. Install rmate on your WSL VM

sudo wget -O /usr/bin/rmate https://raw.githubusercontent.com/aurora/rmate/master/rmate
sudo chmod a+x /usr/bin/rmate

I'm using a Debian Buster WSL here, however you can replace /usr/bin with an appropriate folder in your $PATH depending on your OS or your preference.

2. Install the Remote VS Code plugin

在此处输入图像描述

make sure the Extension is enabled on WSL: after adding the plugin.

Here is how I configured the remote VS Code plugin

File -> Preferences -> Settings

在此处输入图像描述

3. Start the VSCode rmate server

Press F1 and run Search for the Remote: Start Server command.

在此处输入图像描述

4. Edit your privileged files

Start your WSL instance and open a terminal. If you've done everything correctly you should be able to now edit your files with sudo priveledges in your editor, even if you are not the root user.

sudo rmate /etc/profile.d/custom-profile.sh

Set environment variable:

export VISUAL="code -nw"

Then you can edit any file like this:

sudo -e file

It will automatically make a copy of file, and, when you close the editor, copy it back.

SSH in as root through VS Code

Don't know if this is the best approach but it worked for me. Note that this should only be used on files that only root has access to in the first place. I think saved files will be owned by root. Use this to quickly change some settings but not to write code that can done by regular user.

1. Install ssh-server on WSL

sudo apt install ssh

2. Allow remote login as root.

Edit or add PermitRootLogin yes inside /etc/ssh/sshd_config

3. Restart ssh server

sudo service ssh stop
sudo service ssh start

4. Connect to WSL in VS Code

Ctrl + Shift + P -> Remote SSH: Add new ssh host -> root@127.0.0.1 accept all fingerprints and stuff and you're in as root. You should be able to do anything now.

input this in powershell and press 'Enter'

ubuntu config --default-user root

then you will login wsl with root user and you can do anything now

If there is a possibility to upgrade from WSL version 1 to WSL version 2 then you might be able to use setfacl . I've just tested using WSL 1 too and there ACL's are sadly not supported.

Why use setfacl over chown and chmod ? To preserve given chown and chmod Linux permissions (because you might have different folders with different contents for different services running under different system users) using for instance Ubuntu 20.04 (which runs using WSL2) and at the same time give yourself some access to open and edit files within a given WSL2 folder (or to edit just a specific file) you might be able to use getfacl and setfacl .

So you can try something like this to recursively ( -R ) modify ( -m ) permissions for ./path-to-folder for user $USER to include read, write and execute ( rwx ) permissions.

sudo setfacl -R -m u:$USER:rwx ./path-to-folder
cd ./path-to-folder
code .

Then you can go ahead and change some files within the folder from Visual Studio Code and chown and chmod permissions will stay the same.

Of course you can also use setfacl for changing the ACL permissions for an individual file.

I've tested this successfully using WSL2 with Ubuntu 20.04. I don't know if every distribution supports ACL permissions by default. But Ubuntu 20.04 appears to have support for getfacl and setfacl .

if you install Ubuntu from the Windows store the command you will need to run at a windows command prompt is

ubuntu config --default-user root

or

ubuntu1804 config --default-user root

or

ubuntu2004 config --default-user root

(Note: depending on which version you installed this could be ubuntu1804.exe or ubuntu2004.exe)

Restart LxssManager service

sc stop LxssManager
sc start LxssManager

On the Windows PC, install Vscode .

For a fresh WSL Linux distro such as Ubuntu , open the WSL Linux shell.

Type code. (note trailing space and dot)

The Windows Vscode program will be invoked to edit your Linux user profile.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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