One of the first IT projects I undertook was building my own DNS server. This has a host of benefits, from being able to define other local computers by name (and not relying on Windows' computer name property anymore) all the way up to increased mobility between Internet Registrars.
One of the problematic parts about hosting your website locally is that you want to expose a different IP address to your local network than what is exposed externally. Luckily for us, Bind9 has a method for doing this called Views.
In our example, we will be setting up a domain name called "mydomainname.com". Our local network is 192.168.0.0/24 (which means 192.168.0.0 - 192.168.0.255).
The first thing we want to do is open up the named.conf file and ensure that it is importing the named.conf.local file.
sudo nano /etc/bind/named.conf
You should see the following at the bottom of this file:
include "/etc/bind/named.conf.local";
Great, close named.conf, and we will now go and edit named.conf.local.
sudo nano /etc/bind/named.conf.local
Great, add the following, replacing the 192.168.0.0/24 with your local network address range, and the "mydomainname.com" with your domain name:
acl internals {
192.168.0.0/24;
127.0.0.0/8;
};
view "internal" {
match-clients {internals;};
recursion yes;
zone "mydomainname.com" {
type master;
file "/etc/bind/zones/internals/mydomainname.com.db";
};
};
view "external" {
match-clients {any;};
recursion no;
zone "mydomainname.com" {
type master;
file "/etc/bind/zones/mydomainname.com.db";
};
};
Now, we need to set up the zone files for our internal and external people.
sudo nano /etc/bind/zones/mydomainname.com.db
Enter the following, replacing the Serial with the date in the form yyyyMMddxx, where the xx refers to the time you have edited the file today (use 01 today). Replace 60.60.60.60 with your external IP address. Replace "mydomainname.com" with your domain name.
$TTL 604800
mydomainname.com. IN SOA dns.mydomainname.com. root.mydomainname.com. (
2000022301 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
mydomainname.com. IN NS dns.mydomainname.com.
IN A 60.60.60.60
@ IN A 60.60.60.60
dns IN A 60.60.60.60
mydomainname.com IN A 60.60.60.60
www IN A 60.60.60.60
Next, edit the internal version of this file.
sudo nano /etc/bind/zones/internals/mydomainname.com.db
Then enter the following, replacing the 192.168.0.100 with your internal server IP address, and mydomainname.com with your domain name.
$TTL 604800
@ IN SOA dns.mydomainname.com. root.mydomainname.com. (
2009022301 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
mydomainname.com. IN NS dns.mydomainname.com.
@ IN A 192.168.0.100
www IN A 192.168.0.100
Now restart your bind server. Your internal network will now resolve to your internal server IP, and external people will resolve to your external IP.
sudo /etc/init.d/bind9 restart