Setting up internal name resolution in Windows and WSL (probably Linux too)

Tiny little quality of life hack
linux
WSL
DNS
Published

December 26, 2023

Introduction

Previously whenever I wanted to connect to a machine on my network I would create an entry in ~/.ssh/config that looked something like this

Host <host>
  HostName <host>.mydomain.ca

I might include key or default user information as well, but a lot of the time all I really needed was that. There were two reasons for doing that:

  • VS code looks in that file for remote development environments
  • It saves me typing ssh <host>.mydomain.ca and lets me just type ssh <host>

The first one this post doesn’t do anything about, but there’s a limited number of machines I need that for. The second doesn’t really seem like a big deal, but I’m lazy. In fact, I’m so lazy that I got annoyed with having to add a record in that file for every host I wanted to connect to, so I looked up how to configure DNS on my workstation to default to my domain if I didn’t put in a FQDN. That’s what this post is about. Read on to find out how.

On Windows

This is basically cribbed straight from chatGPT:

To configure your Windows machine so that it can automatically resolve hostnames with a specific domain (like appending mydomain.com to machine1 to form machine1.mydomain.com), you need to set up DNS suffix appending. Here’s how to do it:

  • Open Network Connections Settings:
    • Right-click the network icon in the system tray and select “Open Network & Internet settings”.
    • In the settings window, click on “Change adapter options”.
  • Modify Properties of Your Network Adapter:
    • Right-click on the network adapter you are using (it could be Ethernet or Wi-Fi) and select “Properties”. If prompted, provide administrator credentials or confirmation.
  • Edit TCP/IP Settings:
    • In the list of items used by the connection, select “Internet Protocol Version 4 (TCP/IPv4)” or “Internet Protocol Version 6 (TCP/IPv6)” depending on your network configuration.
    • Then click “Properties”.
    • In the next window, click on the “Advanced” button.
  • Set the DNS Suffix:
    • In the Advanced TCP/IP settings window, go to the “DNS” tab.
    • Check the option “Append these DNS suffixes (in order):”.
    • Click “Add” and enter your domain name (e.g., mydomain.com).
    • Click “Add” after typing each domain. You can add multiple domains if necessary.
  • Save Your Changes:
    • Click “OK” on all open windows to save your changes.
  • Test the Configuration:
    • Open Command Prompt or PowerShell.
    • Try to ping or connect to a device using only its hostname (e.g., ping machine1).
    • If everything is configured correctly, Windows will automatically append the DNS suffix and resolve machine1 to machine1.mydomain.com.
  • Note for Dynamic DNS Environments:
    • If you’re in a dynamic DNS environment, like a corporate network using Active Directory, these settings might be managed by your network administrator via DHCP or Group Policy.

Remember, these settings apply to the network adapter you configure. If you switch between different networks (e.g., from Wi-Fi to Ethernet), you may need to configure each adapter separately.

WSL

In theory this part should just work if Windows is configured correctly, but after some reboots and fiddling I couldn’t get it to go. So I did a manual override. There’s two parts to this

  • Set /etc/wsl.conf to not auto-generate /etc/resolv.conf

    • Put an entry in your /etc/wsl.conf that looks like the following:
    [network]
    generateResolvConf=false
    • Restart wsl (or just your whole computer) with wsl --restart from powershell
  • Update /etc/resolv.conf to look something like this:

    nameserver <your DNS IP>
    search <your.domain>

Conclusion

This is a small little quality of life hack. It doesn’t save a ton of time, but it also doesn’t take a ton of time to do, and at least for me while I’ve been experimenting with spinning up and tearing down a bunch of VMs it’s been worth the few minutes it took to figure out and set up.