Click to collapse/expand
WSL2 regenerates /etc/resolv.conf on each restart, overwriting any manual DNS configuration. This causes recurring connectivity failures, particularly in corporate or VPN environments where the automatically injected nameservers are unreachable or incorrect.
fix_wsl2_networking sets static DNS nameservers of your choice and makes the configuration files immutable, preventing WSL2 from reverting them on restart.
The script makes two changes:
1. /etc/wsl.conf
Sets generateResolvConf=false under the [network] section to tell WSL2 not to regenerate /etc/resolv.conf on startup.
2. /etc/resolv.conf
Replaces the file with a static configuration pointing to the DNS nameservers of your choice.
Both files are then marked immutable with chattr +i, which prevents WSL2 from modifying or replacing them even if generateResolvConf is ignored. Any existing files are backed up with a timestamp before modification.
To revert, run the script with --undo: this removes the immutable flags and deletes both files, allowing WSL2 to regenerate them on next restart.
Download the latest release from PowerShell:
Invoke-WebRequest `
-Uri "https://github.com/franckferman/fix_wsl2_networking/releases/latest/download/fix_wsl2_networking.sh" `
-OutFile "$env:USERPROFILE\fix_wsl2_networking.sh"Then from WSL2, copy from the Windows filesystem and run:
cp /mnt/c/Users/<YourWindowsUsername>/fix_wsl2_networking.sh ~/
chmod +x ~/fix_wsl2_networking.sh
sudo ~/fix_wsl2_networking.shsudo ./fix_wsl2_networking.sh [OPTIONS]| Option | Description |
|---|---|
--dns <provider> |
Use a specific DNS provider (non-interactive) |
-y, --yes |
Skip confirmation prompt |
--undo |
Remove immutable flags and delete config files |
--dry-run |
Preview changes without modifying anything |
-h, --help |
Show help message |
--version |
Show version number |
| Provider | Primary | Secondary |
|---|---|---|
| Cloudflare | 1.1.1.1 | 1.0.0.1 |
| Comodo | 8.26.56.26 | 8.20.247.20 |
| Digitalcourage | 85.214.20.141 | 46.182.19.48 |
| DNSWatch | 84.200.69.80 | 84.200.70.40 |
| FDN | 80.67.169.12 | 80.67.169.40 |
| 8.8.8.8 | 8.8.4.4 | |
| Mullvad | 194.242.2.9 | 194.242.2.2 |
| OpenDNS | 208.67.222.222 | 208.67.220.220 |
| Quad9 | 9.9.9.9 | 149.112.112.112 |
Interactive mode (select DNS provider from a menu):
sudo ./fix_wsl2_networking.shNon-interactive mode with a specific provider:
sudo ./fix_wsl2_networking.sh --dns Cloudflare --yesPreview changes without applying them:
sudo ./fix_wsl2_networking.sh --dry-runRevert to automatic DNS configuration:
sudo ./fix_wsl2_networking.sh --undoOnce the script completes, restart WSL2 for the changes to take full effect:
wsl --shutdownLicensed under the GNU Affero General Public License v3.0. See LICENSE for details.