51

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 $

enter image description here

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.

Hawkeye Parker
  • 7,817
  • 7
  • 44
  • 47
  • 6
    Really surprised this doesn't have a clean solution yet... – dusky Aug 26 '20 at 07:40
  • 3
    Here is one solution. https://stackoverflow.com/a/65963974/12160919. In short, you need to modify the /etc/sudoers configure file. – Jack Jan 30 '21 at 02:15

9 Answers9

33

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

kirin nee
  • 670
  • 5
  • 15
13

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

Lakshika Parihar
  • 1,017
  • 9
  • 19
  • 1
    This will recursively change the ownership of the directory and not just the file. If this is done on say `/etc`, where you usually want to change configurations, this would cause issues down the line. I'm not saying this is wrong, but just be careful with this one – Pavel Skipenes Apr 18 '23 at 09:18
12

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.

Barafu Albino
  • 253
  • 2
  • 7
  • I can't seem to get this to work. `sudo -e file` just defaults to what I have for the EDITOR env var. Followed your steps exactly: https://i.imgur.com/BAQBAGC.jpg. Also tried adding full path to `code` and setting SUDO_EDITOR to `code` as well. I assume you actually tried this? I can't think of what might be different on my system; I'm on WSL 2. – Hawkeye Parker Dec 10 '20 at 04:22
  • 2
    You must use and argument "code -w". The documentation says the precedence is SUDO_EDITOR > VISUAL > EDITOR. I use it daily. – Barafu Albino Dec 14 '20 at 15:47
  • This works and IMO the best answer. Definitely needs more upvote – Nolan Edric Nov 09 '21 at 17:43
  • It's needed the edit sudoers secure_path option and needs to launch vscode on windows before I run sudo -e on WSL2 in my case. – hisatoria Dec 26 '22 at 02:39
  • This was working for me and just stopped. Do you have any idea what I could be doing wrong? It keeps opening the default editor instead of vscode. – Aaron Stainback May 23 '23 at 01:37
8

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

enter image description here

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

enter image description here

3. Start the VSCode rmate server

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

enter image description here

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
Lance
  • 895
  • 1
  • 9
  • 18
5

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 systemctl restart ssh

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.

Pavel Skipenes
  • 342
  • 5
  • 14
5

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
Ameur Bennaoui
  • 309
  • 3
  • 5
4

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.

Wieger
  • 663
  • 1
  • 9
  • 24
3

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

Binaryify
  • 300
  • 2
  • 7
1

Changing the user to root doesn't seem like a good solution. As Barafu Albino said, you need to set the SUDO_EDITOR environment variable.

  • If you use VSCode for Windows

For me, the solution looks like this:

export VS_CODE="/mnt/c/Users/alex/AppData/Local/Programs/Microsoft\ VS\ Code/bin/code"
export SUDO_EDITOR="${VS_CODE} -nw"

You may use $ which code to find the VScode binary in Linux or where code in the Windows CMD. Make sure you don't forget to escaped the spaces in the resulting path.

  • If you use the Linux version of VScode try this:

export SUDO_EDITOR="code -nw"

Finally, you can edit the files with sudo -e /path/to/file.

innomerphey
  • 191
  • 2
  • 5
  • for those who use vscode ssh, you might want to avoid open vscode new window and change the path to vscode bin `which code` and update bin path `export SUDO_EDITOR="/home/admi/.vscode-server/bin/2cf817401dc/bin/remote-cli/code -w"` – CircleOnCircles Aug 03 '23 at 10:58