How to set default user for manually installed WSL distro?
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Music Box Puzzles
--
Chapters
00:00 How To Set Default User For Manually Installed Wsl Distro?
00:29 Answer 1 Score 44
01:44 Answer 2 Score 2
02:52 Accepted Answer Score 185
05:59 Answer 4 Score 1
06:54 Thank you
--
Full question
https://superuser.com/questions/1566022/...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#linux #windows #useraccounts #windowssubsystemforlinux
#avk47
ACCEPTED ANSWER
Score 185
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.
ANSWER 2
Score 44
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>
(Edit: Reports were made that the above now also works for imported distros.)
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 id0
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 :
ANSWER 3
Score 2
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 the0
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
ANSWER 4
Score 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