Lightweight DNS

This tutorial will cover setting up a simple DNS server under Ubuntu 7.10. We will be using MaraDNS because it is secure, lightweight, and easy to setup. This tutorial assumes that you own a domain and have some general knowledge of how to use Ubuntu (or your favorite GNU/Linux distribution instead). For the sake of the tutorial we will assume that the name server's IP is 10.0.0.1 and that we want to resolve mydomain.tld, ns1.mydomain.tld, smtp.mydomain.tld, and www.mydomain.tld, like this:

Host IP
mydomain.tld 10.0.0.2
ns1.mydomain.tld 10.0.0.1
smtp.mydomain.tld 10.0.0.3
www.mydomain.tld 10.0.0.2

Install MaraDNS

If you are using Ubuntu, installing MaraDNS is a matter of issuing a single command. Other distributions should be similar, but aren't covered here.

$ sudo apt-get install maradns
...

Configuring MaraDNS

Next, we need to tell MaraDNS a few things about our setup (such as the server's address) and set up a zone file for our domain. Edit /etc/maradns/mararc to change the sections below to look like this:

csv2 = {}
csv2["mydomain.tld."] = "db.mydomain.tld"

IPv4_bind_addresses = "127.0.0.1, 10.0.0.1"

This tells MaraDNS to use the csv2 format for our zone file format and tells it to look in the file db.mydomain.tld for information about the mydomain.tld domain. Notice the trailing period - this is important. It refers to the root of all the world's DNS servers and is mandatory. We will see a lot more of it in just a moment. Now create a file called /etc/maradns/db.mydomain.tld and make it look like this:

mydomain.tld. SOA mydomain.tld. hostmaster@mydomain.tld. /serial 7200 3600 604800 1800
mydomain.tld. NS ns1.mydomain.tld.
mydomain.tld. MX smtp.mydomain.tld.
mydamain.tld. FQDN4 10.0.0.2
ns1.mydomain.tld. A 10.0.0.1
smtp.mydomain.tld. A 10.0.0.3
www.mydomain.tld. CNAME mydomain.tld.

This creates a SOA record, generating a serial and setting the refresh, retry, expire, and minimum times in seconds for the domain (these are used by other servers to cache DNS queries). You can probably just use the values above for your own domains. Next we set ns1.mydomain.tld as the nameserver (NS record) for mydomain.tld, and smtp.mydomain.tld as the mail server (MX record). Below that we begin assigning addresses.

The FQDN4 record actually creates two records, one address record (A) and one reverse DNS record (PTR), so that 10.0.0.2 points to mydomain.tld as well as mydomain.tld pointing to 10.0.0.2. Since ns1.mydomain.tld and smtp.mydomain.tld have their own IPs we set them in A records, and since we want www.mydomain.tld to point at the same addess as mydomain.tld we use a canonical name record (CNAME) that points to the address of mydomain.tld.

Restart and Test the Server

Now restart and test the server to make sure everything is working. If you get similar output to what you see below, then congratulations, your DNS server setup. At this point you can point your domain at your name server!

$ sudo /etc/init.d/maradns force-reload
Stopping maradns: maradns.
Starting maradns: maradns.
$ dig @localhost mydomain.tld ANY
<<>> DiG 9.4.1-P1 <<>> @localhost mydomain.tld ANY
; (1 server found)
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 54637
;; flags: qr aa; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;mydomain.tld. IN ANY

;; ANSWER SECTION:
mydomain.tld. 86400 IN NS ns1.mydomain.tld.
mydomain.tld. 86400 IN SOA mydomain.tld. hostmaster.mydomain.tld. 150984708 7200 3600 604800 1800
mydomain.tld. 86400 IN MX 1 smtp.mydomain.tld.
mydomain.tld. 86400 IN A 10.0.0.2

;; Query time: 1 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sun Jan 27 19:30:40 2008
;; MSG SIZE rcvd: 151

References & Further Reading