Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unattended install of WSL #3369

Closed
Naadwo opened this issue Jul 9, 2018 · 14 comments
Closed

Unattended install of WSL #3369

Naadwo opened this issue Jul 9, 2018 · 14 comments
Labels

Comments

@Naadwo
Copy link

Naadwo commented Jul 9, 2018

  • Your Windows build number: Microsoft Windows [Version 10.0.17134.112]

  • What you're doing and what's happening: No way to do unattended install of WSL

  • What's wrong / what should be happening instead:
    I have to install WSL on several machine and I would like to do it in automated manner (as a part of a workstation setup, so it should work without any prompts etc). I've tried with this approach: https://docs.microsoft.com/en-us/windows/wsl/install-on-server but it still requires input... Is there a way to setup WSL without user action?

@Naadwo
Copy link
Author

Naadwo commented Jul 9, 2018

The 4th step of installation on page that I linked is Create a UNIX user - this is a prompt that I can't skip, I would like to be able to provide this user (eventually keep default).

@MVoz
Copy link

MVoz commented Jul 9, 2018

lxrun.exe /?

@Biswa96
Copy link

Biswa96 commented Jul 9, 2018

@Owdaan That user creating command adduser was written in the launcher e.g. ubuntu.exe. YOu may close the command prompt window after installing but the default user will be root. To remove that adduser you have to manually write a small binary.

@Naadwo
Copy link
Author

Naadwo commented Jul 9, 2018

@voskrese I was using lxrun, but it is deprecated.
@Biswa96 Ok this may work, but still it would be nice to have that without extra custom launcher...

@MVoz
Copy link

MVoz commented Jul 9, 2018

@Owdaan ubuntu.exe /?

adduser Owdaan
su Owdaan
exit
exit

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\ ... ID

"DefaultUid"=dword:00000000 - root - 0
"DefaultUid"=dword:000003e8 - user1(Owdaan) - 1000

2018-07-09_214106

@Naadwo
Copy link
Author

Naadwo commented Jul 9, 2018

@voskrese I don't see how this may help.
It would actually help if --default-user switch would work, but it is ignored during installation.

Regarding edit - of course I can set that, but first I need to have WSL installed (and installer always prompts for username)

@WSLUser
Copy link

WSLUser commented Jul 9, 2018

You want microsoft/windows-dev-box-setup-scripts#32 and probably microsoft/windows-dev-box-setup-scripts#33 as well. Feel free to pitch in to help with those initiatives.

@benhillis
Copy link
Member

Distributions that are up-to-date with the distribution launcher sample have an option for non-interactive install. I know the Ubuntu 18.04 app supports this:
ubuntu1804.exe install --root

@Naadwo
Copy link
Author

Naadwo commented Jul 10, 2018

@benhillis This looks exactly what I need - is there an public link to new version of Ubuntu (1804) like it was for 1604? I can't find any and I'm not sure if i will get access to businessstore...

@WSLUser
Copy link

WSLUser commented Jul 10, 2018

@Owdaan They released this blog post yesterday to help address your question (other than business store) https://blogs.msdn.microsoft.com/commandline/2018/07/09/upgrading-ubuntu/

@kousu
Copy link

kousu commented Mar 20, 2021

ubuntu1804.exe install --root

Isn't good enough for my use case. I want an unattended install that is the same as installing from the store, but where I supply a default account and password. I'm trying to set up a dev environment, and I don't want my devs to have to worry about running su all the time. With install --root: wsl drops you to root, which is a very different environment when you're trying to build and test software, but without it the install is not unattended.

Even the internal Microsoft boxstarter team punts on this: https://github.com/microsoft/windows-dev-box-setup-scripts/blob/d488050/README.md#how-to-run-the-scripts

If you are using WSL there's a followup step we recommend after running the setup script. When the script finishes you will only have a root user with a blank password. You should manually create a non-root user via $ sudo adduser [USERNAME] sudo with a non-blank password. Use this user going forward. For more info on WSL please refer to the documentation.

without really explaining how to "use this user going forward".

Using @MVoz's very helpful deep knowledge, I was able to make an almost-scripted install by:

Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1804 -OutFile ~/Ubuntu.appx -UseBasicParsing
Add-AppxPackage -Path ~/Ubuntu.appx

RefreshEnv
Ubuntu1804 install --root
Ubuntu1804 run apt update
Ubuntu1804 run apt upgrade -y

Ubuntu1804 run adduser -m user
Ubuntu1804 run 'echo user:password | chpasswd'
Ubuntu1804 run 'chsh -s /bin/bash user'

reg.exe query HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\ # to get the GUID of the container
reg.exe add 'HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\{1422a095-2e43-4bf8-8108-d209cabe2a07}' /v DefaultUid /t REG_DWORD /d 1000 /f

