108

I have tried both commands

sudo passwd

and

sudo -u root /bin/bash

But as soon as I enter the new password twice, it says password has been updated. You close the shell and open a new instance, it is still working on the old root password.

Please help.

Dragonborn
  • 1,183
  • well sudo -u root can be a desperate attempt to get things working. but sudo passwd should change the password. and old one should not be required when i open a new WSL shell and try 'sudo apt-get install' – Dragonborn Jul 05 '17 at 06:48

4 Answers4

254

There is a simpler method.

  1. Open cmd.exe
  2. Type wsl -u root
  3. Type passwd username and change the password
  4. Type exit
  5. Type wsl
  6. Type sudo echo hi to confirm the new password works.
bicole
  • 2,981
  • 1
    The method is simpler and the answer more clear and straightforward, worked perfectly – Mickael V. Mar 13 '20 at 21:57
  • Also good for fixing anything preventing sudo from working – SEoF Jun 01 '20 at 13:31
  • This works for me. I am using WSL Legacy(Default). – Daggie Blanqx - Douglas Mwangi Nov 20 '20 at 15:44
  • One liner version that combines steps 2-4 -- wsl -u root passwd username – NotTheDr01ds Jul 14 '21 at 19:49
  • 3
    I don know why, but this command seems not working for me, yes while I enter wsl -u root it simply accesses ubuntu root user, then I enter passwd root it asks for a new password, I enter the new password, and confirm by retyping, after that while I start ubuntu terminal and use sudo su the new password not work ... – M. K Hossain Nov 26 '21 at 08:20
  • @M.KHossain, is it possible that after you entered wsl with wsl -u root you then ran passwd root instead of passwd ubuntu? – MikeyT Jun 16 '22 at 16:45
  • 1
    Oh how many times I have came back for this! Too many. – Niko Fohr Aug 26 '22 at 06:52
  • @M.KHossain You're resetting the password of the user that is doing the sudo. So in the normal console run whoami to determine your regular user. Then login as root to change the password for that user. – RiverHeart Feb 15 '24 at 14:39
  • If you have mutliple distributions, you can also it using argument -d. e.g. wsl -u root -d Ubuntu-22.04 – Titou Mar 04 '24 at 17:58
61
  1. in wsl, sudo passwd will change the password of the WSL root user.
  2. in wsl, passwd will change the password of the current WSL user
  • to specify which WSL user to use (temporarily) wsl -u [user]
  1. in wsl, passwd [user] will change the password of any WSL user
  2. in wsl, sudo generally asks for the password of the current WSL user.
  3. in windows cmd.exe, you can change the default WSL user (permanently):
  • in current versions: ubuntu.exe config --default-user [user]
  • in legacy versions 1703, 1709: lxrun /setdefaultuser [user]
  • a restart may be required

You probably want to change the password of the sudo-capable, non-root user.

You probably do not want to change the root users password (because in Ubuntu, the root user generally should not have a password, sudo should be used instead). You probably also do not want to leave your default user as root. Even when working form windows, its still a bad practice.

Official Microsoft documentation on User Accounts and Permissions can be found here: https://msdn.microsoft.com/en-us/commandline/wsl/user_support

anx
  • 2,348
  • I had to close Ubuntu and restart it for ubuntu config --default-user [user] to work. – Stefan Lasiewski Sep 07 '19 at 21:03
  • Just a quick comment, as mentioned here: https://askubuntu.com/a/966537/340341 , you need to follow the ubuntu config line with sc stop LxssManager and sc stop LxssManager for the update to work in the current shell – Marawan Okasha Nov 22 '19 at 13:36
  • Relevant section of your link: https://docs.microsoft.com/en-us/windows/wsl/user-support?redirectedfrom=MSDN#forgot-your-password Microsoft uses the root user as example. Do they share bad practices? Why is it bad practice to give root a password? – varnaud Jun 17 '20 at 11:49
29

It is assumed you are using Ubuntu in this explanation.

  • If you forgot your password on WSL simply open your command prompt from windows by typing cmd on search.
  • Then type ubuntu config --default-user root to set root as the default user for Linux Bash Shell.
  • Then open the Linux Bash shell which will log you in as root without asking for password.
  • Then use passwd username to reset the password of any user.
  • Use ubuntu config --default-user username to reset back to your normal Linux user account.
abu_bua
  • 10,783
  • /bin/bash/: config: command not found – Geoffrey Aug 08 '19 at 10:34
  • 1
    @systemovich - you need to run the ubuntu config ... commands from a windows command prompt (cmd.exe). To open the bash shell from there, just type wsl and hit Enter. To return to the windows command prompt, from the WSL prompt, type exit and hit enter – StingyJack Sep 09 '19 at 18:38
  • 9
    There is a simpler method.
    1. Open cmd.exe
    2. Type wsl -u root
    3. Type passwd username and change the password
    4. Type exit
    5. Type wsl
    6. Type sudo echo h' to confirm the new password works.
    – bicole Jan 29 '20 at 15:03
  • @bicole you can probably remove this comment as your provider answer is already up ahead of this answer :) and thanks! – lacostenycoder Oct 08 '22 at 08:35
9

There is no need to reconfigure Ubuntu, but, if you have more than one distribution installed and Ubuntu is not your default, you must also specify the distribution in addition to the root user.

  • Option 1 -- Ubuntu is the default WSL distribution:

    wsl.exe -u root
    
  • Option 2 -- Ubuntu is not the default WSL distribution:

    1. First determine the distribution name:

      wsl.exe -l -v
      
    2. Then use the distribution name with -d/--distribution:

      wsl.exe -d <distro> -u root
      

Then go about your business to reset your regular user account. Stealing the one-liner from @NotTheDr01ds, assuming your distribution name is "Ubuntu":

wsl.exe -d Ubuntu -u root passwd username

Typical Ubuntu distribution names include:

  • Ubuntu: When installed from the Microsoft Store or wsl --install as the "unversioned" app.
  • Ubuntu-18.04, Ubuntu-20.04, or Ubuntu-22.04: When installed from the Microsoft Store or wsl --install as a specific version of Ubuntu.
NotTheDr01ds
  • 17,888
John T.
  • 191