Posted on

HOW TO INSTALL AND SETUP BIND9 ON UBUNTU SERVER

So you knew how to manually install and setup a Linux, Apache, MySQL and PHP (also known as LAMP Stack) on a server based on Ubuntu. You may be wondering how to make your server being accessible by domain not by IP address. Well, there are several ways you can do that but the key method is simple: by using DNS Server. There are two methods to have DNS Server connecting your domain to your server: First, you can simply use DNS management service which is usually offered freely by most of domain registrar like GoDaddy and NameCheap; Another method is to install and setup your own DNS server right inside your operating system to manage how the way your domain resolves to your server. I will explain the first method another day while today in this page you’ll see the second method.

p.s: When I say “server” it refers to both VPS or Dedicated server

REQUIREMENTS

 

  1. This tutorial is done on Ubuntu-based server. I am running Ubuntu 12.0.4
  2. Windows users should download Putty while Mac and Linux users can simply use Terminal
  3. You’ll also need a basic skill to use Putty and to navigate through SSH.
  4. In this tutorial I will use Bind9 which is a free DNS management software available for most Operating System including Unix-based OS. I will not explain what is BIND and what are its key features as Wikipedia covers that topics better.
    Read: What is DNS Management Software and comparison of available software.
  5. You may firstly install LAMP stack or any other stack you wish.
  6. About 15 minutes of your time and a cup of tea if you like.

STEP 1. LOGIN TO YOUR SERVER

Launch Putty (or Terminal) and login to your server via SSH connection. Please remember if you’ve followed our very basic guide here, you may already have disabled root login or changing default SSH port from 22 to other numbers. If you really did disable root login, then login by using new username and password you created then type following command once you logged in, otherwise you can simply skip this command:

1
sudo su

then you have to enter root password.

STEP 2. INSTALL BIND9

Type this command then hit enter:

1
apt-get install bind9

install bind9

Once done you’ll see a message saying:

1
* Starting domain name service... bind9      [OK]

STEP 3. BASIC CONFIGURATION

So you have installed BIND9 on your server. It is time to show you a basic configuration how to setup your domain to resolve to your server. Here’s what I did. Type following command then hit Enter on your keyboard:

1
nano /etc/bind/named.conf.local

Nano editor screen will appear. Copy-paste command below into your favorite text editor like Notepad, Notepad2 or Notepad++:

1
2
3
4
5
6
7
8
9
zone "domain.com" {
type master;
file "/etc/bind/zones/domain.com.db";
};
zone "3.2.1.in-addr.arpa" {
type master;
file "/etc/bind/zones/rev.3.2.1.in-addr.arpa";
};

Again, in any command in this page you’ll need to replace “domain.com” with your own domain name and corresponding TLD. E.g: domain.com. Also, you have to replace xxx.xxx.xxx.xxx withIP address of your server.

Once you’ve done editing necessary values, you can paste it to Nano editor screen. You can also simply type command syntax above to Nano editor screen while replacing necessary value during your typing.

edit config file

Now hit Control+O on your keyboard to save the file then hit Control+X to exit Nano editor.

Then go to bind directory to make things easier. Type this command:

1
cd /etc/bind

Create new directory called “zones” inside it.. Use this command:

1
mkdir zones

Now go enter that directory as well..

1
cd zones

cd zones

Then create new file called “nano domain.com.db“. Type this command:

1
nano domain.com.db

of course at this part you’ll also need to replace “domain.com” with your own domain name. Example:

1
nano domain.com.db

That command brings Nano editor screen up once again. Next, you can either copy these syntax to Notepad first then edit it or simply type it in Nano editor directly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
; BIND data file for domain.com
;
$TTL 14400
@ IN SOA ns1.domain.com. host.domain.com. (
201006601 ; Serial
7200 ; Refresh
120 ; Retry
2419200 ; Expire
604800) ; Default TTL
;
domain.com. IN NS ns1.domain.com.
domain.com. IN NS ns2.domain.com.
domain.com. IN MX 10 mail.domain.com.
domain.com. IN A xxx.xxx.xxx.xxx
ns1 IN A xxx.xxx.xxx.xxx
ns2 IN A xxx.xxx.xxx.xxx
www IN CNAME domain.com.
mail IN A xxx.xxx.xxx.xxx
ftp IN CNAME domain.com.
domain.com. IN TXT "v=spf1 ip4:xxx.xxx.xxx.xxx a mx ~all"
mail IN TXT "v=spf1 a -all"

Here’s an example of mine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
; BIND data file for domain.com
;
$TTL 14400
@ IN SOA ns1.domain.com. host.domain.com. (
201006601 ; Serial
7200 ; Refresh
120 ; Retry
2419200 ; Expire
604800) ; Default TTL
;
domain.com. IN NS ns1.domain.com.
domain.com. IN NS ns2.domain.com.
domain.com. IN MX 10 mail.domain.com.
domain.com. IN A 141.0.170.144
ns1 IN A 141.0.170.144
ns2 IN A 141.0.170.145
www IN CNAME domain.com.
mail IN A 141.0.170.144
ftp IN CNAME domain.com.
domain.com. IN TXT "v=spf1 ip4:141.0.170.144 a mx ~all"
mail IN TXT "v=spf1 a -all"

If you have two IPs, you can replace “ns2 IN A xxx.xxx.xxx.xxx” with your second IP address. Otherwise simply use the same IP.

domain.com.db

Once done editing, hit Control+O to save that file then hit Control+X to exit Nano editor.

Next step, it’s time to define reverse DNS lookup. Sounds not so familiar? Don’t worry simply repeat my steps. Type this command syntax:

1
nano /etc/bind/zones/rev.3.2.1.in-addr.arpa

That will open Nano editor screen again but this time you will edit another file. Add following lines inside that file..

1
2
3
4
5
6
7
8
9
@ IN SOA domain.com. host.domain.com. (
2010081401;
28800;
604800;
604800;
86400 );
IN NS ns1.domain.com.
4 IN PTR domain.com.

Replace “host.domain.com” with your own server’s host name. Once done, press Control+O to save followed by Control+X to exit.

Example:

1
2
3
4
5
6
7
8
9
@ IN SOA domain.com. host.domain.com. (
2010081401;
28800;
604800;
604800;
86400 );
IN NS ns1.domain.com.
4 IN PTR domain.com.

screenshot:

arpa address

That’s not done yet. There is another file you have to edit. Run this command:

1
nano /etc/resolv.conf

Now add following line at very top of any lines you see there:

1
search domain.com

Example:

resolver

Once done, press Control+O to save followed by Control+X to exit. That is it. But to make sure that Bind9 can apply all recent changes above, you better restart Bind9 service using this command:

1
/etc/init.d/bind9 restart

To conclude, in your Putty screen it will show all the command syntax used which is something like this:

all command syntax

STEP 4. CHANGE NAMESERVER IN YOUR DOMAIN REGISTRAR

I will not explain this last step because the way it can be done is really vary. It depends on which registrar you purchased the domain from. E.g: GoDaddy, NameCheap, Name, and so on.

The key point is to add ns1.domain.com and ns2.domain.com there. Once done, wait for about 24 hours for your domain to resolve to your server but sometimes it takes faster and done within few hours.

You can confirm it easily via web browser then type your newly configured domain name:

 

STEP 5. TEST YOUR DNS

In this case lets install additional tool called “DNS Utility”. Here’s how to install it:

1
apt-get install dnsutils

Once the install process finished, type following command:

1
dig domain.com

If everything is set correctly you’ll see something like this one below..

You can also test it using third-party service like Pingdom.com DNS test tool here.