Skip to content

Instantly share code, notes, and snippets.

@rrotter
Created November 16, 2018 23:23
Show Gist options
  • Save rrotter/d4a27002eb92989cc680258a72494e36 to your computer and use it in GitHub Desktop.
Save rrotter/d4a27002eb92989cc680258a72494e36 to your computer and use it in GitHub Desktop.
Script to write resolv.conf in WSL
#!/bin/bash
# Write out resolv.conf based on Windows DNS settings.
# I have attemped to replicate the stock WSL behavior, except where noted.
# To run this w/ password-less sudo, add the following to your shell initialization
# If you don't understand what this does, look it up, DO NOT just trust me that it's okay
# %sudo ALL=(ALL:ALL) NOPASSWD: /usr/local/sbin/resolv-fix
# Set powershell path, you may have to change this for your environment
PS="/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe"
# Stock header so WE WILL get overwritten on network changes
# and a warning about that
HEAD="# This file was automatically generated by WSL. To stop automatic generation of this file, remove this line.
# The above is a lie, but we're keeping that line because broken search domains are better than broken DNS"
# get the first three DNS entries we find, favor IPv4
IP4=`${PS} "Get-DnsClientServerAddress -AddressFamily IPv4 | select -ExpandProperty ServerAddresses" | awk '!a[$0]++ { print "nameserver " $0 }' | tr -d '\r'`
IP6=`${PS} "Get-DnsClientServerAddress -AddressFamily IPv6 | select -ExpandProperty ServerAddresses" | awk '!a[$0]++ { print "nameserver " $0 }' | tr -d '\r'`
DNS=`echo -e "${IP4}\n${IP6}" | head -3`
# Search all THREE(!) places Windows buries Search Domains (DNS Suffix in Windows-speak)
A=`${PS} "Get-DnsClient | select -ExpandProperty ConnectionSpecificSuffix" | grep "\S" | tr -d '\r'`
B=`${PS} "Get-DnsClient | select -ExpandProperty ConnectionSpecificSuffixSearchList" | tr -d '\r' `
# this is the thing WSL appears to skip (IN CASE ANYONE FROM MS IS LISTENING, JUST ADD THIS LINE TO WSL SOMEHWERE, PLEASE!)
C=`${PS} "Get-DnsClientGlobalSetting | select -ExpandProperty suffixsearchlist" | tr -d '\r'`
# assemble them w/o superflous newlines
# No attempt is made to validate that this fits in the max length of 256 chars, because I don't have that many search domains.
SearchDomains=`echo "$A $B $C" | tr '\n' ' '`
# Print the whole thing into resolv.conf
echo "${HEAD}
${DNS}
search $SearchDomains" > /run/resolvconf/resolv.conf
@rrotter
Copy link
Author

rrotter commented Nov 16, 2018

I run this w/ sudo from my shell initialization. There are surely better ways to do this, but this is working well enough for now. Much better than my old script that just added a hard coded list of search domains, this picks up the search domains I've already set in Windows and generates resolv.conf based on that. This script will work even if your resolv.conf contents are trashed, because it only gets data from PowerShell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment