Skip to main content
Per JHixson's comment, the startup script should include `sudo`. Also added a comment to show this script's been applied (to get over the 6 char edit limit, and so this doesn't get forgotten)
Source Link
Giacomo1968
  • 56.1k
  • 23
  • 167
  • 214
  1. Re-enable auto generation of resolv.conf (if disabled)

    by commented the disable with #

    sudo nano /etc/wsl.conf
    
    #[network]
    #generateResolvConf = false
    
  2. Create the script

    sudo nano /bin/vpn-dns.sh
    
    #!/bin/bash
    
    echo "Getting current DNS servers, this takes a couple of seconds"
    
    /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '
    $ErrorActionPreference="SilentlyContinue"
    Get-NetAdapter -InterfaceDescription "Cisco AnyConnect*" | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
    Get-NetAdapter | ?{-not ($_.InterfaceDescription -like "Cisco AnyConnect*") } | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
    ' | \
            awk 'BEGIN { print "# Generated by vpn fix func on", strftime("%c"); print } { print "nameserver", $1 }' | \
            tr -d '\r' > /etc/resolv.conf
    clear
    echo "VPN networking fix applied per: https://superuser.com/questions/1630487/no-internet-connection-ubuntu-wsl-while-vpn"
    
  3. Make it executable/run as sudo

    sudo chmod +x /bin/vpn-dns.sh
    echo "$(whoami) ALL=(ALL) NOPASSWD: /bin/vpn-dns.sh" | sudo tee /etc/sudoers.d/010-$(whoami)-vpn-dns
    
  4. Make it run on wsl startup

    echo "sudo /bin/vpn-dns.sh" | sudo tee /etc/profile.d/vpn-dns.sh
    
  1. Re-enable auto generation of resolv.conf (if disabled)

    by commented the disable with #

    sudo nano /etc/wsl.conf
    
    #[network]
    #generateResolvConf = false
    
  2. Create the script

    sudo nano /bin/vpn-dns.sh
    
    #!/bin/bash
    
    echo "Getting current DNS servers, this takes a couple of seconds"
    
    /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '
    $ErrorActionPreference="SilentlyContinue"
    Get-NetAdapter -InterfaceDescription "Cisco AnyConnect*" | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
    Get-NetAdapter | ?{-not ($_.InterfaceDescription -like "Cisco AnyConnect*") } | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
    ' | \
            awk 'BEGIN { print "# Generated by vpn fix func on", strftime("%c"); print } { print "nameserver", $1 }' | \
            tr -d '\r' > /etc/resolv.conf
    clear
    echo "VPN networking fix applied per: https://superuser.com/questions/1630487/no-internet-connection-ubuntu-wsl-while-vpn"
    
  3. Make it executable/run as sudo

    sudo chmod +x /bin/vpn-dns.sh
    echo "$(whoami) ALL=(ALL) NOPASSWD: /bin/vpn-dns.sh" | sudo tee /etc/sudoers.d/010-$(whoami)-vpn-dns
    
  4. Make it run on wsl startup

    echo "sudo /bin/vpn-dns.sh" | sudo tee /etc/profile.d/vpn-dns.sh
    
  1. Re-enable auto generation of resolv.conf (if disabled)

    by commented the disable with #

    sudo nano /etc/wsl.conf
    
    #[network]
    #generateResolvConf = false
    
  2. Create the script

    sudo nano /bin/vpn-dns.sh
    
    #!/bin/bash
    
    echo "Getting current DNS servers, this takes a couple of seconds"
    
    /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '
    $ErrorActionPreference="SilentlyContinue"
    Get-NetAdapter -InterfaceDescription "Cisco AnyConnect*" | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
    Get-NetAdapter | ?{-not ($_.InterfaceDescription -like "Cisco AnyConnect*") } | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
    ' | \
            awk 'BEGIN { print "# Generated by vpn fix func on", strftime("%c"); print } { print "nameserver", $1 }' | \
            tr -d '\r' > /etc/resolv.conf
    clear
    
  3. Make it executable/run as sudo

    sudo chmod +x /bin/vpn-dns.sh
    echo "$(whoami) ALL=(ALL) NOPASSWD: /bin/vpn-dns.sh" | sudo tee /etc/sudoers.d/010-$(whoami)-vpn-dns
    
  4. Make it run on wsl startup

    echo "sudo /bin/vpn-dns.sh" | sudo tee /etc/profile.d/vpn-dns.sh
    
Per JHixson's comment, the startup script should include `sudo`. Also added a comment to show this script's been applied (to get over the 6 char edit limit, and so this doesn't get forgotten)
Source Link
  1. Re-enable auto generation of resolv.conf (if disabled)

    by commented the disable with #

    sudo nano /etc/wsl.conf
    
    #[network]
    #generateResolvConf = false
    
  2. Create the script

    sudo nano /bin/vpn-dns.sh
    
    #!/bin/bash
    
    echo "Getting current DNS servers, this takes a couple of seconds"
    
    /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '
    $ErrorActionPreference="SilentlyContinue"
    Get-NetAdapter -InterfaceDescription "Cisco AnyConnect*" | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
    Get-NetAdapter | ?{-not ($_.InterfaceDescription -like "Cisco AnyConnect*") } | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
    ' | \
            awk 'BEGIN { print "# Generated by vpn fix func on", strftime("%c"); print } { print "nameserver", $1 }' | \
            tr -d '\r' > /etc/resolv.conf
    clear
    echo "VPN networking fix applied per: https://superuser.com/questions/1630487/no-internet-connection-ubuntu-wsl-while-vpn"
    
  3. Make it executable/run as sudo

    sudo chmod +x /bin/vpn-dns.sh
    echo "$(whoami) ALL=(ALL) NOPASSWD: /bin/vpn-dns.sh" | sudo tee /etc/sudoers.d/010-$(whoami)-vpn-dns
    
  4. Make it run on wsl startup

    echo ""sudo /bin/vpn-dns.sh" | sudo tee /etc/profile.d/vpn-dns.sh
    
  1. Re-enable auto generation of resolv.conf (if disabled)

    by commented the disable with #

    sudo nano /etc/wsl.conf
    
    #[network]
    #generateResolvConf = false
    
  2. Create the script

    sudo nano /bin/vpn-dns.sh
    
    #!/bin/bash
    
    echo "Getting current DNS servers, this takes a couple of seconds"
    
    /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '
    $ErrorActionPreference="SilentlyContinue"
    Get-NetAdapter -InterfaceDescription "Cisco AnyConnect*" | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
    Get-NetAdapter | ?{-not ($_.InterfaceDescription -like "Cisco AnyConnect*") } | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
    ' | \
            awk 'BEGIN { print "# Generated by vpn fix func on", strftime("%c"); print } { print "nameserver", $1 }' | \
            tr -d '\r' > /etc/resolv.conf
    clear
    
  3. Make it executable/run as sudo

    sudo chmod +x /bin/vpn-dns.sh
    echo "$(whoami) ALL=(ALL) NOPASSWD: /bin/vpn-dns.sh" | sudo tee /etc/sudoers.d/010-$(whoami)-vpn-dns
    
  4. Make it run on wsl startup

    echo "/bin/vpn-dns.sh" | sudo tee /etc/profile.d/vpn-dns.sh
    
  1. Re-enable auto generation of resolv.conf (if disabled)

    by commented the disable with #

    sudo nano /etc/wsl.conf
    
    #[network]
    #generateResolvConf = false
    
  2. Create the script

    sudo nano /bin/vpn-dns.sh
    
    #!/bin/bash
    
    echo "Getting current DNS servers, this takes a couple of seconds"
    
    /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '
    $ErrorActionPreference="SilentlyContinue"
    Get-NetAdapter -InterfaceDescription "Cisco AnyConnect*" | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
    Get-NetAdapter | ?{-not ($_.InterfaceDescription -like "Cisco AnyConnect*") } | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
    ' | \
            awk 'BEGIN { print "# Generated by vpn fix func on", strftime("%c"); print } { print "nameserver", $1 }' | \
            tr -d '\r' > /etc/resolv.conf
    clear
    echo "VPN networking fix applied per: https://superuser.com/questions/1630487/no-internet-connection-ubuntu-wsl-while-vpn"
    
  3. Make it executable/run as sudo

    sudo chmod +x /bin/vpn-dns.sh
    echo "$(whoami) ALL=(ALL) NOPASSWD: /bin/vpn-dns.sh" | sudo tee /etc/sudoers.d/010-$(whoami)-vpn-dns
    
  4. Make it run on wsl startup

    echo "sudo /bin/vpn-dns.sh" | sudo tee /etc/profile.d/vpn-dns.sh
    
deleted 64 characters in body
Source Link
Kraego
  • 531
  • 4
  • 6

WSL2 - VPN Fix:

The new workaround is exclusive for Cisco AnyConnect the old version should work for any VPN.

  1. Reanable auto generation of resolv.conf (if disabled) by commented the disable with #

    Re-enable auto generation of resolv.conf (if disabled)

    by commented the disable with #

    sudo nano /etc/wsl.conf
    
    #[network]
    #generateResolvConf = false
    
  2. Create the script

    sudo nano /bin/vpn-dns.sh
    
    #!/bin/bash
    
    echo "Getting current DNS servers, this takes a couple of seconds"
    
    /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '
    $ErrorActionPreference="SilentlyContinue"
    Get-NetAdapter -InterfaceDescription "Cisco AnyConnect*" | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
    Get-NetAdapter | ?{-not ($_.InterfaceDescription -like "Cisco AnyConnect*") } | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
    ' | \
            awk 'BEGIN { print "# Generated by vpn fix func on", strftime("%c"); print } { print "nameserver", $1 }' | \
            tr -d '\r' > /etc/resolv.conf
    clear
    
  3. Make it executable/run as sudo

    sudo chmod +x /bin/vpn-dns.sh
    echo "$(whoami) ALL=(ALL) NOPASSWD: /bin/vpn-dns.sh" | sudo tee /etc/sudoers.d/010-$(whoami)-vpn-dns
    
  4. Make it run on wsl startup

    echo "/bin/vpn-dns.sh" | sudo tee /etc/profile.d/vpn-dns.sh
    
sudo nano /etc/wsl.conf
#[network]
#generateResolvConf = false
  1. Create the script:
sudo nano /bin/vpn-dns.sh
#!/bin/bash

echo "Getting current DNS servers, this takes a couple of seconds"

/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '
$ErrorActionPreference="SilentlyContinue"
Get-NetAdapter -InterfaceDescription "Cisco AnyConnect*" | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
Get-NetAdapter | ?{-not ($_.InterfaceDescription -like "Cisco AnyConnect*") } | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
' | \
        awk 'BEGIN { print "# Generated by vpn fix func on", strftime("%c"); print } { print "nameserver", $1 }' | \
        tr -d '\r' > /etc/resolv.conf
clear

3 Make it executable/run as sudo:

sudo chmod +x /bin/vpn-dns.sh
echo "$(whoami) ALL=(ALL) NOPASSWD: /bin/vpn-dns.sh" | sudo tee /etc/sudoers.d/010-$(whoami)-vpn-dns
  • Make it run on wsl startup
echo "/bin/vpn-dns.sh" | sudo tee /etc/profile.d/vpn-dns.sh

You couldcan also run it manually: sudo /bin/vpn-dns.sh

WSL2 - VPN Fix:

The new workaround is exclusive for Cisco AnyConnect the old version should work for any VPN.

  1. Reanable auto generation of resolv.conf (if disabled) by commented the disable with #
sudo nano /etc/wsl.conf
#[network]
#generateResolvConf = false
  1. Create the script:
sudo nano /bin/vpn-dns.sh
#!/bin/bash

echo "Getting current DNS servers, this takes a couple of seconds"

/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '
$ErrorActionPreference="SilentlyContinue"
Get-NetAdapter -InterfaceDescription "Cisco AnyConnect*" | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
Get-NetAdapter | ?{-not ($_.InterfaceDescription -like "Cisco AnyConnect*") } | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
' | \
        awk 'BEGIN { print "# Generated by vpn fix func on", strftime("%c"); print } { print "nameserver", $1 }' | \
        tr -d '\r' > /etc/resolv.conf
clear

3 Make it executable/run as sudo:

sudo chmod +x /bin/vpn-dns.sh
echo "$(whoami) ALL=(ALL) NOPASSWD: /bin/vpn-dns.sh" | sudo tee /etc/sudoers.d/010-$(whoami)-vpn-dns
  • Make it run on wsl startup
echo "/bin/vpn-dns.sh" | sudo tee /etc/profile.d/vpn-dns.sh

You could also run it manually: sudo /bin/vpn-dns.sh

  1. Re-enable auto generation of resolv.conf (if disabled)

    by commented the disable with #

    sudo nano /etc/wsl.conf
    
    #[network]
    #generateResolvConf = false
    
  2. Create the script

    sudo nano /bin/vpn-dns.sh
    
    #!/bin/bash
    
    echo "Getting current DNS servers, this takes a couple of seconds"
    
    /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '
    $ErrorActionPreference="SilentlyContinue"
    Get-NetAdapter -InterfaceDescription "Cisco AnyConnect*" | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
    Get-NetAdapter | ?{-not ($_.InterfaceDescription -like "Cisco AnyConnect*") } | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
    ' | \
            awk 'BEGIN { print "# Generated by vpn fix func on", strftime("%c"); print } { print "nameserver", $1 }' | \
            tr -d '\r' > /etc/resolv.conf
    clear
    
  3. Make it executable/run as sudo

    sudo chmod +x /bin/vpn-dns.sh
    echo "$(whoami) ALL=(ALL) NOPASSWD: /bin/vpn-dns.sh" | sudo tee /etc/sudoers.d/010-$(whoami)-vpn-dns
    
  4. Make it run on wsl startup

    echo "/bin/vpn-dns.sh" | sudo tee /etc/profile.d/vpn-dns.sh
    

You can also run it manually: sudo /bin/vpn-dns.sh

added 1641 characters in body
Source Link
Kraego
  • 531
  • 4
  • 6
Loading
added 354 characters in body
Source Link
Kraego
  • 531
  • 4
  • 6
Loading
added 737 characters in body
Source Link
Kraego
  • 531
  • 4
  • 6
Loading
added 74 characters in body
Source Link
Kraego
  • 531
  • 4
  • 6
Loading
Source Link
Kraego
  • 531
  • 4
  • 6
Loading