Install and configure bind9 DNS server

Bind9 is the DNS server used to resolve domain names to IP addresses. Since it’s the most popular, I’ll be explaining here how you can set it on your own linux box. I’m running my Debian Squeze GNU/Linux, but the configuration stays similar on most distros. Let’s start with the installation. Bind9 is present in my debian repository so I’m just going to use apt to install it:

# apt-get install bind9 dnsutils

After the installation is complete we can start configuring its environment since we will run it chrooted for security purposes. First we need to tell the server to start in its own environment with the following modifications. First open the file /etc/default/bind9 with vi or nano or whatever you want, as root, and modify the line:

OPTIONS="-u bind"

to look like this:

OPTIONS="-u bind -t /var/lib/named"

After that we can start setting up the environment. Execute the following command sequence to create additional directories and files:

# mkdir -p /var/lib/named/etc
# mkdir /var/lib/named/dev
# mkdir -p /var/lib/named/var/cache/bind
# mkdir -p /var/lib/named/var/run/bind/run
# mv /etc/bind /var/lib/named/etc
# ln -s /var/lib/named/etc/bind /etc/bind
# mknod /var/lib/named/dev/null c 1 3
# mknod /var/lib/named/dev/random c 1 8
# chmod 666 /var/lib/named/dev/*
# chown -R bind:bind /var/lib/named/var/*
# chown -R bind:bind /var/lib/named/etc/bind

We created environment in /var/lib/named and as far bind9 is concerned that is a root directory.
Restart bind9. I use init script which came with the installation:

# /etc/init.d/bind9 restart

Now you can test it by adding server ip in your network configuration and just go to some site. Try stopping your server, then try to open some website. If it cannot open, run your server, then try again. It should be working now. If it’s not opening the page there is some error in your configuration, and you should check it.
Now we can add some domains for it to resolve.
Create directory /etc/bind/zones/master

# mkdir -p /etc/bind/zones/master

Create the zone file:

nano /etc/bind/zones/master/home.zone

You can name your zone file whatever you like, but it’s best to use your domain as the filename. Add the following in the file:

;
; BIND data file for home
;
$TTL    604800
@       IN      SOA     ns.home. info.home. (
                                     1         ; Serial
                                  7200         ; Refresh
                                   120         ; Retry
                               2419200         ; Expire
                                604800)        ; Default TTL
;
@       IN      NS      ns.example.com.
home.    IN      MX      10      mail.home.
home.    IN      A       192.168.1.1
ns                     IN      A       192.168.1.1
www                    IN      CNAME   home.
mail                   IN      A       192.168.1.1
ftp                    IN      CNAME   home.
home.           IN     TXT     "v=spf1 ip4:192.168.1.1 a mx ~all"
mail                   IN      TXT     "v=spf1 a -all"

Instead of home you can use whatever you like, for example, my.home or home.lan, but try not to create ambiguous name. For example, if you choose wordpress.com for your domain it won’t go to wordpress as it will resolve to your selected server. Now you can access your http server by directing your browser to http://www.home.
After adding your zone file you need to direct the server to use it. Open the file /etc/bind/named.conf.local and add this:

zone "home" {
       type master;
       file "/etc/bind/zones/master/home.zone";
};

Restart your server and start using it.
To test it start pinging it:

$ ping ns.home

Do the nslookup which should return something like this:

$ nslookup www.wordpress.com

nslookup www.wordpress.com
Server:		192.168.1.1
Address:	192.168.1.1#53

Non-authoritative answer:
www.wordpress.com	canonical name = lb.wordpress.com.
Name:	lb.wordpress.com
Address: 74.200.244.59
Name:	lb.wordpress.com
Address: 76.74.254.120
Name:	lb.wordpress.com
Address: 76.74.254.123
Name:	lb.wordpress.com
Address: 72.233.2.58
Name:	lb.wordpress.com
Address: 72.233.69.6
Name:	lb.wordpress.com
Address: 74.200.243.251

If everything goes smoothly you can start using your local DNS server.
Thank you for reading.

About these ads

About palethorn

Geeky GNU/Linux user, computer science student, anime addict, loves programming and optimization
This entry was posted in General, GNU/Linux, Posts in English, Web development and tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

One Response to Install and configure bind9 DNS server

  1. Anonymous says:

    Nice quick and dirty article. However a suggestion for IPv4 only: modify the /etc/default/bind9 file to say OPTS=”-4 -u bind -t /var/lib/named”.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s