When working with Windows Subsystem for Linux (WSL), you might notice that changes to your Windows hosts
file (%WINDIR%\System32\drivers\etc\hosts
) don't automatically propagate to WSL's /etc/hosts
. This happens because:
- WSL only reads the Windows hosts file during initial startup
- The copy is cached for the WSL instance's lifetime
- Simply closing and reopening terminals doesn't trigger a refresh
Method 1: Restarting the WSL Instance
The most reliable way is to completely restart your WSL instance:
wsl --shutdown
wsl
This terminates all WSL instances and creates a fresh one with updated hosts.
Method 2: Using a Custom Script
For frequent updates, create a bash script:
#!/bin/bash
# Force update WSL hosts file
sudo cp /mnt/c/Windows/System32/drivers/etc/hosts /etc/hosts
sudo chmod 644 /etc/hosts
sudo service networking restart
Save as update-hosts.sh
and make it executable with chmod +x update-hosts.sh
.
Method 3: Automating the Process
For developers who frequently modify hosts, consider this PowerShell automation:
# PowerShell script to watch for hosts file changes
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Windows\System32\drivers\etc"
$watcher.Filter = "hosts"
$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite
Register-ObjectEvent $watcher "Changed" -Action {
wsl --shutdown
wsl
}
For those running multiple WSL distributions:
wsl --shutdown
wsl -d Ubuntu-20.04
wsl -d Debian
Remember that each distribution maintains its own /etc/hosts
copy.
A full Windows reboot achieves the same result as wsl --shutdown
because:
- All WSL instances are terminated during shutdown
- The fresh start reads the current Windows hosts file
- No cached versions remain in memory
When working with Windows Subsystem for Linux (WSL), you might notice that the /etc/hosts
file gets automatically populated from the Windows host file (%WINDIR%\System32\drivers\etc\hosts
) during the first WSL instance launch. However, subsequent modifications to the Windows hosts file don't automatically propagate to WSL.
The current behavior requires either:
- A full Windows system reboot
- Completely shutting down all WSL instances and restarting
This can be particularly frustrating during development when you're:
- Testing local domain configurations
- Working with containerized environments
- Developing microservices with custom host entries
Here are three reliable methods to force a refresh:
Method 1: Full WSL Shutdown
Run these commands in PowerShell:
wsl --shutdown
wsl
This completely terminates all WSL instances and forces a fresh initialization.
Method 2: Temporary File Replacement
Create a script in your WSL instance:
#!/bin/bash
sudo cp /dev/null /etc/hosts
sudo wsl.exe -d "$(wsl.exe -l | head -n 2 | tail -n 1 | awk '{print $1}')" -u root -e cp /etc/hosts /etc/hosts.bak
sudo wsl.exe -d "$(wsl.exe -l | head -n 2 | tail -n 1 | awk '{print $1}')" -u root -e cp /mnt/c/Windows/System32/drivers/etc/hosts /etc/hosts
Method 3: Using wsl.conf (Permanent Solution)
Create or modify /etc/wsl.conf
:
[network]
generateHosts = false
Then manually copy your Windows hosts file:
sudo cp /mnt/c/Windows/System32/drivers/etc/hosts /etc/hosts
For frequent updates, create a PowerShell function:
function Update-WslHosts {
wsl --shutdown
wsl -d Ubuntu-20.04 -- sudo cp /mnt/c/Windows/System32/drivers/etc/hosts /etc/hosts
}
After refreshing, verify the changes with:
cat /etc/hosts
Or test specific entries with:
ping mylocal.dev