Skip to content

Commit f72bef7

Browse files
committed
Deploy WSL with a regular user account
This matches the result you get when you deploy it interactively. The only difference is we set the password for you.
1 parent a0d8067 commit f72bef7

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ To run a recipe script, click a link in the table below from your target machine
4848
| | Submit a PR with a recommended configuration! |
4949

5050
**Notes:**
51-
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`
52-
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).
51+
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).
5352
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).
5453

5554
## Known issues

scripts/WSL.ps1

+44-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,52 @@ choco install -y Microsoft-Windows-Subsystem-Linux --source="'windowsfeatures'"
44
# TODO: Move this to choco install once --root is included in that package
55
Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1804 -OutFile ~/Ubuntu.appx -UseBasicParsing
66
Add-AppxPackage -Path ~/Ubuntu.appx
7-
# run the distro once and have it install locally with root user, unset password
7+
# run the distro once and have it install locally. The default account is "ubuntu:ubuntu".
88

99
RefreshEnv
10-
Ubuntu1804 install --root
11-
Ubuntu1804 run apt update
12-
Ubuntu1804 run apt upgrade -y
10+
11+
$distro = "ubuntu1804"
12+
$username = "ubuntu"
13+
$password = "ubuntu"
14+
15+
& $distro install --root
16+
if ($LASTEXITCODE -ne 0) { throw "Could not install distro." }
17+
18+
# the only non-interactive way to set up a WSL distro is the --root flag
19+
# https://github.com/microsoft/WSL/issues/3369
20+
# but it has the side effect of making all `wsl` calls run as root,
21+
# so users still need to manually intervene to set up a regular account.
22+
# Workaround this by installing with --root but then replicating,
23+
# noninteractively, what happens in the WSL DistroLauncher:
24+
# https://github.com/microsoft/WSL-DistroLauncher/blob/2ed9a9335fc89a688a5150c95eff4fbdbc830f25/DistroLauncher/DistributionInfo.cpp#L8-L33
25+
& $distro run useradd -m "$username"
26+
if ($LASTEXITCODE -ne 0) { throw }
27+
& $distro run sh -c 'echo "${username}:${password}" | chpasswd' # wrapped in sh -c to get the pipe to work
28+
if ($LASTEXITCODE -ne 0) { throw }
29+
& $distro run chsh -s /bin/bash "$username"
30+
if ($LASTEXITCODE -ne 0) { throw }
31+
& $distro run usermod -aG adm,cdrom,sudo,dip,plugdev "$username"
32+
if ($LASTEXITCODE -ne 0) { throw }
33+
34+
35+
# apt install -y isn't enough to be truly noninteractive
36+
$env:DEBIAN_FRONTEND = "noninteractive"
37+
$env:WSLENV += ":DEBIAN_FRONTEND"
38+
39+
# update software
40+
& $distro run apt-get update
41+
if ($LASTEXITCODE -ne 0) { throw }
42+
& $distro run apt-get full-upgrade -y
43+
if ($LASTEXITCODE -ne 0) { throw }
44+
& $distro run apt-get autoremove -y
45+
if ($LASTEXITCODE -ne 0) { throw }
46+
& $distro run apt-get autoclean
47+
if ($LASTEXITCODE -ne 0) { throw }
48+
wsl --terminate "Ubuntu-18.04" # instead of 'reboot'
49+
if ($LASTEXITCODE -ne 0) { throw }
50+
51+
& $distro config --default-user "$username"
52+
if ($LASTEXITCODE -ne 0) { throw }
1353

1454
<#
1555
NOTE: Other distros can be scripted the same way for example:

0 commit comments

Comments
 (0)