问题
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.
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/58980356/vscode-in-wsl-how-to-sudo-a-root-file-so-i-can-edit-it