How-To

Hands-On with WSL: Resetting the User Password

If you forget your password while trying to run sudo from your WSL terminal, there's an easy way to reset it.

Windows Subsystem for Linux (WSL) allows you to run a Linux terminal from a Windows 10 or a Windows Server system, and having powerful Linux tools in a Windows environment can be a godsend. One of the nice features of WSL is that it automatically logs you in as a default user when you bring up the WSL terminal without having to enter the default user's password. This is ordinarily a good thing, but there is one situation I've encountered when this can cause a minor roadblock.

When using WSL, some operations require you to have root privileges, which you can accomplish by using the sudo command. When you use sudo, it will prompt you for the password for the user that you're currently logged in as. The other day I logged into my WSL terminal and attempted to run sudo, only to realize that I had forgotten my password.

On a normal Linux system, you can reset the password by booting into GRUB and resetting it from there. But because WSL doesn't go through a normal boot process, this same method will not work. Fortunately, there is a way to easily reset the password for WSL, and in this article I'll show you how.

To see what options are available for a particular WSL Linux distribution, you can append "/?" to the command to instantiate that distribution. For example, because I'm running Ubuntu on my system, I ran ubuntu.exe  /? from the Windows command window and I got the output shown in Figure 1.

[Click on image for larger view.] Figure 1. Ubuntu.exe options.

From this output, I could see that it has a config –default-user option; using this option, I could switch the default user to root, log in to WSL as root, change the password for my WSL user, and then change the default user back to something other than root.

I ran the following commands from the Windows command prompt to reset my password. Anything after the # is a comment:

ubuntu config --default-user root
ubuntu.exe # (to run WSL in the current Windows command prompt)
whoami # (to see who I'm currently logged in as)
tail -3 /etc/passwd # (to see the last three users added to the Ubuntu system)
passwd user01 # (to change the password for user01)
exit # (to exit WSL)
ubuntu config --default-user user01 # (to set the default user for the WSL terminal)

The output from these commands is shown in Figure 2.

Figure 3 shows that after resetting the password, I could use the new password to sudo to root without any issues.

[Click on image for larger view.] Figure 2. WSL Ubuntu password change.
[Click on image for larger view.] Figure 3. WSL Verifying password change.

I was surprised at how easy it was to change the password for my Ubuntu WSL instance. However, the process to change the default user's password for other Linux distributions may be different, so make sure to check the documentation or run that distribution's instanton command with the append "/?" to decipher the process to reset your password.

About the Author

Tom Fenton has a wealth of hands-on IT experience gained over the past 30 years in a variety of technologies, with the past 20 years focusing on virtualization and storage. He previously worked as a Technical Marketing Manager for ControlUp. He also previously worked at VMware in Staff and Senior level positions. He has also worked as a Senior Validation Engineer with The Taneja Group, where he headed the Validation Service Lab and was instrumental in starting up its vSphere Virtual Volumes practice. He's on X @vDoppler.

Featured

Subscribe on YouTube