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 9:07
<blockquote >Windows Task Scheduler (think of it as a graphical cron job manager)</blockquote>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 21:15
Is it possible to chain the two commands together?
e.g. <i>wsl -d "Wlinux" -u root -e apt update && apt upgrade -y</i>
October 13, 2020 23: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 11: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 14:01
Good tip, but I would use: apt full-upgrade -y
October 15, 2020 21:58
PowerShell looping doesn't seem to work. 😞
<pre>
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'
<em>(runs fine)</em>
</pre>
October 19, 2020 21:20
As a user of "current" (non-LTS) Ubuntu for years - you're probably safe running your <pre>apt update; apt upgrade</pre> even then. Distro upgrades are done (on commandline) via <pre>do-release-upgrade</pre>, 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 16: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.
November 03, 2020 16:38
Hi there! I know this is kinda off topic but I was wondering which blog platform are you using for this website?
I'm getting fed up of Wordpress because I've had problems with hackers and
I'm looking at options for another platform.
I would be fantastic if you could point me in the direction of a good
platform. https://vanzari-parbrize.ro/parbrize/parbrize.php

Comments are closed.

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