73

I installed the WSL Bash on Windows 10 and set my user to e.g. 'abc' but now I want to change it to something else.

How can I change the user name of the user I'm logged in as?

When I open bash it automatically logs me in with my current user.

3
  • 5
    As of now, Ubuntu 20.04 is current. After its installed using WSL 2, the ubuntu2004 command becomes available for use: ubuntu2004 config --default-user robert
    – BSalita
    Jun 23, 2020 at 11:18
  • 4
    For those who didn't installed via store (e.g via export/import): github.com/microsoft/WSL/issues/3974#issuecomment-576782860. Default user can be changed by creating a /etc/wsl.conf file Nov 3, 2020 at 12:27
  • Thanks so much @SebastianHoffmann, this comment is what made me realise that it was all due to the import. Thought when I exported that everything would be an exact copy, but I guess not.
    – EmSixTeen
    Jan 21, 2021 at 13:17

8 Answers 8

62

Now that LxRun.exe is deprecated, if you install Ubuntu from the Windows store the command you will need to run at a windows command prompt is

ubuntu config --default-user <username>

(Note: depending on which version you installed this could be ubuntu1804.exe or ubuntu2004.exe)

Restart LxssManager service

sc stop LxssManager
sc start LxssManager
8
  • 5
    ubuntu might NOT be your default, it depends which version you installed so check the answers below, as mine is "ubuntu1804 config ..."
    – rmcsharry
    Apr 25, 2019 at 11:01
  • 8
    ubuntu1804 config --default-user ... for me
    – masterxilo
    Jul 11, 2019 at 18:13
  • 1
    Is there a way to do this same action but using the 'bash' command?
    – rmutalik
    Dec 2, 2019 at 19:07
  • 1
    instead of setting the default user it opened bash for me. It does not seem to be working for some reason. I am on the latest release of Windows as of today
    – akxer
    Dec 4, 2019 at 17:09
  • 1
    With WSL 2 in use, it was not required to restart the LxSSManager service for the container to use the change
    – Laice
    Aug 23, 2021 at 12:01
54

The top answer (ubuntu config --default-user root) is currently correct for the default instance. But if you have multiple instances, you need to work slightly harder:

Inside the instance, as root, create/edit /etc/wsl.conf and add these lines:

[user]
default=username

Then close it, run wsl --terminate <distro name> in PowerShell, and restart it.

That's from an issue in the WSL repo. The registry hack answer is also in there, but /etc/wsl.conf is now officially the supported solution.

8
  • 7
    For the ones who have moved the distro file from C to other drive like me. This worked in a go.
    – Asif Ali
    Dec 17, 2020 at 8:41
  • 3
    This should be the accepted answer as this also works for custom installed distributions.
    – papercrane
    Jan 29, 2021 at 19:22
  • 3
    This works for my distro that was moved to another drive using wsl --export and wsl --import Mar 24, 2021 at 15:45
  • 2
    I needed to change my default user from root to my own user and this was the answer. Jul 11, 2021 at 5:01
  • 1
    /etc/wsl.conf solution worked for me
    – Adil
    Jul 13, 2021 at 12:53
30

All other answers were helpful, but can be other scenarios too, follow here as per yours. Mine was Ubuntu 16.04, so I used the following:

ubuntu1604 config --default-user <username>

If you installed Ubuntu 18.04:

ubuntu1804 config --default-user <username>

If you used the default one, then:

ubuntu config --default-user <username>
3
  • Should this not change the current username to the username I provide in <username>? When I try this it just tells me the username I am providing does not exist. This is on 18.0.4
    – wuno
    Apr 26, 2019 at 17:09
  • U may want to use root as user??
    – kakabali
    Jun 27, 2019 at 3:50
  • 1
    this should be the ANSWER, today at 2020-02-15, this is the only option works.
    – Luke
    Feb 15, 2020 at 2:24
13

You can change the default user for bash by running the following command in a normal command prompt (cmd.exe):

LxRun.exe /setdefaultuser <new_name>
13

If you're running Ubuntu 18.04 from the Microsoft Store, the command (powershell or cmd) will be:

ubuntu1804 config --default-user <username>
4

In CMD

See which distros you have installed wslconfig /l

I have Ubuntu and I wanted to set the default user to root ubuntu config --default-user root

Documentation: https://docs.microsoft.com/en-us/windows/wsl/user-support

0
2

ubuntu was not recognized by cmd.exe (Windows 10 / WSL Ubuntu 20.04), so the solutions above did not work for me.

What worked for me (from Windows terminal):

wsl.exe -u root
sudo usermod -l newUsername oldUsername
sudo usermod -d /home/newHomeDir -m newUsername
2
  • This is the correct answear, because it is changing the username, not the user itself. Also, if you get the message that username can't be changed because a process is running, just sudo kill processId Jun 25, 2021 at 16:46
  • 1
    And you also probably want to change the group name 'sudo groupmod -n newUsername oldUsername' Jul 1, 2021 at 0:37
1

Old question that got bumped today, but the answers need updating and consolidating to be current (and correct).

There are two parts to the question, and every answer here so far just covers one or the other.

First question:

How can I change the user name of the user I'm logged in as?

As @PJ127 correctly pointed out, the first step is to actually change the username within Ubuntu. This can most easily be done (now, in 2021) by exiting WSL and starting PowerShell, then:

wsl -l -v
wsl --terminate <distro> # <distro> from the previous command
wsl -u root -d <distro> # "-d <distro>" is only needed for non-default instances
# Now from in WSL
usermod -l newname oldname 
# Optionally change the home directory name to match
usermod -d /home/doug -m doug
# Optionally change the group name to match
groupmod -n newname oldname

Then there's the second part of the question, which is what most of the answers here attempt to cover:

When I open bash it automatically logs me in with my current user

There are multiple ways of doing this, but the Microsoft recommended/documented method in 2021 is as @Jacktose answered:

  • In your WSL instance, sudo editor /etc/wsl.conf with the following contents:

    [user]
    default=username
    

    Exit, then from PowerShell, wsl --terminate <distro name> and restart WSL.

While the ubuntu.exe --config method will work, there are two problems:

  • It's not always "ubuntu.exe". The name of the executable (technically "app execution alias") depends on which version you installed from the Store. Ubuntu has come in multiple Store packages over the years. There's a package for each supported release (e.g. ubuntu2004.exe, etc.) and one for the "current" version (ubuntu.exe). And while this is Ask Ubuntu, it also could be alpine.exe, etc. if you are using a different distribution.

  • It also, as has been pointed out in other answers, does not work if you have more than one instance installed (which is a great feature of WSL). It will only work for the first, store-installed instance.

Then, of course, there are registry-based methods of changing the default name, but since we have safer, supported methods let's not even cover those here. :-)

1
  • Setting the default user didn't work for me until I added that user. Now it works like a charm. (WSL with VS Code just used root, no additional user was added until I did this)
    – ps2goat
    Apr 5 at 16:50

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