How to Change the Root Password on Linux (WSL)

Changing the root password on a Linux distribution running under the Windows Subsystem for Linux (WSL) is a straightforward process. This article provides a concise guide for users looking to update the root password in their WSL environment.

Step-by-Step Instructions:

  1. Open Windows Terminal (cmd.exe) Begin by launching the Windows Command Prompt (cmd.exe) or Windows Terminal.

Confirm Password Change To verify that the new password for the root user has been set correctly, use the following command and input your password when prompted:

sudo echo hi

If successful, the terminal will output "hi", indicating that the sudo authentication works with the new password.

Access WSL as the User Start a new WSL session as the user by typing:

wsl

Optional: Again, if using multiple distributions:

wsl -d <distro-name>

Exit the Root Session After the password has been successfully changed, exit the root session by typing:

exit

Change the Password for a Specific User Once logged in as root, type the following command to change the password for your intended user:

passwd <username>

Replace <username> with the actual username. You will be prompted to enter a new password and confirm it.

Access WSL as the Root User Enter the following command to access your Linux distribution as the root user:

wsl -u root

Optional: If you are using multiple Linux distributions under WSL, specify the desired one by adding the -d flag:

wsl -u root -d <distro-name>

Conclusion

Changing the root password within WSL is a useful administrative task, particularly when managing user access or securing the environment. By following the steps outlined above, users can easily update credentials and maintain control over their WSL-based Linux systems.

Always ensure that your passwords are strong and securely stored to avoid unauthorised access.