Skip to content

WSL Internet Connection Issues

WSL Version Information

This article covers solutions for both WSL 1 and WSL 2. Check your WSL version using:

bash
wsl -l -v

Problem Overview

Windows Subsystem for Linux (WSL) users frequently encounter internet connectivity issues where commands like sudo apt update fail with connection errors, and ping google.com shows packet loss. This problem can occur due to several reasons, including DNS configuration issues, VPN conflicts, or network stack problems.

Common Causes

The most frequent causes of WSL internet connectivity issues include:

  • Incorrect DNS configuration in /etc/resolv.conf
  • VPN software interference (especially Cisco AnyConnect)
  • Network adapter metric conflicts
  • NTFS compression on system files
  • Corrupted network stack in Windows
  • WSL version compatibility issues

Solutions

DNS Configuration Fix (Most Common)

bash
# Edit the resolv.conf file
sudo nano /etc/resolv.conf

# Replace the nameserver with a working DNS (e.g., Google DNS)
nameserver 8.8.8.8
nameserver 8.8.4.4
bash
# Remove the automatically generated file
sudo rm /etc/resolv.conf

# Create a new resolv.conf with custom DNS
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'

# Prevent WSL from overwriting the file
sudo bash -c 'echo "[network]" > /etc/wsl.conf'
sudo bash -c 'echo "generateResolvConf = false" >> /etc/wsl.conf'

# Make the file immutable
sudo chattr +i /etc/resolv.conf

File Immutability

The chattr +i command makes the file immutable. To modify it later, first run sudo chattr -i /etc/resolv.conf.

VPN-Specific Solutions

For users with Cisco AnyConnect or other VPN solutions:

powershell
# Get VPN adapter information
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000

# Get DNS server addresses
Get-DnsClientServerAddress -AddressFamily IPv4 | Select-Object -ExpandProperty ServerAddresses

# Get search domain
Get-DnsClientGlobalSetting | Select-Object -ExpandProperty SuffixSearchList
bash
# Create proper resolv.conf for VPN
sudo unlink /etc/resolv.conf
cat <<EOF | sudo tee /etc/wsl.conf
[network]
generateResolvConf = false
EOF

cat <<EOF | sudo tee /etc/resolv.conf
nameserver 10.50.xxx.xxx  # Company DNS from PowerShell
nameserver 8.8.8.8        # Fallback DNS
search yourdomain.com     # Your search domain
EOF

# Make resolv.conf immutable
sudo chattr +i /etc/resolv.conf

Network Stack Reset

If DNS changes don't help, reset the Windows network stack:

powershell
# Shutdown WSL
wsl --shutdown

# Reset network components
netsh winsock reset
netsh int ip reset all
netsh winhttp reset proxy
ipconfig /flushdns
bash
# Create a networkreset.bat file with these commands
wsl --shutdown
netsh winsock reset
netsh int ip reset all
netsh winhttp reset proxy
ipconfig /flushdns
shutdown /r

After running these commands, restart your computer and check if the issue is resolved.

NTFS Compression Issues

NTFS compression can cause WSL networking problems. Disable compression:

powershell
# Disable compression on the swap file
compact /u /s:"%TEMP%" /i /Q
1. Navigate to %TEMP% folder (usually C:\Users\Username\AppData\Local\Temp)
2. Right-click swap.vhdx
3. Select Properties → Advanced
4. Uncheck "Compress contents to save disk space"
5. Click OK

WSL Version Switch

If all else fails, consider switching between WSL 1 and 2:

powershell
# List installed distributions
wsl -l -v

# Set specific distribution to WSL 1
wsl --set-version <distribution-name> 1

# Set default version to WSL 1
wsl --set-default-version 1

WSL Version Trade-offs

  • WSL 1: Better networking but slower file system performance
  • WSL 2: Better performance but more networking complexities

Complete Reinstallation

As a last resort, completely reinstall WSL:

  1. Uninstall Ubuntu/Linux distribution from Settings → Apps
  2. Turn off "Windows Subsystem for Linux" in Windows Features
  3. Restart your computer
  4. Re-enable WSL in Windows Features
  5. Restart again
  6. Reinstall your Linux distribution

Advanced Troubleshooting

Corporate Environment Solutions

For corporate environments with restricted DNS:

powershell
# PowerShell script for corporate environments
$nameserver = Get-WmiObject -Namespace root\cimv2 -Query "Select dnsserversearchorder from win32_networkadapterconfiguration" | where {$_.DNSServerSearchOrder -ne $null} | select -ExpandProperty DNSServerSearchOrder
$nameserver = Out-String -InputObject $nameserver
sc -Path 'c:\Users\Public\Documents\resolv.conf' -Value ('nameserver ' + $nameserver) -Encoding utf8
[string]::Join( "`n", (gc 'c:\Users\Public\Documents\resolv.conf')) | sc '\\wsl$\debian\etc\resolv.conf'
wsl.exe

Testing Connectivity

Verify your connection with these diagnostic commands:

bash
# Test DNS resolution
nslookup google.com

# Test HTTP connectivity
curl -I https://google.com

# Check current nameservers
cat /etc/resolv.conf
bash
# Check network interfaces
ip addr show

# Trace route to identify where connection fails
traceroute google.com

# Check routing table
ip route show

Prevention

To prevent future internet connectivity issues:

  1. Regular updates: Keep Windows and WSL updated
  2. Backup configuration: Backup your /etc/resolv.conf and /etc/wsl.conf files
  3. VPN scripts: Create automation scripts if you frequently use VPN
  4. Documentation: Keep notes of what works for your specific setup

Conclusion

WSL internet connectivity issues typically stem from DNS configuration problems, especially when using VPN software or after network changes. The solutions range from simple DNS changes to more comprehensive network stack resets. For most users, configuring /etc/resolv.conf and preventing WSL from overwriting it resolves the issue.

Corporate Environments

Corporate network policies may require different approaches. Always check with your IT department before making significant network changes.

For ongoing issues, check the official WSL GitHub issues for the latest developments and solutions.