vscode-remote-release
vscode-remote-release copied to clipboard
wsl: allow to run with a non-default user
Issue Type: Bug
As part of my WSL instance, I had to create a user to properly setup nvm for configuring nodejs and npm. I could properly create the user and access it via the provided terminal in remote VS Code but can't open files with 'code-insiders .' or through other means.
I used this tutorial: Developing in the Windows Subsystem For Linux with VS Code to set up the remote development.
Note: by default the VS Code Insiders file explorer shows the root (WSL) folder
Expected
'code-insiders .'
Should open the given directory in the vs code insiders explorer when logged in as a non-root user(w/ sudo privileges)
Actual
'code-insiders .'
code-insiders: command not found
This issue only happens when you're logged in as a given user. It works as expected when you're a root user.
Extension version: 0.34.0 VS Code version: Code - Insiders 1.34.20-insider (57b550c559b945eb9d871dbf2b2e4cb9e31e2551, 2019-05-10T17:36:45.765Z) OS version: Windows_NT x64 10.0.17134 Remote OS version: Linux x64 4.4.0-17134-Microsoft
System Info
| Item | Value |
|---|---|
| CPUs | Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz (12 x 3696) |
| GPU Status | 2d_canvas: enabled checker_imaging: disabled_off flash_3d: enabled flash_stage3d: enabled flash_stage3d_baseline: enabled gpu_compositing: enabled multiple_raster_threads: enabled_on native_gpu_memory_buffers: disabled_software rasterization: enabled surface_synchronization: enabled_on video_decode: enabled webgl: enabled webgl2: enabled |
| Load (avg) | undefined |
| Memory (System) | 31.91GB (22.40GB free) |
| Process Argv | |
| Screen Reader | no |
| VM | 0% |
| Item | Value |
|---|---|
| Remote | WSL |
| OS | Linux x64 4.4.0-17134-Microsoft |
| CPUs | Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz (12 x 3696) |
| Memory (System) | 31.91GB (22.40GB free) |
| VM | 0% |
The WSL remote extension currently always uses the default user.
WSL allows to change the default user for a distro in two ways:
- on the Windows command line: https://docs.microsoft.com/en-us/windows/wsl/wsl-config#change-the-default-user-for-a-distribution
- In the
/etc/wsl.conffile: https://docs.microsoft.com/en-us/windows/wsl/wsl-config#user
Following the steps in description, above, it seems there are additional issues in WSL when creating a new user:
- create a new user in WSL (
adduser). - after switching to the user (
su), the env PATH does not include the windows path. This looks like a WSL issue. That's whycode-insiders .is not working.
@aeschli Should I create an issue for WSL about including the windows path after switching to the user?
@LuisOsta Yes, that would be great.
Can Remote-WSL allow for configuring wsl args? Or use my-distro.exe instead of wsl.exe to run WSL? For some reason wsl.exe starts as root and if I could change args to -u my-user my problem would be fixed.
Can Remote-WSL allow for configuring wsl args? Or use
my-distro.exeinstead ofwsl.exeto run WSL? For some reasonwsl.exestarts asrootand if I could change args to-u my-usermy problem would be fixed.
Same problem. It looks like that the Remote-WSL runs the wslServer.shusing the default user. And the default user of an imported distro is root. Changing the default user should resolve this.
After some research, got a solution from https://github.com/microsoft/WSL/issues/4276#issuecomment-509364493
For the record there is a .wslconfig entry by now for the default user: https://docs.microsoft.com/en-us/windows/wsl/release-notes#build-18980 But that doesn't solve the original problem I guess.
Problem still exists
I'm currently trying to use code . on another user, but it only works for my default user. Is this intended behaviour, or is there a way around it?
Basically, open distro to default user -> run code . (works) -> su - otheruser -> run code . (doesn't work)
I just want to add that running code-insiders . from my non-default WSL user downloads the vscode-server to the correct location (~/.vscode-server-insiders), and launches VSCode. But then the app connects to WSL using the default user, downloads the vscode-server again (this time to /home/<default-user>/.vscode-server-insiders) and runs from there.
Currently there are plans to solve this problem?
Np plans at the moment as I don't know how the UX would look like. I'd rather avoid an additional picker or input dialog. Maybe a settings?
So now that I'm not running VSCode Insiders I'm no longer running into this issue. While I think maybe putting something on settings would be helpful. I think since there's a terminal we can probably avoid that unless there's a lot of people facing these issues.
Are there no plans to add this feature yet? I do want this mainly for reason of permission.
My idea for UX
- launched from Windows Host: as the specified user if set, as default user otherwise (I think this is not so important as below)
- launched from WSL command: as the current user
but there may be difficulty because server and client are implemented to run respectively as a single process so far.
I'd like to add some input here as to why this would be a useful feature. For me personally, I like to keep my work projects & configs and personal projects & configs separate by way of user accounts, it just makes life easier.
I don't think changing the default user account is a proper solution, this just becomes a PITA at the start of the workday and then the end of the workday when you want to work on your own projects. A settings option (like others have mentioned) would be a great way to resolve this issue, therefore it could be set per project as well.
I'd like to add some input here as to why this would be a useful feature. For me personally, I like to keep my work projects & configs and personal projects & configs separate by way of user accounts, it just makes life easier.
I don't think changing the default user account is a proper solution, this just becomes a PITA at the start of the workday and then the end of the workday when you want to work on your own projects. A settings option (like others have mentioned) would be a great way to resolve this issue, therefore it could be set per project as well.
Same situation here, I use this for compartmentalisation and I do development work on both accounts. This would really help people from having to duplicate their WSL distro in order to achieve the same thing, which isn't ideal (particularly in my case) as it unnecessarily uses extra storage.
Jumping on - we're debugging ERP extensions and really need to be able to specify the WSL user account. Many of the files are owned by a specific user, which on purpose is not the default user.
Sorry to ask again, but is there a plan to get this fixed?
WSL can be launched as a specific user using wsl --user $name. The user running the code command can be fetched using whoami or $USER env on most OSes
hi all There is this "workaroud".
I wanted my vscode to connect to WSL2 using a specific user following: NotTheDr01ds's post : https://superuser.com/a/1627461/229645
I changed /etc/wsl.conf and added my default user under [user]
[user]
default=myuser
Now when I launch wsl from powershell it uses myuser
and accordingly in VScode it uses the same
hth
I'd like to add some input here as to why this would be a useful feature. For me personally, I like to keep my work projects & configs and personal projects & configs separate by way of user accounts, it just makes life easier.
I don't think changing the default user account is a proper solution, this just becomes a PITA at the start of the workday and then the end of the workday when you want to work on your own projects. A settings option (like others have mentioned) would be a great way to resolve this issue, therefore it could be set per project as well.
Absolutely here as well. I would like to be able to choose the user for connecting to WSL from VSCode.
Same here. I would request the feature to be able to choose a WSL user at will, when connecting to WSL from VSCode.
Same here. I would request the feature to be able to choose a WSL user at will, when connecting to WSL from VSCode.
It would super helpful to be able to choose what user we need to login as in the WSL distro.
I would request feature to be able choising user to log in the WSL distro while open VS Code
In case it suits anyone, I will share what I ended up doing to have two wsl users, one for work and another for my personal projects.
Caveat: I leverage on wt (Windows Terminal), nircmd and Launchy (similar to macOS Spotlight app).
- I created the second user and set it up with everything I need.
- Once I got it working like the default wsl user, with docker, etc., I created the file
/etc/wsl.confwith these contents: wslconf.zip - Then copied it onto
wsl_<original_user>.confandwsl_<new_user>.confand replacing the fielddefaultwith the names I want. Both files will be swapped back and forth when switching from one wsl user to another (work & personal). - Afterwards, I created this batch script that I will leave here:
switch_wsl_user.zip
It essentially swaps the
.conffiles and then restarts Docker Desktop, WSL and everything in the right order so that Docker Desktop doesn't complain (still waiting for Docker Desktop to have a cli). - And lastly, I created a new Runner command within Launchy with Name
<user>wt, Programnircmdand Argumentselevate "<path_to>\switch_wsl_user.bat" <user>, which just runs the previous script as Admin.
This way, when I want to jump on my personal projects, I just do Alt+Space, type "<new_user>wt" and, once docker & WSL are again up and running, I run an alias (code<new_user>) that runs "code <new_user_workspace_path>" within WSL.
PS: I know this is very hacky, alas, it is what it is.
Here's how I go around this:
-
have the following section on
/etc/wsl.conf:[user] default = <your_username> -
Add the following function to
/etc/profile.d/wsl_default_user.shwhich is a file you need to create:wsl_default_user() { sudo echo "Unblocking sudo" local TARGET_USER_=\${1} if [ -z ${TARGET_USER_} ] ; then echo "You must provide the default user for this WSL instance" ; return 1 ; fi if sudo sed -i '/^\[user\]/q' /etc/wsl.conf ; then echo -e "\nRemove previous user: SUCCESS\n" ; else echo -e "\nRemove previous user: FAILED\n" ; exit ; fi if sudo sed -i "/\[user\]/a default = \${TARGET_USER_}" /etc/wsl.conf ; then echo -e "\nAdd new default user: SUCCESS\nYou must shutdown this instance and wait 8 seconds before login in again\n" ; else echo -e "\Add new default user: FAILED\n" ; exit ; fi } -
Source the file
source /etc/profile.d/wsl_default_user.sh -
Now you can change the default user by running:
wsl_default_user <username> -
Shutdown the WSL instance, wait around 8 seconds and login again with VSCode, this time VSCode should log in as the username passed to the
wsl_default_userfunction.
PS: you can modify the last if block to shutdown automatically after changing the default user this way you don't manually need to do that.