Scott Hanselman

Keeping your WSL Linux instances up to date automatically within Windows 10

October 06, 2020 Comment on this post [9] Posted in Linux | Win10
Sponsored By

image[3]Hayden Barnes from Canonical, the folks that work on Ubuntu (lovely blog, check out it) had a great tweet where he recommended using the Windows Task Scheduler (think of it as a graphical cron job manager) to keep your WSL Linux instances up to date.

There's a few things to unpack here to get into the details.

First, if you run wsl --list -v you'll see all the WSL Linux Instances on your machine.

> wsl --list -v
NAME STATE VERSION
* Ubuntu-18.04 Running 2
kali-linux Stopped 1
Alpine Stopped 1
Ubuntu-20.04 Stopped 2
WLinux Running 2
docker-desktop-data Stopped 2
docker-desktop Stopped 2

You can I see I have a few. I spend most of my time in the Ubuntu instances, but I also occasionally drop into the kali-linux and WLinux instances. If I'm using LTS (long term support) distros then there's minimal risk (my opinion) in "apt get update" and "apt get upgrade"-ing them every week or so. I could even do it unattended.

I could set up a Task Scheduler and make an "on login" task or a weekly task that calls wsl.exe and passes in -d for distro, along with the name of the distro, run as root with -u and -e for the command. For example:

wsl -d "Wlinux" -u root -e apt update
wsl -d "Wlinux" -u root -e apt upgrade -y

Since I have several WSL instances, I could also make a "updateall.cmd" or .bat or .ps1 script and run them occasionally to keep them all updated on my own. Just change the -d and include the name of each distro. One could imagine a group policy as well for large enterprises to do the same thing for developers using a custom or managed WSL instance.

You would not want to update or mess with the docker- managed WSL instances above as they exist only to run your Docker Desktop-managed containers. Leave that to Docker to manage.

It's a whole new world out there, and I'm loving how I can move easily between multiple Linuxes on Windows 10. Check out my YouTube on WSL2 and please subscribe over there.


Sponsor: Never miss a beat with Seq. Live application logs and health checks. Download the Windows installer or pull the Docker image now.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
October 13, 2020 5:07
Windows Task Scheduler (think of it as a graphical cron job manager)
What's "cron"? If I had to make an educated guess, it is Linux's Task Scheduler, but without a good UI. 😉 Man, there is so many spelling and grammatical errors in this article.
October 13, 2020 17:15
Is it possible to chain the two commands together? e.g. wsl -d "Wlinux" -u root -e apt update && apt upgrade -y
October 13, 2020 19:09
I have tried a different tack at getting my WSL updated. I have a GitHub Action that will rebuild the image, along with updates. It still needs some work. The biggest issue is to preserve the home folder between image updates. We do something like this for rebuilding virtual servers where the server is rebuild every night, but the home directory is on a separate drive so your personal workspace is preserved. https://github.com/RobCannon/my-centos
October 14, 2020 7:25
Looks like you need to tweak your spam filter Scott. It seems they've finally gotten wise to the maths question.
October 15, 2020 10:01
Good tip, but I would use: apt full-upgrade -y
October 15, 2020 17:58
PowerShell looping doesn't seem to work. 😞
PS> wsl --list -q |? {$_ -ne "`0"} |% {wsl -d "$_" -u root -e 'apt update ; apt upgrade -y'}
There is no distribution with the supplied name.
PS> wsl --list -q |? {$_ -ne "`0"} |% {"wsl -d $_ -u root -e 'apt update ; apt upgrade -y'"}
wsl -d Ubuntu-20.04 -u root -e 'apt apdate ; apt upgrade -y'
PS> wsl -d Ubuntu-20.04 -u root -e 'apt apdate ; apt upgrade -y'
(runs fine)
October 19, 2020 17:20
As a user of "current" (non-LTS) Ubuntu for years - you're probably safe running your
apt update; apt upgrade
even then. Distro upgrades are done (on commandline) via
do-release-upgrade
, which is the only time I've had issues (and 90% of those were PostgreSQL major version changes, for which they provide cluster upgrade tools that have never failed for me). Thanks for this - I haven't played with WSL in a while (been booting into Xubuntu, a light Ubuntu variant), but I'll have to give it another look.
October 22, 2020 12:42
Does this introduce some security risks in WSL instances? Does any programs in Windows can execute any commands in the name of "root"? I try these commands in normal cmd.exe command line, they just run happily, without pop up the UAC things.
October 24, 2020 18:21
There's certainly a lot to find out about this issue. I like all of the points you've made. https://vanzari-parbrize.ro/parbrize/parbrize-mazda.html

(will show your gravatar icon)
2+5=?
Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, li, ol, pre, strike, strong, sub, super, u, ul) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Live Comment Preview

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.