WSL (Forgot Root Password)

WSL (Forgot Root Password)

Resetting Root Password in Ubuntu <Distribution> WSL

ajeet@alpha2:~$ su -
Password:
su: Authentication failure

PS C:\Users\gupta> wsl -l -v
  NAME            STATE           VERSION
* Ubuntu-22.04    Running         2
  Debian          Running         2
PS C:\Users\gupta> wsl --terminate Ubuntu-22.04
PS C:\Users\gupta> wsl -d Ubuntu-22.04 -u root
Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 5.10.102.1-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage


This message is shown once a day. To disable it please create the
/root/.hushlogin file.
root@alpha2:/mnt/c/Users/gupta# passwd
New password:
Retype new password:
passwd: password updated successfully
root@alpha2:/mnt/c/Users/gupta# exit

ajeet@alpha2:~$ su -
Password:
root@alpha2:~#

To change the password for the root user in WSL (Windows Subsystem for Linux), you first need to access the Linux distribution running within WSL. Here's a step-by-step guide based on your provided output:

  1. List all running WSL distributions and their versions:

     wsl -l -v
    
  2. Terminate the WSL instance that you want to change the root password for. For example, if you want to change the root password for the Ubuntu-22.04 instance:

     wsl --terminate Ubuntu-22.04
    
  3. Start the WSL instance with root access:

     wsl -d Ubuntu-22.04 -u root
     # -d referes to distribution and -u refer to user
    
  4. Once you're logged in as root, use the passwd command to change the password:

     passwd
    

    It will prompt you to enter a new password and then to retype the new password for confirmation.

  5. After changing the password successfully, you might see a message like "passwd: password updated successfully".

  6. If you need to switch back to your regular user (e.g., "ajeet" in your case), you can do so using:

     wsl -d Ubuntu-22.04 -u ajeet
    

Remember to replace "Ubuntu-22.04" with the name of your WSL distribution if it's different. Also, make sure to replace "ajeet" with your actual username if it's different.