Skip to content

Unattended install of WSL #3369

Closed
Closed

Description

@Naadwo
  • 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?

Activity

  1. Naadwo commented on Jul 9, 2018

    @Naadwo
    Author

    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).

  2. MVoz commented on Jul 9, 2018

    @MVoz

    lxrun.exe /?

  3. Biswa96 commented on Jul 9, 2018

    @Biswa96

    @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.

  4. Naadwo commented on Jul 9, 2018

    @Naadwo
    Author

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

  5. MVoz commented on Jul 9, 2018

    @MVoz

    @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

  6. Naadwo commented on Jul 9, 2018

    @Naadwo
    Author

    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)

  7. WSLUser commented on Jul 9, 2018

    @WSLUser

    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.

  8. benhillis commented on Jul 9, 2018

    @benhillis
    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

  9. Naadwo commented on Jul 10, 2018

    @Naadwo
    Author

    Ben Hillis (@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...

  10. WSLUser commented on Jul 10, 2018

    @WSLUser

    @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/

  11. kousu commented on Mar 20, 2021

    @kousu
    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 Vos (@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

  12. kousu commented on Mar 21, 2021

    @kousu

    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
    
  13. kousu commented on Mar 21, 2021

    @kousu

    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'
    
  14. kousu commented on Mar 21, 2021

    @kousu

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions