Set Custom DNS Servers for Specific Wi-Fi network on MacOS

Currently I am running AgGuard to block ads when I am connected to my local network via Wi-Fi. This is a great adblocking tool – it works basically the same as Pi-Hole (which I tried before using AdGuard and its one of the best Raspberry Pi use cases). Each DNS request is sent to AdGuard and requests from advertisement networks are blocked. This requires me to manually set my DNS server for each device.

On my MacBook Pro, configuring the DNS server is as follows:

  • Open Wi-Fi menu
  • Open Network Preferences and select Advanced
  • In the DNS tab, set the DNS server(s) which will be the internal IP address of your PI-Hole or AdGuard instance

The problem however I faced on MacOS was the following: when I set a custom DNS server (which pointed to my AdGuard service running on a Raspberry Pi) for my home Wi-FI network, the same DNS server would be used when connecting to other Wi-Fi networks. This is a problem because for example the McDonald’s Free Wi-Fi network will not recognise the internal IP address of my Raspberry Pi. The result is: ‘no internet’, no webpage will load when on a non-home Wi-FI network.

The solution could be to always redefine the DNS servers when switching between home and non-home networks. If you are using your device (for example: laptop or tablet) almost exclusively at home, this may be a sufficiently good solution for you. Yet, you may be looking for a more elegant solution that does not require to open the settings each time you switch to another network.

Using Locations on MacOS

The solution is: define a location. This location will allow you to define settings for different locations you will be at.

In the Network Preference section on your Macbook running MacOS, you can define a Location.

Setting Locations via Network Preference

You can remove existing locations, and remove locations afterwards as well. In my case, it was sufficient to create a Home location. When the Home location is selected in the Network Preferences, I point the DNS server to my Raspberry Pi. While the “Automatic” location will retrieve the DNS servers as they are suggested by the network I am connected to at that moment.

Switching between Locations

I have yet to figure out what the behaviour is of MacOS when setting the location to ‘Automatic’. In any case, the easiest way to manually define the current location you are at (and to trigger the custom DNS server for your home location) is to go via the Apple logo on the top left of the screen and select to switch your location. By switching the location, the DNS will switch to your internal IP address (or the one(s) you have defined).

Switch between the locations you have earlier defined via the Apple logo on the top left.

This is it, you can now use your Pi-Hole or AdGuard and route the DNS requests to your internal IP address. At the other hand, you will still be able to surf the web when you are not at home.

This article should solve typically the questions that I receive from time to time:

  • How to set a specific DNS server for one Wi-FI network
  • Auto switch to DNS when at home
  • macOS set custom DNS server for Pi-Hole
  • AdGuard DNS on MacOS only on home Wi-Fi

Let me know if this worked for you as well!

 

Author Bio

Thank you for your interest in my blog! On this miniblog, I write mostly short (technical) blog posts that might interest other people. Read more about me or feel free to contact me.

 

4 thoughts on “Set Custom DNS Servers for Specific Wi-Fi network on MacOS

  1. Thank you Thomas, I was searching exactly how to fix the exact same problem on my MacBook Air and my Pi hole at home, worked great!

Leave a Reply

Your email address will not be published. Required fields are marked *