Skip to content

franckferman/fix_wsl2_networking

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CI Contributors Stargazers License

fix_wsl2_networking

fix_wsl2_networking

Permanently fix DNS connectivity issues in WSL2.

Table of Contents

Click to collapse/expand
  1. About
  2. How it works
  3. Getting Started
  4. Usage
  5. License
  6. Contact

About

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.

Tested on

(back to top)

How it works

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.

(back to top)

Getting Started

Download

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.sh

(back to top)

Usage

sudo ./fix_wsl2_networking.sh [OPTIONS]

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

DNS providers

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
Google 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

Examples

Interactive mode (select DNS provider from a menu):

sudo ./fix_wsl2_networking.sh

Non-interactive mode with a specific provider:

sudo ./fix_wsl2_networking.sh --dns Cloudflare --yes

Preview changes without applying them:

sudo ./fix_wsl2_networking.sh --dry-run

Revert to automatic DNS configuration:

sudo ./fix_wsl2_networking.sh --undo

Once the script completes, restart WSL2 for the changes to take full effect:

wsl --shutdown

(back to top)

License

Licensed under the GNU Affero General Public License v3.0. See LICENSE for details.

(back to top)

Contact

ProtonMail LinkedIn

(back to top)