96

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.

mlevit
  • 1,069
  • 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 '20 at 11:18
  • 5
    For those who didn't installed via store (e.g via export/import): https://github.com/microsoft/WSL/issues/3974#issuecomment-576782860. Default user can be changed by creating a /etc/wsl.conf file – Sebastian Hoffmann Nov 03 '20 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 '21 at 13:17

9 Answers9

91

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)

If you're using WSL2, you're done.

For WSL1, you'll need to also restart LxssManager service

sc stop LxssManager
sc start LxssManager
KDEx
  • 1,011
  • 7
  • 4
82

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.

Jacktose
  • 939
  • 6
  • 9
37

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>
Zanna
  • 70,465
kakabali
  • 471
15

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

ubuntu1804 config --default-user <username>
Zanna
  • 70,465
rainabba
  • 277
14

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>
8

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. :-)

NotTheDr01ds
  • 17,888
  • 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 05 '22 at 16:50
5

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

4

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
PJ127
  • 281
  • 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 – pmiguelpinto90 Jun 25 '21 at 16:46
  • 1
    And you also probably want to change the group name 'sudo groupmod -n newUsername oldUsername' – pmiguelpinto90 Jul 01 '21 at 00:37
0

Use wsl --list command to see what WSL distribution you have installed. It may return Ubuntu-22.04 or some other version you may have installed. Alternatively you can run the following from WSL Ubuntu terminal session to get your Ubuntu version:

grep "DISTRIB_RELEASE=" /etc/lsb-release | grep -E [0-9]{2}.[0-9]{2} -o

Having Ubuntu version clarified run the command below from Windows CMD to set your default user, making sure that replacing 2204 to the specific version you have (obtained using commands above):

ubuntu2204 config --default-user <username>
Mike
  • 119
  • 3
  • 2
    This looks to be exactly the same information that was already given in this answer from 5 years ago. While we welcome new answers to old questions, we do ask that they be new ;-). If you are just adding the ubuntu2204 for the latest release, then that should be a simple edit to the existing answer. If you'd like to make that edit, I'd be happy to approve it for you. Thanks! – NotTheDr01ds Dec 02 '22 at 12:27
  • Well I explicitly added on how to get Ubuntu version used instead of listing couple of versions to be common at the moment of the answer. that was the only new part... I didn't noticed that part covered in other answers so decided to add mine – Mike Dec 02 '22 at 14:01
  • 2
    Understood - Just looks like information that should probably have been edited in to the other answer. Sometimes it's a tough call, I know. But look at the other similar edits that other users have made to that answer. – NotTheDr01ds Dec 02 '22 at 14:04
  • When building on other people's answers it is important to give credit. These purpose of these communities is to share knowledge therefore it is important to link duplicate or similar answers. – theYnot Dec 09 '22 at 21:39