🐧 WSL2

Using Ubuntu 24.04 on Windows 10/11

This page provides an introduction to WSL2 (Windows Subsystem for Linux v2) and focuses on WSL usage, management, networking, hostname configuration, SSH access, and common administrative commands. Examples use Ubuntu 24.04, the recommended distribution.


πŸ’‘ What Is WSL2?

WSL2 is a lightweight virtualization layer built into Windows that runs a real Linux kernel inside a highly optimized virtual machine. It provides:

WSL2 behaves like a Linux VM but with far less overhead, making it ideal for development, automation, and homelab workflows.


βš™οΈ Why Use WSL2?

WSL2 is useful for:

Because WSL2 uses a real Linux kernel, it supports nearly all Linux software without modification.

For details on how to use WSL2 as an Ansible Control node,

πŸ‘‰ See: Configure WSL2 as an Ansible Control Node Runbook.


πŸ—οΈ Installing WSL2 (Ubuntu 24.04)

Open PowerShell as Administrator:

wsl --install -d Ubuntu-24.04

If WSL is already installed:

wsl --install
wsl --list --online
wsl --install -d Ubuntu-24.04

Reboot if prompted.


▢️ Starting and Stopping WSL2

Start WSL

wsl

Start a specific distribution

wsl -d Ubuntu-24.04

Stop all WSL instances

wsl --shutdown

Stop a specific distribution

wsl --terminate Ubuntu-24.04

πŸ“¦ Listing and Managing Distributions

List installed distributions

wsl --list --verbose

List available distributions

wsl --list --online

Set default distribution

wsl --set-default Ubuntu-24.04

Unregister (delete) a distribution

(This permanently deletes the Linux filesystem)

wsl --unregister Ubuntu-24.04

πŸ”„ Updating Ubuntu 24.04

Inside WSL:

sudo apt update && sudo apt upgrade -y

🌐 WSL2 Networking: IP Address and Hostname

WSL2 uses a virtualized NAT network. This affects IP addressing, hostname resolution, and SSH access.


πŸ“‘ WSL2 IP Address

Inside WSL:

ip addr show eth0

Typical values:

Note:
WSL2 IP addresses are dynamic and change every time WSL restarts.


🧭 Static IP Support

WSL2 does not support static IP assignment.

Because WSL2 uses a lightweight NAT‑based virtual network, the IP is recreated on each boot.


🏷️ Configuring the WSL2 Hostname

Inside WSL:

[network]
hostname = my-wsl-node
generateHosts = false
wsl --shutdown

πŸ” Connecting to the WSL2 Host Using SSH

WSL2 can run an OpenSSH server, allowing SSH access from Windows or even other machines (with port forwarding).


πŸ› οΈ Step 1 β€” Install and Enable OpenSSH Server

Inside WSL:

sudo apt install openssh-server
sudo systemctl enable ssh
sudo systemctl start ssh

🌍 Step 2 β€” Find the WSL2 IP Address

ip -4 addr show eth0

Example:

172.29.112.5

πŸ’» Step 3 β€” SSH from Windows

From PowerShell or CMD:

ssh <username>@<wsl-ip>

Example:

ssh frank@172.29.112.5

🌐 SSH From Another Machine (LAN Access)

WSL2 is behind a NAT and not directly reachable from the LAN.

To allow external SSH access, configure Windows port forwarding:

netsh interface portproxy add v4tov4 listenport=22 listenaddress=0.0.0.0 connectport=22 connectaddress=<WSL-IP>

Then allow the firewall rule:

New-NetFirewallRule -DisplayName "WSL SSH" -Direction Inbound -Protocol TCP -LocalPort 22 -Action Allow

You can then SSH from another machine using the Windows host’s LAN IP.

ℹ️ NOTE The IP address of the WSL host changes whenever it restarts, which requires configuring Windows port forwarding.


πŸ“ Accessing Windows Files from WSL

Windows drives are mounted under /mnt:

/mnt/c/Users/<username>/Documents
/mnt/d/

You can:


🧰 Useful WSL Commands (Quick Reference)

Install a distribution

wsl --install -d Ubuntu-24.04

Get the WSL IP Address

wsl ip -4 addr show eth0

or to get just the IP address cleanly,

wsl ip -4 -o addr show eth0 | awk '{print $4}' | cut -d/ -f1

Start WSL

wsl

Start a specific distro

wsl -d Ubuntu-24.04

Shutdown all WSL instances

wsl --shutdown

Terminate a specific distro

wsl --terminate Ubuntu-24.04

List installed distros

wsl --list --verbose

List available distros

wsl --list --online

Set default distro

wsl --set-default Ubuntu-24.04

Export a distro

wsl --export Ubuntu-24.04 ubuntu-backup.tar

Import a distro

wsl --import Ubuntu-24.04 C:\WSL\Ubuntu ubuntu-backup.tar

Delete a distro

wsl --unregister Ubuntu-24.04

πŸ“ Summary

WSL2 provides a powerful, lightweight Linux environment directly inside Windows. It is ideal for development, automation, scripting, and running tools like Python, Git, Docker, and Ansible. With Ubuntu 24.04, WSL2 becomes a flexible and efficient platform for both personal and professional workflows.