WSL Configuration Guide

Optimize your WSL environment with advanced configuration options, performance tuning, and customization.

WSL Configuration (.wslconfig)

Create a .wslconfig file in your Windows user directory (C:\Users\<username>\.wslconfig) to configure global WSL settings.

.wslconfig
# WSL 2 Global Configuration
[wsl2]
memory=8GB # Limit memory usage
processors=4 # Limit CPU cores
swap=2GB # Set swap file size
swapFile=C:\\temp\\wsl-swap.vhdx
localhostForwarding=true
nestedVirtualization=true
pageReporting=true
guiApplications=true

Note: After creating or modifying .wslconfig, restart WSL withwsl --shutdown for changes to take effect.

Distribution Configuration (wsl.conf)

Create a /etc/wsl.conf file inside your Linux distribution to configure distribution-specific settings.

Ubuntu: /etc/wsl.conf
# Network configuration
[network]
generateHosts = true
generateResolvConf = true
hostname = wsl-ubuntu
# Boot configuration
[boot]
systemd = true
# Automount configuration
[automount]
enabled = true
root = /mnt/
options = "metadata,umask=22,fmask=11"
# Interop configuration
[interop]
enabled = true
appendWindowsPath = true

To create this file, run: sudo nano /etc/wsl.conf

Performance Optimization

File System Performance

✅ DO: Store Linux files in WSL file system

Keep your project files in the Linux file system for best performance:

/home/username/projects/my-app

❌ AVOID: Cross-file-system operations

Avoid running Linux tools on Windows files (slower performance):

/mnt/c/Users/username/Desktop/my-app

Memory Management

WSL 2 uses a dynamic memory allocation. You can limit memory usage and reclaim unused memory:

PowerShell
# Reclaim memory from WSL
PS>wsl --shutdown
PS>Get-Process vmmem | Stop-Process -Force

Disk Space Management

WSL disk images can grow large over time. Here's how to compact them:

PowerShell (Admin)
# Shutdown WSL first
PS>wsl --shutdown
# Compact the disk
PS>diskpart
select vdisk file="C:\Users\<username>\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx"
compact vdisk
exit

Networking Configuration

Port Forwarding

Access services running in WSL from Windows or external networks:

PowerShell (Admin)
# Forward port 3000 from WSL to Windows
PS>netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=172.x.x.x
# Get WSL IP address
PS>wsl hostname -I

DNS Configuration

If you're experiencing DNS issues, you can configure custom DNS servers:

Ubuntu
user@PC:~$ sudo nano /etc/resolv.conf
# Add these lines:
nameserver 8.8.8.8
nameserver 1.1.1.1

Shell Configuration

Install Zsh and Oh My Zsh

Enhance your shell experience with Zsh and Oh My Zsh:

Ubuntu
user@PC:~$ sudo apt install zsh
user@PC:~$ sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Useful Aliases

Add these aliases to your ~/.bashrc or~/.zshrc:

~/.bashrc
# WSL Aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias ..='cd ..'
alias ...='cd ../..'
alias grep='grep --color=auto'
alias winhome='cd /mnt/c/Users/$USER'
alias desktop='cd /mnt/c/Users/$USER/Desktop'
alias documents='cd /mnt/c/Users/$USER/Documents'