π§ 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:
- Full Linux compatibility
- High performance and fast startup
- Seamless integration with the Windows filesystem
- Native networking
- Support for Linux tools such as Python, Git, OpenSSH, Docker, and Ansible
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:
- Running Linux tools on a Windows workstation
- Development environments (Python, Node, Go, Rust, etc.)
- Gitβbased workflows
- Container development
- Automation and scripting
- Running Ansible as a Linux control node
- Accessing both Windows and Linux files from the same terminal
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:
- IP:
172.29.x.x - Gateway:
172.29.x.1
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:
- Edit /etc/wsl.conf
- Add the following,
[network]
hostname = my-wsl-node
generateHosts = false
- Shutdown WSL completely using the following command from the Windows PowerShell,
wsl --shutdown
- Restart WSL
π 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:
- Work directly in Windows folders
- Clone repositories into WSL for better performance
- Use symbolic links between environments
π§° 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.