Dynamic Dns Updates With A Simple Php Script

This guide will show you how to setup a very simple, useful and straightforward dynamic DNS udpate system using only a small php script (and a nameserver, of course).

What is it?

This is a very simple approach into building a very simple dynamic update system, in a similar fashion to those provided by dyndns.org and similar sites.

Basically, what a client needs to do in order to update its IP is to fetch a webpage which, in this case, it would be;

http://dyn.domain.com/dns/update.php?host=client&pass=secret

It's a simple method because the client only has to fetch a web page to update its DNS IP address (client.dyn.domain.com in this case).

What is it not?

This is not a secure way to install a dynamic DNS update infrastructure. If you need that, take a look at the GnuDIP project.

Requirements

This requirement for this approach are quite basic indeed (that's why it's called "simple").

  • a DNS server, which resolves DNS requests for your dynamic zone (in this case, dyn.domain.com)
  • a HTTP server with PHP support (could be the same DNS server)

In this scenario the script would be run in the HTTP server which, in turn, will update the DNS zone (in the DNS server)

How it works

  1. the (dynamic IP) client makes a HTTP request to the HTTP server, eg: http://dyn.domain.com/dns/update.php?host=client&pass=secret
  2. the PHP script process the request, validates the user/password, and fires the DNS update request through the nsupdate utility
  3. the DNS server receives the update request from the HTTP server and performs the update into the DNS zone leaving the hostname (client.dyn.domain.com in this case) with the new IP address

Receipt

This is a receipt to setup the dynamic DNS update system, as specified above. I assume you have already registered the domain and created the zone in your named configuration (this is not a BIND nor a registrar guide).

Install and configure the PHP update script

Download the PHP script (by clicking on the link below), log into the HTTP server and install the PHP script into a public accesible location, ie. http://dyn.domain.com/dns/update.php.

For simplification purposes, the configuration options are located inside the same script. Those are:

  • $hosts - an array which contains the available hosts and their passwords
  • $zone - the DNS zone to hold the dynamic hosts
  • $dnsserver - the authorative server for the zone specified above (must allow updates from this host)

Configure the DNS server to allow updates

You must also configure the DNS server to allow update from the HTTP server (which could be the same machine). To do that you use the allow-update directive. The zone configuration (in your named.conf file) would be something like this:

zone "dyn.domain.com" { type master; file "dyn.domain.com"; allow-update { 127.0.0.1; }; };

In this case update requests will come from the same machine. If your HTTP server is located in another machine just replace 127.0.0.1 with its IP address.

For better security you should use key authentication. See man named.conf for more information.

Configure the clients

The only thing left to do is configure your clients to update the information by running wget (or a similar web fetch program) on the update. In order to do that, you have to add the following lines to your /etc/crontab file:

/5 * * * root wget -q -O /dev/null "http://dny.domain.com/dns/update.php?host=client&pass=secret"

If anything goes wrong during an update, the system will try again in five minutes. The PHP have protection so that it won't issue a DNS update if the IP hasn't changed. That would alleviate the load on the DNS server thus reducing the zone journaling file size.

Like any other guides on this site, this is a quick and dirty one. "A simple solution for a simple problem". So don't blame me if any of this things doesn't work for you. I only wanted to give you hand. If you have any problems write a comment and I'll do my best, but keep in mind that I'm not a full-time computer guide blogger.

6 responses
Excellent this is was i was looking for very detailed thanks for all !!
Thanks very much! It was very usefully for me!
Pretty great post. I just stumbled upon your blog and wished to mention that I have really loved surfing around your weblog posts. After all I will be subscribing on your rss feed and I am hoping you write again soon!
This is the third time I've been to your site. Thank you for providing more details.
3-Aug-2016 11:09:14.3337: P6660.7fc9e2a8b780@mysever rserpoolsocket.c:354 doRegistration() 23-Aug-2016 11:09:14.3339: Error: (Re-)Registration failed: no registrar available You know what the error reason ? rspserver??
obrigado 'secret', 'host2' => 'secret2', ); # CONFIGURATION ENDS --------------------------------------------------------- #URL EXEMPLO # http://www.monsoft.com.br/dns/update.php?host=c... # CRIA UM ARQUIVO COM REDIRECT PARA O SEU SITE SEM PRECISAR DE ZONA OU NADA # CRONTAB # $ip = $_SERVER['REMOTE_ADDR']; $host = $_GET['host']; $pass = $_GET['pass']; $home =$_SERVER["DOCUMENT_ROOT"]; $path=$home."dns/".$host.".html"; $tmpfile = trim($path); if ((!$host) or (!$pass) or (!($hosts[$host] == $pass))) { echo "FALHA ATUALIZACAO"; exit; } $fp = fopen($tmpfile, 'w'); fwrite($fp, ' '); fclose($fp); echo " $ip - $path "; echo "OK"; ?>