Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WSL2 starting as root when starting with wsl.exe #4276

Closed
Aulos opened this issue Jul 6, 2019 · 40 comments
Closed

WSL2 starting as root when starting with wsl.exe #4276

Aulos opened this issue Jul 6, 2019 · 40 comments

Comments

@Aulos
Copy link

Aulos commented Jul 6, 2019

  • Your Windows build number: Microsoft Windows [Version 10.0.18932.1000]

  • What you're doing and what's happening: > wsl.exe -d my-wsl2-distro

  • What's wrong / what should be happening instead:
    It starts as root, even after my-wsl2-distro.exe config --default-user my-user. my-wsl2-distro.exe starts as the default-user. It is quite problematic when using Remote-WSL on vscode, because it runs as the root user.

@craigloewen-msft
Copy link
Member

Which distro are you using? Is this a general problem across distros or just on one specific distro? If you switch back to WSL 1 and try to change the default user does that work?

@Aulos
Copy link
Author

Aulos commented Jul 8, 2019

Ubuntu 18.04.

I may add that this is imported WSL1 distro as WSL2 distro - I couldn't convert my old WSL1 distro.

I'll have to check.

@craigloewen-msft
Copy link
Member

craigloewen-msft commented Jul 8, 2019

I'm unable to repro your issue. I was able to set the default user using ubuntu1804.exe config --default-user root and back to my original user in both WSL 1 and WSL 2. I'd be interested if this issue was reproducible across other distros, and across distros backed by WSL 1 or WSL 2.

@Aulos
Copy link
Author

Aulos commented Jul 8, 2019

Ok, so this is how it went:

  • I had a WSL1 distro with default user X
  • I try to convert it and it fails due to compression issues
  • So I try to export this distro and then reimport it with version set to 2
  • After successfully importing it, the default user is root (and for some reason after running it I have to hit Ctrl-C - only then shell starts)

@craigloewen-msft
Copy link
Member

To resolve your immediate issue please see issue #3974 .
Specifically go to:

  • Find your UID for your username in your Linux distro typing in the command id -u <yourUserName>. Remember this value.
  • Open registry edit and navigate to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\{MY-UUID}. Please be careful when editing registry keys!
  • Replace the DefaultUid value with the UID value of the user in your distro.

@Aulos
Copy link
Author

Aulos commented Jul 8, 2019

Yup, that worked, but why setting user doesn't do anything, without any error?

@craigloewen-msft
Copy link
Member

At the time of writing this wsl --import doesn't provide a ..exe, as described in #3974. We'll track adding that feature to make it easier to change the default user in that other thread. I'll mark this issue as closed since your immediate problem seems resolved, but if it isn't please feel free to reopen!

@cspotcode
Copy link

@Aulos I still have the Ctrl-C issue you described:

After successfully importing it, the default user is root (and for some reason after running it I have to hit Ctrl-C - only then shell starts)

Did you figure out how to fix the Ctrl-C issue?

I have successfully changed the default user via RegEdit. I did the same as you: --export and --import to convert my distro to WSL2, since --set-version failed.

@benhillis
Copy link
Member

I have recently added the ability to specify the default user in the /etc/wsl.conf file. That change is making its way to Insider builds.

@Biswa96
Copy link

Biswa96 commented Aug 28, 2019

Should not that default user option be in %UserProfile%\.wslconfig file? Because it's like Windows-controlled-thing?

@benhillis
Copy link
Member

benhillis commented Aug 28, 2019

@Biswa96 - .wslconfig is for global settings, /etc/wsl.conf is per-distro settings.

@cspotcode
Copy link

Turns out the Ctrl-C issue I was seeing was due to an unrelated WSL bug: Windows interop being broken. Pengwin has a /etc/profile.d/00-pengwin.sh script that tries to call cmd.exe, piping the output to a linux process. This is currently broken, so it was hanging. Fix was to remove that line from 00-pengwin.sh. There are other tickets tracking the interop bug.

@cpbotha
Copy link

cpbotha commented Nov 13, 2019

For anyone else wondering how the default user specification in wsl.conf as implemented by @benhillis works, see https://docs.microsoft.com/en-us/windows/wsl/release-notes#build-18980

[user]
default=default_username

You will probably have to wsl --shutdown and then restart to see the change.

@thomasfrobieter
Copy link

It may should mentioned that the /etc/wsl.conf file needs to be created manually.

This fixed it! thx!

@ZealousMacwan
Copy link

For WSL2
Added this wsl.config
[user]
default=root

And did wsl.exe --shutdown

Still logging in using zealous@Desktop

<distro_name>.exe config --default-user root
returns command not found,
Is it for WSL2 or WSL ?

@liuyongAlive
Copy link

liuyongAlive commented Nov 25, 2020

