I have cloned a WSL distro using wsl --export and wsl --import, but now, running wsl newdistro always logs me in as root. I understand that the lxrun command is deprecated and want to avoid it. The docs recommend using distroname.exe config, but that doesn't work, since this one doesn't have a corresponding executable.
- 925
- 1
- 6
- 12
7 Answers
As of the time of this writing, there are at least three (let's call it 3.5) different methods of changing/setting the default user in an imported WSL instance. While the two that have already been mentioned still work, there is a Microsoft recommended way to do it that hasn't been mentioned yet in this question.
Method 1 - /etc/wsl.conf
The current Microsoft recommended way of setting the username in an instance is to create a /etc/wsl.conf in the instance with the following setting:
[user]
default=username
Changing, of course, username to be your default username.
Exit your distro/instance, then issue a wsl --terminate <distroname> from PowerShell or CMD. When you restart, the default user should be set.
This is safer and less error-prone than the registry-based methods.
Method 2 - Registry Key
Setting the registry key per @harrymc's answer.
Method 3 - LxRunOffline
Using LxRunOffline to set the registry entry, as described in @Jaime's answer. Ultimately this has the same effect as Method 2.
Semi-method 4 - Runtime user selection via wsl commandline argument
The username can be selected when starting any WSL instance by:
wsl -u username or wsl -d distroname -u username, etc.
For instance, wsl -d Ubuntu -u root.
Side Note: This question was specifically about setting the default username in an imported instance.
However, for completeness, you can also set the default username for a distro that was installed from the Store (or wsl --install) with:
<distro>.exe config --default-user <username>
For instance, if you installed "Ubuntu 20.04" from the Store, you would use:
ubuntu2004.exe config --default-user <username>
The .exe here is an "App Execution Alias" in Windows. You can check the name by going to "Manage app execution aliases" in the Windows System Settings.
For readers who need to create a new user
@ndemou made a good point in the comments last year that I, in hindsight, dismissed improperly. Two other users have now mentioned in the comments that @ndemou's information was helpful. My belated apologies! Here's the additional information based on @ndemou's advice ...
Some users who find this answer may be starting up as root not because they --import'd a distribution, but possibly because the default user was never created during installation in the first place. This can happen when the distribution installation is stopped prematurely, and likely for other reasons as well.
Under Ubuntu, the default distribution for WSL, you can create your user by starting with:
wsl ~ -u root
If you need to, add the -d <distribution> option.
Then run:
NEWUSER=<your_username>
useradd --create-home --shell /usr/bin/bash --user-group --groups adm,dialout,cdrom,floppy,sudo,audio,dip,video,plugdev,netdev --password $(read -sp Password: pw ; echo $pw | openssl passwd -1 -stdin) $NEWUSER
Then set the user as the default using the /etc/wsl.conf as mentioned above.
If you've already created the user, or are using a different distribution, as @ndemou points out, make sure to also add the user either to a group that is in sudoers or directly to sudoers via visudo. See the answers to this question for additional details.
- 17,574
- 4
- 44
- 81
-
1You may want to add the new user to sudoers. If you forget it start wsl as root with something like `wsl --list; wsl --distribution Ubuntu-22.04 -u root` – ndemou Jul 29 '22 at 17:56
-
@ndemou Since this question is about a `wsl --export`'d distro, then the user in the `wsl --import`'d one will still be in `sudoers` -- No need to add it again. – NotTheDr01ds Jul 29 '22 at 17:58
-
1you are absolutely right. However it doesn't hurt to leave the comment for those like me that came here by googling and are not at the exact same situation. – ndemou Jul 31 '22 at 11:00
-
1@ndemou I just realized -- If you had to add the user to `sudoers` in Ubuntu 22.04, then you may not have created the user correctly. Under 22.04, the normal groups that the WSL user should be in are `adm cdrom sudo dip plugdev lxd` (other than the `$USER` group, of course). As long as your user is in the `sudo` group, the default `sudoers` file will give you access without needing to edit it. – NotTheDr01ds Aug 02 '22 at 22:12
-
adding the entry to /etc/wsl.conf to set the default user does not appear to be working for me – Matt Warren Oct 24 '22 at 10:35
-
@MattWarren You did `wsl --terminate` after, right? Troubleshooting recommendations - Do a `wsl -l -v` to check that the default distribution is what you expect it to be. Do a `su -
` as root and confirm that you can change to that user manually. – NotTheDr01ds Oct 24 '22 at 11:30 -
Yep. no matter what I tried it ignored the entry in /etc/wsl.conf. I ended up using the -u option in the windows terminal profile that I use to launch a shell in the distro (ie: semi-method 4 above) – Matt Warren Oct 24 '22 at 23:26
-
1Thanks for the inclusion of `-u`. This answer is applicable to many more people than just imported distros. A coworker somehow installed Ubuntu without a user, so everything was defaulting to `root`. Created a user for him and then it all went to hell when we forgot to add him as a sudoer before setting his default user. So @ndemou, greatly appreciate the answer. – UtahJarhead Jun 07 '23 at 17:20
-
Thanks @UtahJarhead, and apologies to ndemou for not including this when you originally pointed out that it could be helpful. – NotTheDr01ds Jun 07 '23 at 22:48
-
The first method has worked for me as of 04/07/2023 for Ubuntu-22.04. – Wilfred Almeida Jul 04 '23 at 07:03
The normal command syntax is for example:
ubuntu config --default-user new_user_name
However, this does not work for an imported distro, which is started by the following command :
wsl --distribution <DistributionName>
Try this undocumented method:
- Use
regeditand navigate to the key:
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss - Examine its subkeys for a distribution that has the right name in the
item
DistributionName - Create or modify a DWORD item named
DefaultUidand set it to the user-id (uid) of your default user. Here root user is id0while the first user id is 1000 (0x3e8).
If this does not work for your setup, you would need to run as:
wsl --distribution ubuntu -u user_name
For more information see :
- 455,459
- 31
- 526
- 924
-
2The copy made with `wsl --import` does not contain that executable. I did `wsl --export Ubuntu UbuntuFile`, then `wsl --import Ubuntu2 UbuntuDirectory UbuntuFile`. The contents of `UbuntuDirectory` are `rootfs`, `temp`, `fsserver` and `UbuntuFile`. No executable. – schuelermine Jul 04 '20 at 13:47
-
Yes, forgot about it : there's a gotcha with import that means you need to execute it via wsl. I corrected my answer. – harrymc Jul 04 '20 at 13:55
-
This doesn't solve my problem. I know how to set the user for the original Ubuntu (I have two), and I know how to run the second Ubuntu, but how do I set the user for the second Ubuntu? – schuelermine Jul 04 '20 at 14:01
-
Answer corrected again, hoping that this method applies to your setup. Importing distributions is not ideal. – harrymc Jul 04 '20 at 14:12
-
-
-
@harrymc, imported distributions from a previous laptop (both Debian and Ubuntu) are all I use and I code against Linux every day. They work fine for me. I have never had a problem. – Señor CMasMas Jul 04 '20 at 15:15
-
-
this hint save me when my WSL started defaulting to `root` after some crash – Pawel Oct 20 '22 at 09:44
-
This is still the only way that worked for me. Launching `ubuntu2204.exe config --default-user myuser` didn't do anything. Does it expect UID "1000" instead of username or what? – Jorma Rebane May 31 '23 at 13:39
Verify the uid of the user that you want to set as default in your Linux console by id:
username@host:~$ id
uid=1000(username) gid=1000(username) groups=1000(username),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),117(netdev),1001(docker)
username@host:~$
Verify the distro name in Powershell by wsl -l -v:
PS C:\WINDOWS\system32> wsl -l -v
NAME STATE VERSION
* ubuntu-20.04 Running 2
docker-desktop-data Running 2
docker-desktop Running 2
PS C:\WINDOWS\system32>
Edit the registry in Powershell:
Get-ItemProperty Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\*\ DistributionName | Where-Object -Property DistributionName -eq ubuntu-20.04 | Set-ItemProperty -Name DefaultUid -Value 1000
Should be good by now.
- 51
- 1
- 1
If you are installing some custom distributions in WSL, you will not get a "distroname.exe" command to change the configuration. You may use LxRunOffline to change the default user. This toolset can be installed in Windows using choco install lxrunoffline.
Get the default user id of a distro
LxRunOffline allows you get and set the default user id (uid) for any installed distributions. For instance, if you have a distro named newdistro you may check the default user using gu (get default user) and -n with the name of the distribution:
lxrunoffline gu -n newdistro
The command returns a number, the user id of the default user:
- the
rootuser has the0user-id - the default user created during the install has the
1000user id.
Set the default user id for a distro
If you want to set the root as the default user, you can make it using su (set default user) with -n and the name of the distro, and -v with the user id, i.e., 0 for the root.
lxrunoffline su -n newdistro -v 0
- 2,169
- 21
- 20
-
Note that LxRunOffline ultimately does the same thing as the original answer - Set the registry key. However, the Microsoft recommended way is via `/etc/wsl.conf` (see my answer). – NotTheDr01ds Feb 19 '21 at 21:18
-
You are right. I typically use `lxrunoffline` to support old versions of WSL running on Windows LTSB machines (that you cannot upgrade). I think using `/etc/wsl.conf` is a more elegant solution than `lxrunoffline` in Windows Build 17093 and later – Jaime Feb 27 '21 at 12:14
If you are using Windows Terminal then you can configure a profile for your distribution.
In the Profiles settings for Windows Terminal select your distro's profile, then click the General Tab and under Command line you can use one of the previously recommended options for the wsl command.
For example, if you named your Profile Ubuntu and your username is bob, then you would set Command line to wsl.exe -d Ubuntu -u bob
While you're at it, you can configure the Starting directory, aka the one opened by default when you open a terminal for your distro with:
\\wsl$\Distro\home\username
Using the previous example, for a distro named Ubuntu and a username of bob, you would set Starting directory to:
\\wsl$\Ubuntu\home\bob
- 11
- 2
None of the answers worked for me on Windows 11 with an imported Ubuntu distribution. This is more than likely because my user is a domain user rather than local.
What worked is:
- 111
- 3
Normally went to the root user terminal edit the .bashrc
vim .bashrc
Append this line to the last of it
su - $name
- 11
-
1A problem with your solution is that you can use `wsl` to execute scripts and pipelines in Windows. I think that your solution only works if the user enter to the Linux environment. Changing the WSL configuration is a better alternative. You may check the [WSL documentation](https://docs.microsoft.com/en-us/windows/wsl/wsl-config) and many other tools, such as [WSLU](https://github.com/wslutilities/wslu) and [`lxrunoffline`](https://github.com/DDoSolitary/LxRunOffline) – Jaime Feb 27 '21 at 12:25

