I am having difficulty in setting a user password from Windows 10 using a script that contains the desired password. I have attempted to create a PowerShell .ps1 file with the command “wsl passwd” and “wsl testPassword” but this still prompts for the password. Additionally, I have tried using the pipe command twice with the passwd command, but it returned an error.
I am currently using Windows 10 Pro N 64-bit (desktop) version 1709, build 16299.192, with WSL Ubuntu 16.04.2 LTS installed.
I have also tried a method which includes the following steps:
- Setting the default user to root using the command “lxrun /setdefaultuser root /y”
- Creating a new user and setting it as the default user using the command “lxrun /setdefaultuser testuser /y”
- Executing a command as the new user to initialize any necessary settings in WSL, for example “wsl echo “hello world””
- Switching back to the root user using the command “lxrun /setdefaultuser root /y”
- Setting the path to the passwd file, which can be found at /etc/passwd using the command “wsl set path /etc/passwd”
- Changing the password for the non-root user using the command “wsl echo ‘testusername:newpasswd’ | chpasswd”
However, this resulted in an error message that ‘chpasswd’ is not recognized as an internal or external command, operable program or batch file.
3 Answers
Introduction
Windows Subsystem for Linux (WSL) is a feature on Windows 10 that allows users to run Linux applications directly on Windows without the need for a virtual machine. One of the key features of WSL is the ability to access the Linux file system from Windows and vice versa. However, if you want to change the password for a user in WSL, you may encounter some difficulties. In this blog post, we will explore some methods to change the WSL password in Windows 10.
Method 1: Using the passwd command
The passwd command is used in Linux to change the password of a user. To change the WSL password in Windows 10 using the passwd command, follow these steps:
1. Open the WSL terminal by typing “bash” in the Windows search bar and selecting “Bash on Ubuntu on Windows”.
2. Type “passwd” in the terminal and press Enter.
3. You will be prompted to enter the current password for the user. If you have not set a password for the user, you can skip this step.
4. Enter the new password for the user and press Enter.
5. Confirm the new password by entering it again and press Enter.
This method is straightforward and does not require any additional tools or scripts. However, it may not be suitable if you want to automate the password change process.
Method 2: Using a PowerShell script
If you want to change the WSL password using a PowerShell script, you can use the following command:
wsl echo ‘username:newpassword’ | chpasswd
Replace “username” with the username of the user whose password you want to change and “newpassword” with the new password. Here’s how to create the script:
1. Open Notepad or any text editor.
2. Type the command above in the editor.
3. Save the file with a .ps1 extension, for example, “changepassword.ps1”.
4. Open PowerShell as an administrator.
5. Type “Set-ExecutionPolicy RemoteSigned” and press Enter. This command allows you to run scripts on your computer.
6. Navigate to the directory where you saved the script using the “cd” command.
7. Type “.changepassword.ps1” and press Enter.
This method should change the password for the user without any prompts. However, you may encounter an error if the “chpasswd” command is not recognized. In that case, you can try the next method.
Method 3: Using a bash script
Another way to change the WSL password using a script is to use a bash script. Here’s how to create the script:
1. Open Notepad or any text editor.
2. Type the following commands in the editor:
#!/bin/bash
echo -e “currentpasswordnnewpasswordnnewpassword” | passwd username
Replace “currentpassword” with the current password for the user, “newpassword” with the new password, and “username” with the username of the user whose password you want to change.
3. Save the file with a .sh extension, for example, “changepassword.sh”.
4. Open the WSL terminal.
5. Navigate to the directory where you saved the script using the “cd” command.
6. Type “chmod +x changepassword.sh” and press Enter. This command makes the script executable.
7. Type “./changepassword.sh” and press Enter.
This method should change the password for the user without any prompts. However, you may encounter an error if the script is not executable. In that case, you can try the next method.
Method 4: Using the lxrun command
The lxrun command is used to manage the WSL installation on Windows 10. Here’s how to change the WSL password using the lxrun command:
1. Open the Command Prompt as an administrator.
2. Type “lxrun /setdefaultuser username /y” and press Enter. Replace “username” with the username of the user whose password you want to change.
3. Type “bash” and press Enter.
4. Type “passwd” and press Enter.
5. You will be prompted to enter the current password for the user. If you have not set a password for the user, you can skip this step.
6. Enter the new password for the user and press Enter.
7. Confirm the new password by entering it again and press Enter.
8. Type “exit” and press Enter.
9. Type “lxrun /setdefaultuser root /y” and press Enter.
This method should change the password for the user without any prompts. However, it requires you to switch to the WSL terminal, which may not be suitable if you want to automate the password change process.
Method 5: Using the wsl command
The wsl command is used to run Linux commands from the Windows command prompt. Here’s how to change the WSL password using the wsl command:
1. Open the Command Prompt as an administrator.
2. Type “wsl -u username passwd” and press Enter. Replace “username” with the username of the user whose password you want to change.
3. You will be prompted to enter the current password for the user. If you have not set a password for the user, you can skip this step.
4. Enter the new password for the user and press Enter.
5. Confirm the new password by entering it again and press Enter.
This method should change the password for the user without any prompts. However, it requires you to switch to the Windows command prompt, which may not be suitable if you want to automate the password change process.
Conclusion
Changing the WSL password in Windows 10 can be a bit tricky, but there are several methods you can use to accomplish this task. The method you choose depends on your requirements and preferences. If you want to change the password manually, you can use the passwd command in the WSL terminal. If you want to automate the password change process, you can use a PowerShell script, a bash script, the lxrun command, or the wsl command. With these methods, you should be able to change the WSL password without any issues.
To set a user password for Windows Subsystem for Linux (WSL) Ubuntu 16.04 from a Windows 10 script, you can try the following steps:
- Set the default user to root using the command:
lxrun /setdefaultuser root /y
- Create a new user and set it as the default user using the command:
lxrun /setdefaultuser testuser /y
- Execute a command as the new user to initialize any necessary settings in WSL, for example:
wsl echo "hello world"
- Switch back to the root user using the command:
lxrun /setdefaultuser root /y
- Set the path to the passwd file, which can be found at /etc/passwd:
wsl set path /etc/passwd
- Change the password for the non-root user using the command:
wsl echo 'testusername:newpasswd' | chpasswd
Note that in step 6, if you receive an error message that ‘chpasswd’ is not recognized as an internal or external command, operable program or batch file, it may be due to issues with piping commands when running WSL commands from cmd or powershell. In this case, you can try running the command manually in the WSL shell to see if it works.
When running Windows Subsystem for Linux (WSL) from the command-line, it does not create a login shell. This means that the PATH environmental variable is not set, as WSL inherits the PATH from the Windows parent CMD.
To specify the exact path to the passwd file, you can use the path /usr/bin/passwd, but it’s worth noting that this path might not be accurate as the product might be old.
It’s worth noting that the command “testPassword testPassword | passwd” does not pass two lines with the values “testPassword” and “testPassword”. It is not clear if this is necessary for the task.
A better command to use, if available in WSL, is “echo ‘userid:newpasswd’ | chpasswd”. Remember to include the full path and this command might require some “sudo” magic.