简体   繁体   English

WSL中的VSCode:如何对根文件进行sudo以便我可以编辑它

[英]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) WSL v.1 -- VSCode v1.40.1(使用“远程 - WSL”扩展 40.3)

How to open a root-owned file for edit using sudo and VSCode?如何使用 sudo 和 VSCode 打开 root 拥有的文件进行编辑? (without running as root) (不以root身份运行)

If I open a root file without sudo , I can't edit it (expected): $ code /etc/profile.d/custom-profile.sh $如果我打开没有sudo的根文件,我将无法编辑它(预期): $ code /etc/profile.d/custom-profile.sh $

在此处输入图像描述

But, if I try to sudo code the file, I get:但是,如果我尝试对文件进行sudo code ,我会得到:

$ 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. Binarify 下面的回答表明我可以将默认用户切换到root ,但我绝对不想以 root 身份运行,所以我仍在寻找另一种解决方案。

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:我遇到了同样的错误,编辑后我无法在 vscode 中保存任何文件,并通过以下命令解决:

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 .目前,我能够实现这一目标的唯一方法是使用rmate

1. Install rmate on your WSL VM 1. 在你的 WSL 虚拟机上安装 rmate

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.我在这里使用的是 Debian Buster WSL,但是您可以根据您的操作系统或偏好将/usr/bin替换为 $PATH 中的适当文件夹。

2. Install the Remote VS Code plugin 2.安装Remote VS Code插件

在此处输入图像描述

make sure the Extension is enabled on WSL: after adding the plugin.确保在 WSL 上启用扩展:添加插件后。

Here is how I configured the remote VS Code plugin这是我配置远程 VS Code 插件的方式

File -> Preferences -> Settings

在此处输入图像描述

3. Start the VSCode rmate server 3.启动VSCode rmate服务器

Press F1 and run Search for the Remote: Start Server command.按 F1 并运行搜索Remote: Start Server命令。

在此处输入图像描述

4. Edit your privileged files 4. 编辑您的特权文件

Start your WSL instance and open a terminal.启动您的 WSL 实例并打开一个终端。 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 权限编辑文件,即使您不是 root 用户。

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 SSH 通过 VS Code 以 root 身份登录

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.请注意,这应该只用于首先只有 root 有权访问的文件。 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 1. 在 WSL 上安装 ssh-server

sudo apt install ssh

2. Allow remote login as root. 2.允许以root身份远程登录。

Edit or add PermitRootLogin yes inside /etc/ssh/sshd_config/etc/ssh/sshd_config中编辑或添加PermitRootLogin yes

3. Restart ssh server 3.重启ssh服务器

sudo service ssh stop
sudo service ssh start

4. Connect to WSL in VS Code 4.在VS Code中连接WSL

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. Ctrl + Shift + P -> Remote SSH: Add new ssh host -> root@127.0.0.1接受所有指纹和东西,你以 root 身份进入。 You should be able to do anything now.你现在应该可以做任何事情了。

input this in powershell and press 'Enter'在 powershell 中输入,然后按“Enter”

ubuntu config --default-user root

then you will login wsl with root user and you can do anything now然后您将使用 root 用户登录 wsl,您现在可以做任何事情

If there is a possibility to upgrade from WSL version 1 to WSL version 2 then you might be able to use setfacl .如果有可能从 WSL 版本 1 升级到 WSL 版本 2,那么您也许可以使用setfacl I've just tested using WSL 1 too and there ACL's are sadly not supported.我也刚刚使用 WSL 1 进行了测试,遗憾的是不支持 ACL。

Why use setfacl over chown and chmod ?为什么使用setfacl而不是chownchmod 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 .要保留给定的chownchmod Linux 权限(因为对于在不同系统用户下运行的不同服务,您可能有不同内容的不同文件夹),例如使用 Ubuntu 20.04 (使用 WSL2 运行),同时给自己一些打开和编辑给定 WSL2 文件夹中的文件(或仅编辑特定文件),您可能可以使用getfaclsetfacl

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.因此,您可以尝试这样的方法来递归( -R )修改( -m )用户$USER./path-to-folder权限,以包括读取、写入和执行( rwx )权限。

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.然后您可以提前 go 并从 Visual Studio Code 更改文件夹中的一些文件,并且chownchmod权限将保持不变。

Of course you can also use setfacl for changing the ACL permissions for an individual file.当然,您也可以使用setfacl更改单个文件的 ACL 权限。

I've tested this successfully using WSL2 with Ubuntu 20.04.我已经使用 WSL2 和 Ubuntu 20.04 成功测试了这个。 I don't know if every distribution supports ACL permissions by default.我不知道是否每个发行版默认都支持 ACL 权限。 But Ubuntu 20.04 appears to have support for getfacl and setfacl .但是 Ubuntu 20.04 似乎支持getfaclsetfacl

if you install Ubuntu from the Windows store the command you will need to run at a windows command prompt is如果从 Windows 商店安装 Ubuntu 存储命令,则需要在 windows 命令提示符下运行

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) (注意:根据您安装的版本,这可能是 ubuntu1804.exe 或 ubuntu2004.exe)

Restart LxssManager service重启 LxssManager 服务

sc stop LxssManager
sc start LxssManager

On the Windows PC, install Vscode .在 Windows PC 上,安装Vscode

For a fresh WSL Linux distro such as Ubuntu , open the WSL Linux shell.对于新的WSL Linux发行版,例如Ubuntu ,请打开WSL Linux Z2565291C98B1ZE484

Type code.键入code. (note trailing space and dot) (注意尾随空格和点)

The Windows Vscode program will be invoked to edit your Linux user profile.将调用Windows Vscode程序来编辑您的Linux用户配置文件。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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