I have Ubuntu 20.04 with WSL2 and created a new user with sudo privileges:
sudo adduser newuser
sudo usermod -aG sudo newuser
su - newuser
Windows does not seem to have permission to write in the new user folder. Here with Visual Studio Code:
touch test.txt
code test.txt
and then writing something and trying to save will ask me to try and do it as admin, which I do and then get this error message:
Failed to save 'test.txt':
Command failed: "C:\Users\<username>\AppData\Local\Programs\Microsoft VS Code\bin\code.cmd" --file-write "c:\Users\<username>\AppData\Roaming\Code\code-elevated-pdtmzg" "\\wsl$\Ubuntu\home\newuser\test.txt"
Error using --file-write: EPERM: operation not permitted, open '\\wsl$\Ubuntu\home\newuser\test.txt'
Here is the result of ls -all
with the new user work
:
Here is the result of ls -all
with the default user hellmers
:
Here are the groups for the users. I also tried adding work
to the hellmers
group:
The only way I manage to get it to work is to give others
write privileges through sudo chmod o+w <file>
, which shouldn't be necessary?
答案1
You seem to be running into this issue. Namely, VSCode always runs as the default user in a WSL session.
You should be able to confirm this with a simple whoami
in the VSCode terminal. Even though you started it from the work
user, it will likely be running as hellmers
.
For your particular use case, I would think, you could give your hellmers
user access to the work
user folder. Seems to work for me:
sudo usermod -aG work hellmers
chmod -R g+wrx /home/work
If you really do need to run VSCode as用户work
,根据 Github 问题,你需要更改默认WSL 实例的用户。受到推崇的实现此目的的方法是创建一个具有以下内容的/etc/wsl.conf
(as ):sudo
[user]
default=work
然后停止 WSL 实例wsl --terminate <distroname
并重新启动。
work
此时VSCode 应该以用户身份运行。
这既不美观也不方便。但如果您必须以其他用户身份运行 VSCode,则需要将该用户设为 WSL 实例的默认用户。
另一个可能的替代方案是设置单独的 WSL 实例,一个使用hellmers
默认,另一个使用work
。根据需要保留多个“实用”WSL 实例并没有错。