vscode-remote-release icon indicating copy to clipboard operation
vscode-remote-release copied to clipboard

wsl: allow to run with a non-default user

Open LuisOsta opened this issue 6 years ago • 28 comments

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%

LuisOsta avatar May 11 '19 00:05 LuisOsta

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.conf file: 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 why code-insiders . is not working.

aeschli avatar May 14 '19 20:05 aeschli

@aeschli Should I create an issue for WSL about including the windows path after switching to the user?

LuisOsta avatar May 18 '19 01:05 LuisOsta

@LuisOsta Yes, that would be great.

aeschli avatar May 22 '19 11:05 aeschli

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.

Aulos avatar Jul 06 '19 11:07 Aulos

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.

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

acui avatar Sep 02 '19 09:09 acui

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.

Trass3r avatar Jul 02 '20 17:07 Trass3r

Problem still exists

pikadun avatar Sep 17 '20 09:09 pikadun

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)

danielfewster avatar Sep 18 '20 08:09 danielfewster

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.

ATGardner avatar Nov 06 '20 10:11 ATGardner

Currently there are plans to solve this problem?

cller avatar Nov 18 '20 09:11 cller

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?

aeschli avatar Nov 18 '20 09:11 aeschli

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.

LuisOsta avatar Nov 18 '20 21:11 LuisOsta

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.

aymkx avatar Feb 28 '21 10:02 aymkx

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.

PurplProto avatar May 27 '21 21:05 PurplProto

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.

mattsilverwood avatar Oct 13 '21 14:10 mattsilverwood

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.

jgwinner avatar Feb 23 '22 20:02 jgwinner

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

luisfavila avatar Sep 22 '22 18:09 luisfavila

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

thenger avatar Oct 03 '22 07:10 thenger

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.

ataudt avatar Sep 17 '23 08:09 ataudt

Same here. I would request the feature to be able to choose a WSL user at will, when connecting to WSL from VSCode.

rc-tronix avatar Oct 09 '23 22:10 rc-tronix

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.

yiskaneto avatar Jan 22 '24 18:01 yiskaneto

I would request feature to be able choising user to log in the WSL distro while open VS Code

brut0 avatar Feb 05 '24 18:02 brut0

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).

  1. I created the second user and set it up with everything I need.
  2. Once I got it working like the default wsl user, with docker, etc., I created the file /etc/wsl.conf with these contents: wslconf.zip
  3. Then copied it onto wsl_<original_user>.conf and wsl_<new_user>.conf and replacing the field default with the names I want. Both files will be swapped back and forth when switching from one wsl user to another (work & personal).
  4. Afterwards, I created this batch script that I will leave here: switch_wsl_user.zip It essentially swaps the .conf files 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).
  5. And lastly, I created a new Runner command within Launchy with Name <user>wt, Program nircmd and Arguments elevate "<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.

Ryodany avatar Feb 27 '24 13:02 Ryodany

Here's how I go around this:

  1. have the following section on /etc/wsl.conf:

       [user]
       default = <your_username>
    
  2. Add the following function to /etc/profile.d/wsl_default_user.sh which 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
      }
    
  3. Source the file source /etc/profile.d/wsl_default_user.sh

  4. Now you can change the default user by running:

       wsl_default_user <username>
    
  5. 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_user function.

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.

yiskaneto avatar Mar 07 '24 21:03 yiskaneto