How to set up your own DNS server

Learn how to set up your personal DNS server on Linux and macOS and use it remotely over Meshnet.

Introduction

Once the internet connection is set up in your home, it is usually configured to use the internet service provider’s DNS addresses. Using such DNS can lead to a number of inconveniences: longer website loading times, unwanted URL blocking, and query logging. All of these factors can make the browsing experience feel like a chore. Thankfully, there are various ways to overcome these hindrances, such as configuring a personal DNS server. A tool that allows you to do exactly this is dnsmasq.

By using the free dnsmasq software, your device can function as a DNS forwarder and DHCP server. It has an integrated DNS caching feature. This means that any website that is saved in the cache will open nearly instantly. Such behavior leads to a much smoother and more enjoyable browsing experience. Additionally, you are in control of your DNS records, as you can monitor them directly on your device or disable logging altogether.

The drawback of dnsmasq is that it is designed to work only on the local network. However, with the addition of Meshnet, you can use your dnsmasq configuration no matter how far away from home you are.

In this article, you will find instructions on how to set up a personal DNS server using dnsmasq on macOS and Linux.

Note

Changing DNS addresses while using cellular data and keeping the Meshnet connection active is not possible. If you are using a mobile device with a cellular connection, you will need to use dnsmasq with a VPN connection.

Before you begin

Because dnsmasq uses port 53 for its functionality, it is important to ensure that this port is available on your system before configuring dnsmasq. To do so, open Terminal and execute the following command:

sudo lsof -i -n -P | grep :53

If port 53 is being used, in the output, you will see the name of the process that is using it.

In case the port is occupied, you will need to free it up by killing the other process which is using port 53 or changing its port to a different one.

Install Homebrew

To install dnsmasq, you can use the Homebrew package manager. Install Homebrew by taking these steps:

  1. Open Terminal.

  2. Enter the following command:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

    For additional information about the installation process, refer to Homebrew documentation.

  3. Follow the on-screen instructions to finish the installation.

Find the Meshnet network interface

Communication between devices in Meshnet is done via the Meshnet network interface. The name of the interface can differ for each device. To find the correct name on your device:

  1. Open Terminal.

  2. Run the following command:

    ifconfig

  3. Locate the interface with your Meshnet IP and note its name. For example, utun3.

    Meshnet network interface

Install dnsmasq

To start configuring your DNS server, you will need to install dnsmasq on your machine.

  1. Open Terminal.

  2. Install dnsmasq by running the following command:

    brew install dnsmasq

The dnsmasq application will be installed.

Configure dnsmasq

