17

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.

23

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

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.

10

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 regedit and 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 DefaultUid and set it to the user-id (uid) of your default user. Here root user is id 0 while 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 :

8
  • The 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. Jul 4 '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 4 '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? Jul 4 '20 at 14:01
  • Answer corrected again, hoping that this method applies to your setup. Importing distributions is not ideal.
    – harrymc
    Jul 4 '20 at 14:12
  • Should the UID be in hexadecimal format? Jul 4 '20 at 14:17
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 root user has the 0 user-id
  • the default user created during the install has the 1000 user 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
  • 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). Feb 19 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 at 12:14
1

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

-2

Normally went to the root user terminal edit the .bashrc

vim .bashrc

Append this line to the last of it

su - $name
1
  • 1
    A 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 and many other tools, such as WSLU and lxrunoffline
    – Jaime
    Feb 27 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.