How to set the `WSL` password from Windows 10?

Scenario

After automatically installing a fresh installation of Windows Subsystems for Linux Ubuntu 16.04 with commands:

lxrun /install /y
lxrun /setdefaultuser exampleUsername /y

a new installation is installed with username: exampleUsername. Next, if one wants to execute a command with sudo priviliges, e.g. yes | sudo apt update, one is prompted for the password of exampleUsername twice.

Question

How can I set that user password from Windows 10 using a script has the password?

Attempts

  1. Create a password changing powershell .ps1 file with content:
wsl passwd
wsl testPassword
wsl testPassword

But that returns still a manual prompt for the password when it's executed:

PS C:\twInstall> ./pw.ps1 Changing password for exampleUser. (current) UNIX password:

  1. Tried piping the password twice to the passwd command with pw.ps1 content:
wsl testPassword testPassword | passwd

which returns error:

passwd : The term 'passwd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\twInstall\pw.ps1:1 char:25

  • wsl testtest testtest | passwd
  •                     ~~~~~~
    
    • CategoryInfo : ObjectNotFound: (passwd:String) [], CommandNotFoundException
    • FullyQualifiedErrorId : CommandNotFoundException

Comment feedback adaptation

Currently, the scripts are run on a on a Windows 10 Pro N 64 bit (desktop), version 1709, build 16299.192 with WSL Ubuntu 16.04.2 LTS.

  1. Based on the answer suggested by @harrymc:

3.1 First set the default user to root with command:

lxrun /setdefaultuser root /y

3.2 Then create another the user (by setting it to default user) with command:

lxrun /setdefaultuser testuser /y

3.3 run a command as that user just incase anything needs to be initialised in the wsl for the creation of the user:

wsl echo "hello world"

3.4 Switch back to root user with command:

lxrun /setdefaultuser root /y

3.5 Then set the path to the passwd file (manual inspection found it at: /etc/passwd hence:

wsl set path /etc/passwd

3.6 Then change the password for the non-root user with command:

wsl echo 'testusername:newpasswd' | chpasswd

which returns:

'chpasswd' is not recognized as an internal or external command, operable program or batch file.

3.7 Retrying with sudo yielded the same error:

wsl sudo echo 'testusername:newpasswd' | chpasswd

returns:

'chpasswd' is not recognized as an internal or external command, operable program or batch file.

3.8 Since concerns were raised whether wsl Ubuntu 16.04.2 LTS had the capability of chpassword, I tried it afterwards in the wsl manually which did not throw any errors with command:

echo 'testusername:newpasswd' | chpasswd

3.9 Apparently there occurs an error in the conversion from:

echo 'testusername:newpasswd' | chpasswd

to:

wsl echo 'testusername:newpasswd' | chpasswd

I am suspecting it is due to errors in how piping works when you run a wsl command from cmd or powershell.


Running WSL from the command-line does not create a login shell. Specifically, the PATH is not set, as WSL inherits the PATH environmental variable from the Windows parent CMD.

You need to specify exactly the path to the passwd file, which might be /usr/bin/passwd (but I'm not using this old product).

As another remark, the command testPassword testPassword | passwd does not pass two lines having testPassword and testPassword, but I don't think that this is required.

The chpasswd command is better (if available in WSL):

echo 'userid:newpasswd' | chpasswd

Remember to add the full path, and this might need some sudo magic.