Dnsmasq settings are controlled via its configuration file called dnsmasq.conf.

  1. Open the dnsmasq.conf file using the Nano text editor by running this command in Terminal: For Intel Macs:

    nano /usr/local/etc/dnsmasq.conf

    For Apple silicon Macs:

    nano /opt/homebrew/etc/dnsmasq.conf

  2. Using the Control ⌃ + W keyboard shortcut, locate and uncomment the following lines by removing the hash (#) symbol at the start: • domain-needed • bogus-priv • no-resolv

    domain-needed and bogus-priv lines

    no-resolv line

  3. Find the cache-size line, uncomment it, and change the value to 1000.

    cache-size line

  4. Locate the interface line, uncomment it, and append the name of the network interface from the Find the Meshnet network interface section after the equals (=) sign.

    interface line with the Meshnet network interface name

  5. Find the #server=/localnet/192.168.0.1 line and replace it with the following two lines:

    server=1.1.1.1
    server=8.8.8.8

    Edited server lines

    Instead of the 1.1.1.1 and 8.8.8.8 DNS addresses, you can use your preferred DNS provider for the upstream DNS.

  6. Press Control ⌃ + X, Y, and Return to save changes and exit.

Start the dnsmasq process as the root user via the following command:

sudo brew services start dnsmasq

Note

Detailed information about the altered parameters can be found in the Options section of the dnsmasq manual page.

Enable query logging (optional)

You can enable query logging in the dnsmasq.conf file to monitor for any failed DNS resolutions or inconsistencies in website loading.

  1. Open Terminal and run the following command: For Intel Macs:

    nano /usr/local/etc/dnsmasq.conf

    For Apple silicon Macs:

    nano /opt/homebrew/etc/dnsmasq.conf

  2. Locate the log-queries line and uncomment it by removing the hash (#) symbol at the start.

  3. Append a new log-facility=/usr/local/var/log/dnsmasq.log line below log-queries.

    Lines for query logging

  4. Press Control + X, Y, and Return to save changes and exit.

  5. Restart the dnsmasq process by running:

    sudo brew services restart dnsmasq

All of the DNS queries will now be logged in the /usr/local/var/log/dnsmasq.log file.

Test dnsmasq

To test if dnsmasq was configured properly and is working, do the following:

  1. Open Terminal.

  2. Check the status of the dnsmasq service via this command:

    sudo brew services list

    Status of the dnsmasq service

  3. Check if the domain name resolution is working properly by using dig:

    dig nordvpn.com @localhost

    Output of the 'dig' command

If you receive an answer to the dig command, it indicates that your DNS server is working properly and ready for use.

Use your dnsmasq DNS over Meshnet

With dnsmasq fully set up, you can start using it on your devices. For usage via Meshnet, you must set the server device’s Meshnet IP address as the DNS server on each client device.

Without a VPN connection

Windows 11

  1. Right-click Start and select Network connections.

  2. Choose the network type your device is using (Wi-Fi or Ethernet).

  3. Find the DNS server assignment option and click Edit.

  4. Choose Manual from the dropdown menu and enable IPv4.

  5. Fill in the DNS fields.

    • Under Preferred DNS, enter the Meshnet IP of the dnsmasq device.

    • Under Alternate DNS, enter the DNS address of another provider.

  6. Click Save.

Windows 10

  1. Right-click Start and select Network connections.

  2. Select Change adapter options.

  3. Right-click your current network adapter (Wi-Fi or Ethernet) and choose Properties.

  4. In the new window, select Internet Protocol Version 4 (TCP/IPv4) and click Properties.

  5. Select Use the following DNS server addresses and fill in the fields underneath.

    • Preferred DNS server: the Meshnet IP of the dnsmasq device.

    • Alternate DNS server: the DNS address of another provider.

    dnsmasq and alternate DNS addresses entered.

  6. Click OK twice to apply the changes.

With a VPN connection

Meshnet allows you to use your custom, self-hosted DNS server alongside a VPN connection via the NordVPN app. Follow these steps to use your dnsmasq DNS when connected to a VPN server:

  1. Open the NordVPN app.

  2. Navigate to Devices in Meshnet and copy the Meshnet IP address of your dnsmasq device.

  3. In the lower-left corner, click Settings .

  4. Select the Connection section.

  5. Enable the Use custom DNS toggle and click the dropdown arrow.

  6. In the first field, paste the copied Meshnet IP address.

    Entering the Meshnet IP address in the Use custom DNS section in the NordVPN app settings.

  7. On the right, click the Save checkmark.

Now, when you establish a VPN connection to a NordVPN server, the DNS address that you specified will be used for the connection.

Note

In Threat protection pro > Web protection, ensure that the DNS filtering toggle is turned off. Otherwise, the Threat Protection feature will overwrite the custom DNS change.

Tip

If you're looking for alternate DNS addresses, here are some of the most popular free DNS providers:

  • Google — 8.8.8.8 and 8.8.4.4

  • Cloudflare — 1.1.1.1 and 1.0.0.1

  • AdGuard — 94.140.14.14 and 94.140.15.15

  • Quad9 — 9.9.9.9 and 149.112.112.112

  • OpenDNS — 208.67.222.222 and 208.67.220.220

You can also use the same upstream DNS addresses from the dnsmasq.conf file.

With the DNS changes applied, you have finished the setup process. Your personal DNS server can now be used remotely, with the help of Meshnet.

Last updated

© 2024 Nord Security. All Rights Reserved.