3

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.

5
  • 2
    First of all 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.
    – Biswapriyo
    Commented Jul 25, 2019 at 9:01
  • 1
    Please edit your question to indicate which version of Windows 10 you are using. What WSL commands are and are not possible entirely depend on the version of Windows 10 you are using. Until you provide this information you will get inaccurate (and incorrect) answers.
    – Ramhound
    Commented Jul 25, 2019 at 12:07
  • 1
    Your latest edit did not indicate what version of Windows 10 you are using
    – Ramhound
    Commented Jul 25, 2019 at 12:42
  • Thank you, the version and build have been included in the comment feedback adaptation.
    – a.t.
    Commented Jul 25, 2019 at 12:47
  • @Biswapriyo, could you perhaps explain how upgrading the Windows 10 is required to use WSL? Since I am currently be able to use the legacy Ubuntu 16.04.2 LTS, which is an old version of the WIndows Subsystem for Linux (WSL) without the update.
    – a.t.
    Commented Jul 25, 2019 at 13:00

2 Answers 2

2

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.

1
  • The password will be visible in command prompt window.
    – Biswapriyo
    Commented Jul 25, 2019 at 12:07
0

These are not necessarily the minimal steps, but they are verified in the following system type: 10 Pro N 64 bit (desktop), version 1709, build 16299.192 with WSL Ubuntu 16.04.2 LTS, source here.

After having enabled "WSL" in the windows features and a reboot:

  1. First set the default user to root with command:
lxrun /setdefaultuser root /y
  1. Then create another the user (by setting it to default user) with command:
lxrun /setdefaultuser testusername /y
  1. Switch back to root user with command:
lxrun /setdefaultuser root /y
  1. Then change the password for the non-root user with command:
bash -c "echo 'testusername:mypassword' | chpasswd"

Appearently the same command, as given by @harrymc does not work with wsl in front of it, but does work with bash -c.

  1. Verification: First actually open the WSL in the command prompt window:
wsl su testusername
  1. Then execute a command that requires a password of the user named: testusername:
su testusername

returns with the incorrect password:

testusername@DESKTOP-SomeDesktop:/mnt/c/Users/a$ su testusername Password: su: Authentication failure

returns with the correct password:

testusername@DESKTOP-SomeDesktop:/mnt/c/Users/a$ su testusername Password: To run a command as administrator (user "root"), use "sudo ". See "man sudo_root" for details.

From that output, one can conlude that the above sequence of commands indeed set the password mypassword for username testusername.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .