How to Fix Internet Connection Problems in WSL 2 Ubuntu Windows 11

Changing WSL 2 Network Settings Globally

This allows you to change WSL 2 network settings globally for all Linux distributions you run on WSL 2. You can set several options, such as:

  • generateHosts : specifies whether /etc/hosts files will be created automatically by WSL 2 or not
  • generateResolvConf : specifies whether the /etc/resolv.conf file will be created automatically by WSL 2 or not
  • network: specifies specific network settings for the WSL 2 virtual network interface, such as IP address, default gateway, and DNS server

To change WSL 2 network settings globally, you need to create or edit the /etc/wsl.conf configuration file inside the Linux distribution you are using. This file must have the following format:

[network]
generateHosts = <true|false>
generateResolvConf = <true|false>
address = <IP address>
gateway = <IP address>
nameserver = <IP address>

You can fill in the above values according to your needs. For example, if you want to assign a static IP address to a WSL 2 virtual network interface, and use the same default gateway and DNS server as your Windows 11, then your /etc/wsl.conf configuration file could look like this:

[network]
generateHosts = false
generateResolvConf = false
address = 192.168.1.100
gateway = 192.168.1.1
nameserver = 8.8.8.8

After creating or editing the /etc/wsl.conf configuration file, you need to restart your Linux distribution for the changes to take effect. You can do this by running the following command in PowerShell as an administrator:

wsl --shutdown
wsl -d <distro name>

The first command will shut down all Linux distributions running on WSL 2, and the second command will rerun the Linux distribution you want. Replace <distro name> with the name of the Linux distribution you’re using, for example, Ubuntu.

After restarting your Linux distribution, try checking again if your internet connection is normal in WSL 2. If you’re still having trouble, continue to the next method.

Changing WSL 2 network settings specifically

This allows you to change WSL 2 network settings specifically for each Linux distribution you run on WSL 2. You can set the DNS server settings used by WSL 2 to resolve domain names to IP addresses.

To change WSL 2 network settings specifically, you need to create or edit the /etc/resolv.conf configuration file inside the Linux distribution you are using. This file must have the following format:

nameserver <IP address>

You can fill in the <IP address> value with the IP address of the DNS server you want. For example, if you want to use Google’s DNS servers, then your /etc/resolv.conf configuration file could look like this:

nameserver 8.8.8.8
nameserver 8.8.4.4

You can add more than one line nameserver if you want to use more than one DNS server.

After creating or editing the /etc/resolv.conf configuration file, you need to ensure that this file will not be overwritten by WSL 2 automatically. You can do this by running the following command inside your Linux distribution:

sudo chattr +i /etc/resolv.conf

This command will make the /etc/resolv.conf file unchangeable or deleted by anyone, including WSL 2.

After changing the WSL 2 network settings specifically, try checking again if your internet connection is normal on WSL 2. If you’re still having trouble, move on to the next solution.

Latest Articles