Not Ubuntu-20.04.exe
Is ubuntu2004.exe

Ubuntu2004.exe config --default-user root

@ZealousMacwan
Copy link

Still not working, Command not found. I doubt is it because I have installed distro manually ? And not from Windows Store

@Deeptwix
Copy link

Deeptwix commented Dec 4, 2020

Just to add a different context :
I've had the same issue while importing a distro from WSL2 to WSL2, but the registry value changed did it for me.
As a PSA, just be careful when you edit the DefaultUid value to either put it in hexadecimal, or to change the default format to decimal. Because id -u gives you a decimal value, and the DefaultUid field takes an hexadecimal value as a default.

@woss
Copy link

woss commented Mar 24, 2021

for reference this approach can be used to fix when you mess up something with the root, like introducing the syntax error in the sudoers file.

if you have few distros you can easily find to see which one is yours.

Open registry edit and navigate to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss{DISTRO-UUID. Please be careful when editing registry keys!
Replace the DefaultUid value with the UID value of the user in your distro, my normal user is 1000 and root is 0. Use the Decimal entry and change it to the root user. open the distro and fix the bug. ( you might do the wsl --shutdown ) then change back to the 1000 r what ever was before, wsl --shutdown again and enjoy fixed system

@craigloewen-msft
Copy link
Member

To accomplish this feature please add this section to your /etc/wsl.conf file:

[user]
default=<yourUserNameGoesHere>

Replace with your Linux username.

Here's a link to the docs:
https://docs.microsoft.com/en-us/windows/wsl/wsl-config#user

@woss
Copy link

woss commented Mar 24, 2021

i know it's mentioned but that didn't work for me @craigloewen-msft
did it work for you?

@craigloewen-msft
Copy link
Member

Please make sure you are using Windows 10 Build 19041 or higher to use that feature.

@junkuan-coder
Copy link

for reference this approach can be used to fix when you mess up something with the root, like introducing the syntax error in the sudoers file.

if you have few distros you can easily find to see which one is yours.

Open registry edit and navigate to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss{DISTRO-UUID. Please be careful when editing registry keys!
Replace the DefaultUid value with the UID value of the user in your distro, my normal user is 1000 and root is 0. Use the Decimal entry and change it to the root user. open the distro and fix the bug. ( you might do the wsl --shutdown ) then change back to the 1000 r what ever was before, wsl --shutdown again and enjoy fixed system

it works,thank you

@ReySys
Copy link

ReySys commented Jul 6, 2021

I report this issue with Fedora distro I installed yesterday from the store. Ask for change of default password for root never accepted any password I typed. Closed an open the session. Already in root. Later let me to change the password. But always in root. Different from the other distros I installed. Ubuntu, Pengwin, Debian.

I think is a theme with the username cannot be the same with windows.

@moracabanas
Copy link

For WSL2
Added this wsl.config
[user] default=root

And did wsl.exe --shutdown

Still logging in using zealous@Desktop

<distro_name>.exe config --default-user root
returns command not found,
Is it for WSL2 or WSL ?

I had the same issue and it is caused by a typo.

instead /etc/wsl.config you should write /etc/wsl.conf
note the extension change

@TerminatedProcess
Copy link

As of 8/1/2021, with windows 11 and wsl2, this issue still exists. Exported Ubuntu. Imported it back and it goes straight to root. I did make the registry key change and this resolved it for me.

@amirhp110
Copy link

This issue has nothing to do with import.
On a new Windows I install WSL ubuntu using "wsl.exe --install -d Ubuntu" in PowerShell and start Ubuntu from Start menu, it is root, does not even asked for setup username and password, I am in as root.

@maxisam
Copy link

maxisam commented Oct 21, 2021

in windows 11,

wsl -d xxxx -u root

@gmankab
Copy link

gmankab commented Nov 5, 2021

Not Ubuntu-20.04.exe Is ubuntu2004.exe

Ubuntu2004.exe config --default-user root

works, thank you

@gabrielsimas
Copy link

For anyone else wondering how the default user specification in wsl.conf as implemented by @benhillis works, see https://docs.microsoft.com/en-us/windows/wsl/release-notes#build-18980

[user]
default=default_username

You will probably have to wsl --shutdown and then restart to see the change.

Same in 2022... works for me! Thanks!

@TerminatedProcess
Copy link

TerminatedProcess commented Jul 5, 2022

I also use this command. It's in my restore script for restoring a WSL2 backup. See the last line.. YOURUSERNAME is the username you want to switch too.

`
wsl --shutdown

wsl --unregister ubuntu

wsl --import Ubuntu D:\wsl2distros C:\wslbackups%1%.tar

ubuntu config --default-user YOURUSERNAME

`

@lucit21
Copy link

lucit21 commented Sep 10, 2022

Step 1.) In your WSL/Linux Distribution enter command: id YOURUSERNAME (RECORD THE VALUE)

Step 2.) Right click the start menu and select run, then type regedit and hit enter

