Simple Intranet DNS Server in Linux

What is a DNS server?

In few words, a DNS server (or nameserver) helps you to resolve names into IP addresses.
For example, if you want to access another computer using a easy-to-remember name (e.g., "linuxbox") instead of its IP address (e.g., 192.168.0.10).

Why you may be interested in setup an Intranet DNS Server in your Linux box (if you have it)?

Normally, each time you add a Linux computer (or a subdomain for any computer) in your network, you may need to update each computer's hosts files (in windows: windows/system32/drivers/etc/hosts, in linux: /etc/hosts), in this way:

192.168.0.10    linuxbox1    sql.linuxbox1
192.168.0.11    linuxbox2    svn.linuxbox2   ftp.linuxbox2   

having an Intranet DNS server can help you to manage all names in only one computer and the rest will resolve it correctly (automatically).

It may look difficult but it is not.

Why another how-to-setup-a-DNS-server blog?

I looked on Internet and I didn't find any easy tutorial to setup it. Almost all links suggested to use "bind9", however I tried to setup it using a "4 minute tutorial" and at the end it was almost 4 hours and I didn't succeed with "bind9". It seems it requires a fully qualified domain name in order to work.
So, I finally succeed using "dnsmasq". However I didn't find a easy tutorial of it.
In less than 4 minutes (I think) you will be able to setup your DNS server with the following 8-simple-steps instructions (or 1 if you go with the default values):

Setup Instructions:

1) Install dnsmasq. (for ubuntu users, type: sudo apt-get install dnsmasq-base).
(dnsmasq is ready to work after install, but you may want to setup some of it options)

2) Create a config file: sudo vi /etc/dnsmasq.conf
(check vi quick reference if you don't know how to use it)

3) Paste the following text in the document:
(press "i" to inset text, and middle mouse button to paste the selected text)

# Configuration file for dnsmasq.
no-dhcp-interface=
server=/localnet/192.168.0.1
server=xxx.xxx.xxx.xxx

4) Change "192.168.0.1" to your router IP (in case you have) and "xxx.xxx.xxx.xxx" to your ISP DNS server (if needed). Usually, if you type "more /etc/resolv.conf" (or ipconfig /all in windows) you can see which are your actual nameservers. Change them accordingly to that information in order to be able to resolve the rest of the domain names.

The "no-dhcp-interface" option is to disable the DHCP capabilities that has dnsmasq. If you are interested in also assign the IPs to each computer you may be interested in that feature of dnsmasq. If so, please read the dnsmasq documentation for details.

5) Save the file and exit (press ESC key and then type ":wq" to save and quit).

6) Execute: sudo dnsmasq  (to run it as daemon)
Also you can run it with the "-d", to test it is working correctly.

7) Setup a script in /etc/init.d (or in your distribution startup script folder) to start dnsmasq every time you turn on your Linux machine.

8) Change your /etc/resolv.conf in Linux or your dns IP in Windows networking configuration to your new Linux DNS server IP address.

Final Notes:

By default dnsmasq will read your /etc/hosts file and will use it to resolve the IP addresses. However, if you have something like:

127.0.0.1  localhost
127.0.0.1  linuxbox1

Every time you type "linuxbox1" in a remote computer it will resolve to its own IP address.
For that reason, I suggest to add the following 2 lines to your /etc/dnsmasq.conf :

no-hosts
addn-hosts=/etc/hosts.dns

"no-hosts" is to prevent reading the /etc/hosts file. So, create the new "/etc/hosts.dns" file and add only intranet IP addresses in it:

192.168.0.1 linuxbox1

I strongly suggest to read the complete config sample file, which includes a better explanation of each option, in case you need other features I am not writing here.

For some reason, when I tested it from a windows machine, it couldn't resolve a name if it doesn't have a "." in it, for example: "linuxbox1" only.
In order to make it work, you may add a "." at the end, like this: "linuxbox1."
However, from other Linux machines it worked as expected (without the dot). I don't know the reason, and so far I have found nothing to solve it on Internet. However you can create custom "subdomains" that may reflect its intention (e.g., svn.linuxbox1 , www.linuxbox1 , ftp.linuxbox1 , etc.)

Dnsmasq runs in port 53, so I recommend you to adjust your Firewall to enable that port.

Very useful uses:

I personally find it very useful using dnsmasq daemon together with Apache httpd (or any other web server that supports virtual hosts). For example, in your httpd.conf, you can write something like this:
<VirtualHost *>     ServerName      www.alepe.com     ServerAlias     final.linuxbox1     DocumentRoot    /var/www/htdocs/ </VirtualHost>
<VirtualHost *>     ServerName      www2.alepe.com     ServerAlias     beta.linuxbox1     DocumentRoot    /var/www/beta </VirtualHost>
<VirtualHost *>     ServerName      sql.linuxbox1     DocumentRoot    /var/www/sql_admin </VirtualHost>
 <VirtualHost *>     ProxyPreserveHost On     ProxyPass       /     http://192.168.0.2/     ProxyPassReverse  /     http://192.168.0.2/     ServerName      win2.linuxbox1 </VirtualHost>

So, you can add new services easily, adding new "subdomains" to the hosts file and to your httpd.conf file.

If dnsmasq is not what you wanted or this tutorial didn't work for you, check TinyDNS (djbdns), Bind, or Samba NBNS.

I hope you find it useful. Thank you for reading it.

guest@alepe.com:~/Blog/$ ls

drwxr-xr-x lepe  4.0K 2019-04-30 04:07:16 .
drwxr-xr-x lepe  4.0K 2019-04-30 04:07:28 ..
-rw-r--r-- lepe 2.8KB 2019-04-30 04:07:16 cache_management_in_php.html
-rw-r--r-- lepe 5.3KB 2019-04-30 04:07:16 hide_your_email_from_spambots.html
-rw-r--r-- lepe 3.3KB 2019-04-30 04:07:16 my_linux.html
-rw-r--r-- lepe 8.3KB 2019-04-30 04:07:16 simple_intranet_dns_server_in_linux.html

guest@alepe.com:~/Blog/$