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

12

54

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.

Manufacture answered 21/11, 2019 at 17:16 Comment(4)
Really surprised this doesn't have a clean solution yet...Kado
Here is one solution. https://mcmap.net/q/339978/-visual-studio-code-is-not-working-on-wsl2. In short, you need to modify the /etc/sudoers configure file.Lottery
It needs to be said, though, that sudo code does run code as root, which seems both clumsy and dangerous.Window
I have created a edit_as_root alias just for this purpose, when you are using vscode server on a remote github.com/palash-sarate/Edit_as_root_onRemoteMiles
A
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

Aspire answered 17/6, 2020 at 7:11 Comment(2)
sudo chown -R $USER:$USER /path/to/group worked for meCompositor
This crucially hardcodes the ower to root. Properly speaking, you should note down the owner and group before you run this, then revert back to exactly the same values.Window
C
14

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.

Cotyledon answered 4/12, 2020 at 23:5 Comment(5)
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: 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.Manufacture
You must use and argument "code -w". The documentation says the precedence is SUDO_EDITOR > VISUAL > EDITOR. I use it daily.Cotyledon
This works and IMO the best answer. Definitely needs more upvoteHautesavoie
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.Myongmyopia
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.Broderick
H
12

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

Have answered 26/3, 2021 at 5:51 Comment(2)
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 oneAirdrie
Pavel you should say this is wrong.Window
H
10

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
Halfbreed answered 24/5, 2020 at 10:10 Comment(0)
A
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 -> [email protected] accept all fingerprints and stuff and you're in as root. You should be able to do anything now.

Airdrie answered 5/12, 2019 at 12:7 Comment(1)
Thanks, but I don't want to allow root SSH access to WSL.Manufacture
S
5

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

Subadar answered 4/2, 2020 at 7:14 Comment(3)
@Binarify Thanks, but I don't want to run as root by default. I'm really looking for some way to do the usual 'sudo x'.Manufacture
not a good workaroundUmbilicus
If you're not in ubuntu, can follow this: superuser.com/questions/1566022/… (Method 1 seemed to work)Tees
B
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
Benoite answered 7/6, 2022 at 4:4 Comment(2)
Do we really want the default user to be root? :/Semite
This duplicates the original answer from 2020, so should be downvoted both for being bad advice and for being a redundant duplicate.Window
A
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.

Alexandro answered 28/1, 2022 at 10:14 Comment(0)
H
3

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.

Haimes answered 18/10, 2022 at 1:48 Comment(1)
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"Nidia
W
2

simple method is using this plugin named save as root

after install it, simply open file such as /etc/hosts and save it by pressing F1 and type save as root

Why answered 31/5, 2024 at 9:31 Comment(1)
Thanks for pointing out this plugin, such a life saver!Christan
N
1

No need for sudo -i to be root user
add more details to the answer from @innomerphey :

which code
vi ~/.bashrc

Press O to add new line above the cursor, add Linux vscode code -w like this:

export SUDO_EDITOR="/home/admin/.vscode-server/bin/1a5daa3a0231a0fbba4f14db7ec463cf99d7768e/bin/remote-cli/code -w"

ps: Replace hash value 1a5da... in path. Everytime you restart a SSH session, the export will expired. The permanent way is to update /etc/environment

then refresh env:

source ~/.bashrc

Now use sudoedit:

sudoedit /etc/apt/sources.list
Nigel answered 18/11, 2023 at 16:31 Comment(0)
B
1

I tried all of the solutions on this page (except rmate and SSH, both of which I considered way too awkward) plus several others. Most didn't work for me at all, and worse, some had undesirable side effects eg. taking permanent ownership of all files in "/etc" let me edit the files but broke my ability to use "sudo"!

Instead, I added my (Windows-installed) VS Code's directory to root's default PATH environment variable. My instructions use the nano editor for editing the script, but you can use vi or anything else you're familiar with and have installed in your WSL2.

  1. Edit root's profile startup script:

    sudo nano /root/.bashrc

  2. Add the following line at the end of the file (you may need to adjust the path for your VS Code's directory):

    PATH="/c/Program Files/Microsoft VS Code/bin:$PATH"

  3. Save and exit (^S^X in nano).

  4. You may need to restart your WSL2 instance.

VS Code should now be easily runnable by root, either via "sudo code whatever" or by switching to "su" and then using "code whatever".

Benzidine answered 10/5, 2024 at 1:23 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.