How to change the default user name in WSL?

Many websites says I should use lxrun /setdefaultuser, but this command is deprecated:

C:\WINDOWS\system32>lxrun /setdefaultuser foo

Warning: lyrun.exe is only used to configure the legacy Windows Subsystem for Linux distribution.

How can I change the default username which is in my case root. I would like it to be the same username as I use with my Windows account.

Btw I am using mintty to run WSL:

%LOCALAPPDATA%\wsltty\bin\mintty.exe --WSL= -h err --configdir="%APPDATA%\wsltty" -~ 

Solution 1:

As of the time of this answer in 2021 (and for a few years now), 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 works in WSL1, WSL2, and regardless of the distribution name. It even works if the distribution wasn't installed from the Store.

Solution 2:

Microsoft has documented how this is done on Fall Creators Update and later. The commands suggested are only an example and might vary based on the WSL instance you are using.

This question was asked in 2017, so the name of the WSL instance, might no longer be applicable to a newer version of Ubuntu WSL or if a different WSL distribution is being used. The commands that are suggested will work with any WSL distrubition on the Windows Store.

To see what commands are available for a particular distribution, run [distro.exe] /?.

For example, with Ubuntu installed:

C:> ubuntu.exe /?

enter image description here

Which means in order to change the default user to root in Ubuntu you would use the following command.

C:\> ubuntu config --default-user root

The documentation indicates the following:

enter image description here

Source: User Accounts and Permissions