Skip to content

Commit

Permalink
Deploy WSL with a regular user account
Browse files Browse the repository at this point in the history
This matches the result you get when you deploy it interactively. The only difference is we set the password for you.
  • Loading branch information
kousu committed Mar 21, 2021
1 parent a0d8067 commit f72bef7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ To run a recipe script, click a link in the table below from your target machine
| | Submit a PR with a recommended configuration! |

**Notes:**
1. 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](https://docs.microsoft.com/en-us/windows/wsl/about).
1. If you are using WSL note that the default password is "ubuntu". You will need it for `sudo`, but not to connect to the system. For more info on WSL please refer to the [documentation](https://docs.microsoft.com/en-us/windows/wsl/about).
2. If you're a Node.js contributor working on Node.js core, please see the [Node.js Bootstrapping Guide](https://github.com/nodejs/node/tree/master/tools/bootstrap) or [click here to run](http://boxstarter.org/package/nr/url?https://raw.githubusercontent.com/nodejs/node/master/tools/bootstrap/windows_boxstarter).

## Known issues
Expand Down
48 changes: 44 additions & 4 deletions scripts/WSL.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,52 @@ choco install -y Microsoft-Windows-Subsystem-Linux --source="'windowsfeatures'"
# TODO: Move this to choco install once --root is included in that package
Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1804 -OutFile ~/Ubuntu.appx -UseBasicParsing
Add-AppxPackage -Path ~/Ubuntu.appx
# run the distro once and have it install locally with root user, unset password
# run the distro once and have it install locally. The default account is "ubuntu:ubuntu".

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

$distro = "ubuntu1804"
$username = "ubuntu"
$password = "ubuntu"

& $distro install --root
if ($LASTEXITCODE -ne 0) { throw "Could not install distro." }

# the only non-interactive way to set up a WSL distro is the --root flag
# https://github.com/microsoft/WSL/issues/3369
# but it has the side effect of making all `wsl` calls run as root,
# so users still need to manually intervene to set up a regular account.
# Workaround this by installing with --root but then replicating,
# noninteractively, what happens in the WSL DistroLauncher:
# https://github.com/microsoft/WSL-DistroLauncher/blob/2ed9a9335fc89a688a5150c95eff4fbdbc830f25/DistroLauncher/DistributionInfo.cpp#L8-L33
& $distro run useradd -m "$username"
if ($LASTEXITCODE -ne 0) { throw }
& $distro run sh -c 'echo "${username}:${password}" | chpasswd' # wrapped in sh -c to get the pipe to work
if ($LASTEXITCODE -ne 0) { throw }
& $distro run chsh -s /bin/bash "$username"
if ($LASTEXITCODE -ne 0) { throw }
& $distro run usermod -aG adm,cdrom,sudo,dip,plugdev "$username"
if ($LASTEXITCODE -ne 0) { throw }


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

# update software
& $distro run apt-get update
if ($LASTEXITCODE -ne 0) { throw }
& $distro run apt-get full-upgrade -y
if ($LASTEXITCODE -ne 0) { throw }
& $distro run apt-get autoremove -y
if ($LASTEXITCODE -ne 0) { throw }
& $distro run apt-get autoclean
if ($LASTEXITCODE -ne 0) { throw }
wsl --terminate "Ubuntu-18.04" # instead of 'reboot'
if ($LASTEXITCODE -ne 0) { throw }

& $distro config --default-user "$username"
if ($LASTEXITCODE -ne 0) { throw }

<#
NOTE: Other distros can be scripted the same way for example:
Expand Down

0 comments on commit f72bef7

Please sign in to comment.