Step 3.) In the left panel navigate to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\

Step 4.) In the left panel click on the drop down arrow next to the Lxss folder and select your Linux Distribution

Step 5.) Locate the value titled "DefaultUid" and double click it

Step 6.) In the box that appears enter the value you recorded in step 1 and select "Decimal" then hit enter

MAKE SURE YOUR VALUE MATCHES THE RETURNED VALUE FROM STEP ONE AND MAKE SURE "DECIMAL" IS SELECTED

If you've followed every step described above and you issue remains, throw your computer away and take up knitting

Step 1

Step 2

Step 3 4

Step 5

Step 6

@DOM779
Copy link

DOM779 commented Oct 27, 2022

Step 1.) In your WSL/Linux Distribution enter command: id YOURUSERNAME (RECORD THE VALUE)

Step 2.) Right click the start menu and select run, then type regedit and hit enter

Step 3.) In the left panel navigate to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\

Step 4.) In the left panel click on the drop down arrow next to the Lxss folder and select your Linux Distribution

Step 5.) Locate the value titled "DefaultUid" and double click it

Step 6.) In the box that appears enter the value you recorded in step 1 and select "Decimal" then hit enter

MAKE SURE YOUR VALUE MATCHES THE RETURNED VALUE FROM STEP ONE AND MAKE SURE "DECIMAL" IS SELECTED

If you've followed every step described above and you issue remains, throw your computer away and take up knitting

Step 1

Step 2

Step 3 4

Step 5

Step 6

Showwwwww. Omg

@korenmic
Copy link

To resolve your immediate issue please see issue #3974 . Specifically go to:

  • Find your UID for your username in your Linux distro typing in the command id -u <yourUserName>. Remember this value.
  • Open registry edit and navigate to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\{MY-UUID}. Please be careful when editing registry keys!
  • Replace the DefaultUid value with the UID value of the user in your distro.

@craigloewen-msft - that registry path does not exist in windows 11, I could create it, but it feel as the wrong solution if it did not come pre-existing with windows.

For anyone else wondering how the default user specification in wsl.conf as implemented by @benhillis works, see https://docs.microsoft.com/en-us/windows/wsl/release-notes#build-18980

[user]
default=default_username

You will probably have to wsl --shutdown and then restart to see the change.

@cpbotha - that did not work, after wsl shutdown and start again I still came back as root
I thought wsl --shutdown was not good enough so I tried taskkill /f /im wslservice.exe, but even that was not good enough.

OH I thought the ~/.wslconfig file! No! it was the /etc/wsl.conf file, that actually worked!

@MalcolmAnderson
Copy link

To resolve your immediate issue please see issue #3974 . Specifically go to:

  • Find your UID for your username in your Linux distro typing in the command id -u <yourUserName>. Remember this value.
  • Open registry edit and navigate to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\{MY-UUID}. Please be careful when editing registry keys!
  • Replace the DefaultUid value with the UID value of the user in your distro.

Thanks for this, saved my life.
Just a note, that this is the last day of August in 2023, so this issue is still on going 4 years later.

@jglathe
Copy link

jglathe commented Sep 17, 2023

[user]
default=default_username

This helped, thanks.
Why does this still happen , in 2023?

@TheMagicNacho
Copy link

@jglathe , I don't know about you, but this only happened to me when I changed the which drive WSL2 lived on.

@jglathe
Copy link

jglathe commented Dec 29, 2023

I actually changed the SSD in the box, so a UUID change might be possible deep down in the partition UUIDs. I restored a backup image, the IDs oft the GPT will at least have partially changed due to the restore method. Seen some other issues (power management related) though, did a recovery image restore, replaced the Windows partition with the backed-up one. Managed to repair the boot config with bcdboot. This didn't bring up the issue again - probably due to the fix being in place already.

@omarmciver
Copy link

omarmciver commented Aug 16, 2024

Not Ubuntu-20.04.exe Is ubuntu2004.exe

Ubuntu2004.exe config --default-user root
or
Ubuntu2204.exe config --default-user root

This ^^^ After exporting and importing to move WSL disk to a new location.

@ScoutmanPt
Copy link

ScoutmanPt commented Jan 30, 2025

Howdy to all , try this psmodule https://github.com/pnp/wsl2 or Import-Module PnP.Wsl2 , a way to manage wsl2 instances easy\peasy . There's the Import\Export cmdlet and the Copy that fix the resizing ... and actually solve the export lock problem bug ( im adding some updates this weekend) (#12434 (comment)) that is not solved still

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests