Basic WSL Commands

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# List available distributions
wsl --list
wsl -l                  # Short form
wsl -l -v               # Show detailed info with versions

# Start/Stop WSL
wsl --shutdown         # Stop all distributions
wsl --terminate <distro>  # Stop specific distribution

# Set default distribution
wsl --set-default <distro>

# Set default WSL version
wsl --set-default-version 2

Distribution Management

Installation

1
2
3
4
5
6
7
8
9
# List available distributions
wsl --list --online

# Install a distribution
wsl --install Ubuntu
wsl --install -d Ubuntu-20.04

# Unregister/Remove a distribution
wsl --unregister <distro>

Backup and Restore

1
2
3
4
5
6
7
# Export (Backup) a distribution
wsl --export Ubuntu "D:\Backups\ubuntu.tar"
wsl --export Ubuntu-Dev "D:\Backups\Ubuntu-Dev.tar"

# Import (Restore) a distribution
wsl --import Ubuntu "C:\WSL\Ubuntu" "D:\Backups\ubuntu.tar"
wsl --import Ubuntu-Dev "C:\WSL\Ubuntu-Dev" "D:\Backups\Ubuntu-Dev.tar"

File System Access

1
2
3
4
5
6
7
# Access Windows files from WSL
cd /mnt/c/Users/YourUsername
cd /mnt/d/Projects

# Access WSL files from Windows
# Windows path: \\wsl$\Ubuntu\home\username
# Windows Explorer: \\wsl.localhost\Ubuntu

Network Management

1
2
3
4
5
6
7
8
9
# Get WSL IP address
ip addr show eth0

# Port forwarding
netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=$(wsl hostname -I)

# Reset WSL network
wsl --shutdown
netsh winsock reset

System Resources

1
2
3
4
5
6
# Configure memory limits
# In %UserProfile%\.wslconfig:
[wsl2]
memory=8GB
processors=4
swap=2GB

Development Setup

VSCode Integration

1
2
3
# Install Remote WSL extension
code .                  # Open current directory in VSCode
code project_name      # Open specific project

Docker Integration

1
2
3
4
# Enable Docker integration
wsl --install Ubuntu
wsl --set-version Ubuntu 2
# Install Docker Desktop with WSL 2 backend

Troubleshooting

Common Issues

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Fix DNS issues
sudo rm /etc/resolv.conf
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'

# Fix file permissions
sudo chmod 644 /etc/resolv.conf
sudo chown root:root /etc/resolv.conf

# Reset WSL
wsl --shutdown
wsl --terminate Ubuntu

Performance Optimization

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# In .wslconfig
[wsl2]
memory=8GB
processors=4
swap=2GB
localhostForwarding=true

# In .bashrc or .zshrc
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1

Best Practices

  1. File System Access

    • Store project files in WSL file system for better performance
    • Use /mnt/c/ only when necessary
    • Avoid running git operations in Windows file system
  2. Resource Management

    • Configure appropriate memory limits
    • Monitor resource usage
    • Clean up unused distributions
  3. Development Workflow

    • Use VSCode Remote WSL extension
    • Configure Git in WSL
    • Use WSL-native tools over Windows versions
  4. Security

    • Keep WSL distributions updated
    • Use appropriate file permissions
    • Don’t run unnecessary services

Useful Commands Reference

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Version and status
wsl --version
wsl --status

# Run commands
wsl --exec command     # Run command in default distribution
wsl -d Ubuntu command  # Run command in specific distribution

# Update WSL
wsl --update

# Convert versions
wsl --set-version <distro> 2  # Convert to WSL 2

# Clean up
wsl --shutdown        # Stop all distributions
wsl --terminate <distro>  # Stop specific distribution