Skip to content

Commit 8a96bc0

Browse files
authored
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 8a96bc0

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

scripts/WSL.ps1

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

1453
<#
1554
NOTE: Other distros can be scripted the same way for example:

0 commit comments

Comments
 (0)