Run dnsmasq as a local DNS Server (Arch Linux)

Dnsmasq is a very handy and easy to install DNS / DHCP server implementation, that uses the local hosts file to determine hostname to IP mappings. I use a local DNS to be able to call my local machines by a memorable domain instead of their IP. Additionally dnsmasq works as an DNS Cache.

Installation of dnsmasq

You can install dnsmasq with the following commands (Arch Linux with installed sudo package):

Configuration of dnsmasq

After that you have to configure dnsmasq. This could be done directly in the dnsmasq configuration file (/etc/dnsmasq.conf) or in an own configuration file that is included in the dnsmasq.conf. I decided to use an own file:

Now the copied configuration file (lan.ask-sheldon.com) will be interpreted.

Now you have to set the following configurations ( $> nano /etc/dnsmasq.d/lan.ask-sheldon.com):

I deleted the unchanged lines for clarity and because I have preserve the original dnsmasq.conf.

Now localhost have to be set as the first DNS server for the local machine ( $> nano /etc/resolv.conf ):

The problem is, that different programs can override the resolv.conf, so that all changes are lost. That’s why we have to protect it. In my case the resolv.conf was a symlink to /run/systemd/resolve/resolv.conf.

So I removed the symlink and crated a static file /etc/resolv.conf where I put the lines above again.

Additionally its possible, that the DHCP deamon could change the/etc/resolv.conf . That’s why I added the following line to the end of my dhcpd.conf ( $> nano /etc/dhcpcd.conf):

To be 100% sure to have the total control about what’s happening with the resolv.conf, I added a write-protection to that file:

Add local domains

Local domains could be defined in the file /etc/hosts now. Afterwards dnsmasq can resolve them.

Example:

So you can call dnspi.ask-sheldon.com or just asgard  to call these hosts.

Register and start dnsmasq service

Now after we installed and configured the dnsmasq sever, we have to make sure it will launch after the machine has booted. Therefore we have to enable the service:

Then we have to start the service:

Use the built in DHCP of dnsmasq

I also wanted dnsmasq to manage my local IP-Address assignment. That’s why I use the build in DHCP feature of dnsmasq.

Therefore I added the following lines to my dnsmasq configuration (lan.ask-sheldon.com):

Attention: I had to switch off the DHCP of my Fritz!Box (Homenet -> Network Settings -> IP-Addresses) before it worked!!!

Testing the dnsmasq service

Afterwards I ran a few test, to check that everything works as expected:

  1. Check if the service is running:

    As you can see, you’ll get a log of all actions the dnsmasq did on startup.  At the end of the log it should look like this:
  2. Check if the dnsmasq service listens on the necessary ports of the machine:

    In the resulting list you should see the following lines:

    As you can see the DNS listens on port 53 and the DHCP on port 67 as they should.
  3. Use dig on the local machine to resolve a domain name:

    Result:

    As you can see the domain is resolved correctly.
  4. Test DHCP:
    For this I configured the router (Fritz!Box) in my network to not deal as a DHCP server and my computer to use DHCP to get IP, DNS, etc.
    After reboot the machine got an IP address in the defined range.
  5. Watch the actual leases of the DHCP:

    Result:

    Obviously my computer got a DHCP lease from the machine with the dnsmasq server on it.
  6.  Dig again. But from a remote machine and for a local domain defined in the /etc/hosts file mentioned above. You should get the IP of the target machine in the answer section.
  7. You could also just ping a local domain to see if it is resolved correctly.
  8. Check DNS-Cache:
    Call   $> dig ask-sheldon.com | grep "Query time" twice. This gives you the time it takes the DNS Server to resolve the given domain name. The second call should be significantly shorter.

Further information about dnsmasq under Arch Linux

2 thoughts on “Run dnsmasq as a local DNS Server (Arch Linux)

  1. I follow your instructions and I want to reset all to default as the first cuz my network not working, can u tell me how ?

Leave a Reply

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

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.