It's not totally scripted, because the "get the GUID" step is hard. I'm not familiar enough (yet!) with Powershell to work out how to parse that out (oh, I guess there's a whole powershell regedit API isn't there?).

Um, anyway I guess I've answered my own question: you can do an unattended install if you just need linux with a root user, but you can't really do it if you want a regular user account, at least not yet.

kicking it upstream: microsoft/WSL-DistroLauncher#90

@kousu
Copy link

kousu commented Mar 21, 2021

I got an unattended install with a non-root user account fully working thanks to powershell:

Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1804 -OutFile ~/Ubuntu.appx -UseBasicParsing
Add-AppxPackage -Path ~/Ubuntu.appx

RefreshEnv
ubuntu1804 install --root

# apt install -y isn't enough to be truly noninteractive
$env:DEBIAN_FRONTEND = "noninteractive"
$env:WSLENV += ":DEBIAN_FRONTEND"

# update software
wsl -u root apt-get update
wsl -u root apt-get full-upgrade -y
wsl -u root apt-get autoremove -y
wsl -u root apt-get autoclean
wsl --shutdown  # instead of 'reboot'

# create user account
wsl -u root useradd -m "$username"
wsl -u root sh -c "echo "${username}:${password}" | chpasswd" # wrapped in sh -c to get the pipe to work
wsl -u root  chsh -s /bin/bash "$username"
wsl -u root usermod -aG adm,cdrom,sudo,dip,plugdev "$username"

# patch up WSL so that $username is the default user
# this workaround from @MVoz: https://github.com/microsoft/WSL/issues/3369#issuecomment-403481209
$uid = (wsl -u root id -u $username)

echo "Setting default WSL user to $username ($uid)"
$distro = (Get-ItemProperty -Path Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss -Name DefaultDistribution).DefaultDistribution
$cur_id = (Get-ItemProperty -Path Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\$distro -Name DefaultUid).DefaultUid
Set-ItemProperty -Path Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\$distro -Name DefaultUid -Value $uid

@kousu
Copy link

kousu commented Mar 21, 2021

Ahh I didn't read the docs enough, there's $distro config --default-user. So this does it:

Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1804 -OutFile ~/Ubuntu.appx -UseBasicParsing
Add-AppxPackage -Path ~/Ubuntu.appx
RefreshEnv

$username = "ubuntu"
$password = "ubuntu"

ubuntu1804 install --root

# create user account
wsl -u root useradd -m "$username"
wsl -u root sh -c "echo "${username}:${password}" | chpasswd" # wrapped in sh -c to get the pipe to work
wsl -u root  chsh -s /bin/bash "$username"
wsl -u root usermod -aG adm,cdrom,sudo,dip,plugdev "$username"

ubuntu1804 config --default-user "$username"


# apt install -y isn't enough to be truly noninteractive
$env:DEBIAN_FRONTEND = "noninteractive"
$env:WSLENV += ":DEBIAN_FRONTEND"

# update software
wsl -u root apt-get update
wsl -u root apt-get full-upgrade -y
wsl -u root apt-get autoremove -y
wsl -u root apt-get autoclean
wsl --shutdown  # instead of 'reboot'

@kousu
Copy link

kousu commented Mar 21, 2021

Or, this is probably even better:

Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1804 -OutFile ~/Ubuntu.appx -UseBasicParsing
Add-AppxPackage -Path ~/Ubuntu.appx
RefreshEnv

$distro = "ubuntu1804" # unfortunately, there's no obvious way to get this value out of the previous command
$username = "ubuntu"
$password = "ubuntu"

& $distro install --root

# create user account
& $distro run useradd -m "$username"
& $distro run sh -c "echo "${username}:${password}" | chpasswd" # wrapped in sh -c to get the pipe to work
& $distro run chsh -s /bin/bash "$username"
& $distro run usermod -aG adm,cdrom,sudo,dip,plugdev "$username"

& $distro config --default-user "$username"

This makes doing initial system updates trickier though, because DistroLauncher doesn't come with -u; maybe it's better to leave that up to the user, but if you do want to include it in your initial provisioning then either:

$env:DEBIAN_FRONTEND = "noninteractive"
$env:WSLENV += ":DEBIAN_FRONTEND"
wsl -u root -D Ubuntu-18.04 sh -c 'apt-get update && apt-get full-upgrade -y && apt-get autoremove -y && apt-get autoclean'

or:

$env:DEBIAN_FRONTEND = "noninteractive"
$env:WSLENV += ":DEBIAN_FRONTEND"
& $distro config --default-user "root"
& $distro run sh -c 'apt-get update && apt-get full-upgrade -y && apt-get autoremove -y && apt-get autoclean'
& $distro config --default-user "$username"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants