Custom Domain, DDNS, and Raspberry PI

Using a custom domain with a dynamic IP

Custom Domain, DDNS, and Raspberry PI

Inspired by this article:

http://www.ianatkinson.net/computing/ddns.htm

Your own DNS

You need to get bind running on your rasp pi. Lots of tutorials about this in the ether (install bind9).

Change the /etc/bind/named.conf.local file

zone "domain.biz" {  
	type master;  
    file "/home/pi/homedns/domain.zone";
};

You need to define a zone file:


$TTL 60S
@ IN SOA myns1.duckdns.org. mail.uk. (  
	2017081402 ; serial  
    8H ; refresh  
    2H ; retry  
    4W ; expire  
    60S ; minimum
)

NS myns1.duckdns.org.
NS myns2.duckdns.org.

myns1.duckdns.org A 86.21.183.217
myns2.duckdns.org A 86.21.183.217

www                 A 86.21.183.217
homepage  CNAME       www

You need to have two nameserver domains with your ddns provider (eg: no-ip, duckdns), in the example above myns1.duckdns.org and myns2.duckdns.org.

Start bind

sudo systemctl start bind9

Check it is resolving locally

dig -tns domain.me @localhost

The NS authority section should say “myns1.duckdns.org”

Configure your domain name provider

Set your nameserver for your custom domain in your domain provider (such as 123-reg) to point to the two name servers defined in your ddns (eg myns1.duckdns.org, and myns2.duckdns.org).

For example in 123-reg.co.uk you would go to the manage nameserver section to replace the 123 ns1/ns2 server urls with your new ddns ones. You can then use the dig command to check:

dig -tns domain.me @resolver1.opendns.com

It should respond with your ddns nameservers (note: the change in your domain provider can take a while).

Run:

dig domain.me @resolver1.opendns.com

The IP address should be of your dynamic ISP.

NOTE: You should have a script to keep the ip in your DDNS provider in step with the dynamic IP provided by your ISP.

Zone IP Changes

The next part is to then also change the zone file for the domain as well whenever your ISP changes the IP.

This is a perl script to change the zone IP (based on Ian Atkinsons’ perl script):

You can use crontab to check the IP every so often and then update the zone file using the script.  Like so (assuming the script above is named change.sh and is in your homedns directory):

*/6 * * * * ~/homedns/change.sh >/dev/null 2>&1

Test Your Server

You can easily run a server on your local machine (such as nginx), even easier if you have docker.

On more thing you need to do is to open up your router to allow the outside world to connect to the Rasp Pi (your machine) to the specific port running your server.

You also need to allow the router to connect to your Bind DNS server (port 53 for both TCP and UDP)

You won’t be able to call your custom domain inside your home network (unless to add the domain to /etc/hosts). However an easy way is to turn off the Wifi on your mobile and use the data network to test.

Mail Forwarding

If you need to forward mail to say mail@domain.me then you can add the following mx records in your file. Here the mail server is from 123-reg.

@       MX 1 mx0.123-reg.co.uk.
@       MX 2 mx1.123-reg.co.uk.

And you can check the MX records with

dig mx domain.me

You should see an answer section like so

;; ANSWER SECTION:
domain.me.  53 IN MX 1 mx0.123-reg.co.uk.

~ Voila ~