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
- 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:
- 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
.
- 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.
lxrun
is history now. You should upgrade Windows 10 immediately if you want to use WSL. Password can be added only by logging in as root.