Skip to content

Instantly share code, notes, and snippets.

@bouzou4
Created June 9, 2023 20:04
Show Gist options
  • Save bouzou4/3cacbc63447725ef31af2c524bfc4152 to your computer and use it in GitHub Desktop.
Save bouzou4/3cacbc63447725ef31af2c524bfc4152 to your computer and use it in GitHub Desktop.

WSL2 Cheatsheet

WSL2, or Windows Subsystem for Linux 2, allows you to run a full Linux kernel directly on Windows. This powerful feature unlocks a wide range of possibilities, making it possible to run a full-fledged Linux environment on your Windows machine without the need for a dual-boot or virtual machine setup.

Installation and Configuration

Before you can use WSL2, you'll need to install it and configure your system:

Action Command
Enable WSL (from PowerShell as Admin) dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
Enable Virtual Machine Platform (from PowerShell as Admin) dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Set WSL2 as default version (from PowerShell as Admin) wsl --set-default-version 2
Install a Linux distribution Visit the Microsoft Store and choose a Linux distribution

Note: The above commands should be run in PowerShell with administrator privileges. After running these commands, you will need to restart your machine.

Session Management

Session management involves starting up and shutting down your WSL2 instance. Here are some of the common commands:

Action Command
Start default WSL2 distro wsl
Start specific WSL2 distro wsl -d distroname
Shutdown all WSL2 instances wsl --shutdown
List distros (shows running) wsl --list --verbose or wsl -l -v
Set default distro wsl --setdefault distroname

Interoperability Commands

WSL2 allows you to run Windows commands from WSL2 and vice versa. This interoperability greatly enhances productivity:

Action Command
Run Windows command from WSL2 cmd.exe /c command
Run PowerShell command from WSL2 powershell.exe -Command command
Run WSL2 command from Windows wsl command
Access Windows files from WSL2 /mnt/c/path...
Access WSL2 files from Windows \\wsl$\distroname\path...

Useful Aliases

Aliases can help simplify complex commands. Add these to your .bashrc or .zshrc file:

Action Command
Restart WSL2 alias wslrestart='wsl --shutdown && wsl'
Update Linux alias update='sudo apt update && sudo apt upgrade -y'

Managing Distributions

WSL2 allows for multiple Linux distributions to be installed simultaneously. Here's how to manage them:

Action Command
List installed distributions wsl --list --verbose
Set default distribution wsl --set-default distroname
Unregister (remove) a distribution wsl --unregister distroname
Convert a distribution to WSL1 or WSL2 wsl --set-version distroname 1 or wsl --set-version distroname 2

Network Configuration

Networking in WSL2 is somewhat different from traditional Windows networking. Here are some common networking commands:

Action Command
Show network configuration ifconfig or ip addr
Access a Windows service from WSL2 Use the IP address from ipconfig in Windows, not localhost
Access a WSL2 service from Windows Use localhost

WSL2 allows you to seamlessly integrate your Windows and Linux environments, enhancing your productivity and expanding your toolset. This cheat sheet covers the basics to get you started, but there's much more to explore and discover!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment