Selfhost with Namecheap dynamic dns

Selfhost with Namecheap dynamic dns

On this post I'll talk about how you can use a service like Namecheap to buy your domain and also keep that domain in sync with your IP address assigned by your ISP (internet service provider).

This is something that affects me directly since I don't have a fixed IP address from my ISP. Although it remains the same for quite some time i never know when it's going to change due to some ISP decision.

We will create a cronjob that will check our IP address every 5 minutes and update Namecheap with our new IP address. This way there is only a maximum 5 minute window after our IP changes and we update Namecheap with the new IP address.

For this we will use Namecheap with dynamic DNS and ddclient to keep that DNS up to date. I'm also assuming that you already own a domain on Namecheap service.

Let's create our DNS record

First of all we need to create a new DNS record, for that, jump into the Namecheap website, go into Account and click on Domain List.

Then choose the desired domain to change and click Manage.

And go to Advanced DNS.

In there you should see a table of Host Records.

For the sake of this demo, let's create a subdomain for our domain, so let's click on Add New Record.

On the new line select A + Dynamic DNS Record, for the host write your subdomain, blog for this example, and on the IP address write your current public IP address, for the TTL you can leave it on Automatic and save it.

The propagation of this change can take some time, but after that, the new subdomain will be pointing to you IP address.

But we still need to activate the dynamic DNS

On the bottom of that same page you will find a toggle on a line called Dynamic DNS. Click on that toggle in order to activate the service and also get the password required for the next step.

Setup the auto IP update with ddclient

Now it's time to setup ddclient, and first of all, we need to install it.

For debian based systems you can run:

sudo apt install ddclient

ddclient configuration

Now we need to change the configuration, open the file at /etc/ddclient.conf with your prefered editor and look for:

##
## NameCheap (namecheap.com)
##
# protocol=namecheap,                           \
# server=dynamicdns.park-your-domain.com,       \
# login=my-namecheap.com-login,                 \
# password=my-namecheap.com-password            \
# fully.qualified.host

And change it to:

##
## NameCheap (namecheap.com)
##
use=web, web=dynamicdns.park-your-domain.com/getip
protocol=namecheap
server=dynamicdns.park-your-domain.com
login=jejay.me
password=my-namecheap.com-password
blog

On the above configuration replace:

  • the login with your actual domain
  • password with the one got from Namecheap dynamic DNS password above
  • blog with your subdomain.

Testing everything

Let's run the program manually and see if we get the IP and that it is sent to Namecheap correctly

sudo /usr/sbin/ddclient -daemon 0 -debug

And you should see a response similar to:

DEBUG:    proxy  = 
DEBUG:    url    = dynamicdns.park-your-domain.com/getip
DEBUG:    server = dynamicdns.park-your-domain.com
DEBUG:    get_ip: using web, dynamicdns.park-your-domain.com/getip reports 111.111.111.111
DEBUG:    
DEBUG:     nic_namecheap1_update -------------------
DEBUG:    proxy  = 
DEBUG:    url    = https://dynamicdns.park-your-domain.com/update?host=blog&domain=jejay.me&password=yourpasswordshowshere&ip=111.111.111.111
DEBUG:    server = dynamicdns.park-your-domain.com
SUCCESS:  updating blog: good: IP address set to 111.111.111.111

Creating the cronjob

There are some things that we want the cronjob to do:

  • mainly it needs to run every five minutes
  • we need to say that we don't want the daemon service
  • it will run quietly reducing the quantity of messages
  • it will retry failed updates

Run sudo crontab -e and add the following line:

*/5 * * * * root /usr/bin/ddclient -daemon=0 -syslog -quiet retry

And that is it. Every 5 minutes ddclient will check you if address and update Namecheap accordingly.