[HowTo] Free .tk domain name that always points to your Pi

This guide will show you how to have a normal domain name that will always point to your home’s IP address, so you can access your pi from the outside world using a .tk domain name. Its basically like having dyndns.org type of name, but instead of paying for their services now…you have your own .tk name for free. Your raspberry pi will be doing the update. Every 300 seconds it will check your home’s ip address and update your .tk name with a new ip if needed.
So lets start…

  1. Grab a free .tk domain name from http://dot.tk/ (ex: myzuperpi.tk) Make an account on the site and register a .tk name for 1 year…its totally free. Once you’ve done that then create an account at https://www.cloudflare.com/ as well.
    We’ll use cloudflare’s nameservers on the dot.tk domain name then use an updater program on the pi, that will update cloudflare’s IP whenever you change IP at your house, thus changing your .tk IP at the same time.

  2. So you have a .tk name and you’ve made a new account at cloudflare. Now we’ll hook up clourflare’s nameservers to your .tk domain.
    In cloudflare, (once you log in), click at the top left corner the “Add site” button, and type in your .tk domain name.
    It might not find it one the first try, since you’ve just registered it…keep trying until it does. When it allows you to press “Continue Setup”, do it. It will show you a table with some A, CNAME records with an orange cloud next to the right.
    Click on the clouds to turn them off, then press continue and select the Free Website and continue again.

  3. It will take you to a page that has some nameservers. We need the first 2 nameservers to add them at your .tk domain name. Go now to your dot.tk account (log in) and click at Domains->My domains. Then click “Manage Domain” button next to your newly created .tk name. Go to “Management Tools”, then “Nameservers”, select Custom and then copy paste the 2 names servers you got from cloudflare. You dont need nameserver3 , 4 and 5.

  4. To recap. We’ve ‘bought’ a .tk name, that we are not forwarding to cloudflare by using cloudflare’s nameservers on it. That means that the name will now forward all traffic to cloudflare. So the only thing left is to go back to cloudflare and create a DNS record that will get updated by our updater on the pi, that will have your home IP address…So aaaaaaall the traffic from your .tk name will eventually lead to your home’s ip address.
    So lets do that. Go back to cloudflare and from the top left tab click your domain name. Then click the blue button that says DNS. You should see something like this: https://i.imgur.com/jQdNLK3.jpg

If you dont see an A and CNAME record at your setup then create one. Follow the same values as my example…Thats my home IP address…It doesnt matter what you put there…Of course you can put yours, but once we start the updater it will automatically change to your home’s IP address…But do the www thing…so in case you ever host a website on your pi, it will work properly. We’ll also need an API key from cloudflare…We’ll use that for the updater to log in to cloudflare and update your IP. At the top right corner click your username and go to My Profile. Scroll down where it says API KEY and create a GLOBAL API KEY, then copy it…you’ll need it later on.

  1. Now here comes the Pi stuff…We’ll use a client called ddclient. This is the updater, that will automatically update your cloudflare IP whenever you change IP at your house. The one from the raspberry pi repo didnt work well for me so i had to downgrade to an earlier version. So for starters download this version of ddclient. Microsoft OneDrive - Access files anywhere. Create docs with free Office Online.

Now i assume you have an ftp client to connect to your pi, something like winscp or filezilla ?
Use that to connect to your pi and upload the ddclient-3.8.3.tar.bz2 file you just got…put it at /home/osmc folder…so in the end it will here: /home/osmc/ddclient-3.8.3.tar.bz2

  1. We’ll extract the client now and set it up. Its much easier to do this with the root account on the pi instead of the osmc one…since you dont have to constantly type sudo to execute a command. So log on to your pi with putty then type this: sudo passwd root and type in a password that you want to have for your root account. (twice). After you’ve done that, log in to your root account with:su root and type in your root password.
    Now we’ll continue with the extraction and installation. Type the following, pressing Y whenever it asks you something:

    apt-get update
    apt-get install libjson-perl libjson-any-perl libio-socket-ssl-perl bzip2
    cd /home/osmc
    tar jxf ddclient-3.8.3.tar.bz2
    cd ddclient-3.8.3
    chmod +x ddclient
    cp ddclient /usr/sbin/
    mkdir /etc/ddclient
    mkdir /var/cache/ddclient
    cp sample-etc_ddclient.conf /etc/ddclient/ddclient.conf
    nano /etc/ddclient/ddclient.conf

Now a text editor will open. I’ll send you an example of what to place there with Ctrl+V.

daemon=300                # check every 300 seconds
syslog=yes                # log update msgs to syslog
mail=root                # mail all msgs to root
mail-failure=root            # mail failed update msgs to root
pid=/var/run/ddclient.pid    # record PID in file.
ssl=yes                    # use ssl-support.  Works with

#use=ip,                     ip=127.0.0.1    # via static IP's
#use=if,                     if=eth0        # via interfaces
#use=web                    # via web

##
## CloudFlare (www.cloudflare.com)
##
use=web,
protocol=cloudflare,        \
zone=yourdomainn.tk,            \
server=www.cloudflare.com,  \
login=your_email_at_cloudflare@email.com,     \
password=yourapikeyhere             \
yourdomainn.tk

You have to change these values before you copy paste it…add your values instead:
yourdomainn.tk <— your new .tk domain name
your_email_at_cloudflare@email.com <---- the one you use to log in cloudflare
yourapikeyhere <— the api key you got from cloudflare
yourdomainn.tk <----at the bottom of the config file…dont forget this!

Once you copy paste them, Ctlr+X, then Y, then Enter

  1. Now we’ll make ddclient a service to start when the pi boots.
    I’ve used this guide here for that: Ddclient not working as a service (Rpi2 - OSMC alpha4) - #2 by DBMandrake

Basically we do this:
nano /etc/systemd/system/ddclient.service

Copy paste inside this:

[Unit]
Description=Dynamic DNS Client
After=network.target

[Service]
Type=forking
User=root
Group=root
UMask=002

ExecStart=/usr/sbin/ddclient -daemon 300 -syslog

Restart=Always
TimeoutStopSec=300

[Install]
WantedBy=multi-user.target

Then Ctrl+X, Y, Enter again

And finally do this:

systemctl start ddclient.service
systemctl enable ddclient.service
systemctl daemon-reload
ddclient -daemon=0 -debug -verbose -noquiet

The last command of this last bunch is an important one. You should have that command somewhere on the side. If you execute it, it will manually update your IP at cloudflare with your router’s ip but also give you information about the update. If something failed or if the ip didnt update or something. You can execute it whenever you want…dont be afraid.

This service will check every 300 seconds and see if your IP matches the one at cloudflare…if it doesnt it will update it automatically.

And you are done…you can use your .tk domain name now from anywhere in the world on a putty console (or even vnc) instead of your dynamic IP address.

Note
Your new .tk name cant be use from your pc to connect to the pi (they are both under the same LAN network). If you want to do that you’ll have to edit your C:\Windows\System32\drivers\etc\hosts file and add a forwarding line to point to your pi’s local ip address…like so:

192.168.1.10 mydomain.tk

where …10 is the local ip of your pi.

2 Likes

Nice write-up. I like being able to use an easy to remember address rather than IP.

I use Duckdns. It’s free, easy, and you get a yourname.duckdns.org address.
You can sign up using your Persona, Twitter, Facebook, Reddit or google account.
They even have instructions for updating your IP using the pi:
Duck DNS - install and click PI.

It updates using crontab - simply by connecting to a URL (which includes your access token) from your Pi, and it updates it to the IP address of the connecting device (or you can specify an IP).

1 Like

Oh nice…I didnt know duckdns was that easy. I was using dyndns for years, but when i realized i can use cloudflare to do this trick, i was hooked :slightly_smiling:
But thats a pretty simple way to update your domain, with duck…I’ll keep it in mind. Thanks!

I used your .tk + cloudflare formula just to see how it worked although the duckdns seems very easy.

I couldn’t ever figure out how to d/l the ddclient from onelive using wget but also it seemed a little shady. So I downloaded the distro version and just applied the patch on this page. http://www.ubuntugeek.com/how-to-use-cloudflare-as-a-ddclient-provider-under-ubuntu.html

You’ll have to ‘apt-get install patch’ also. You won’t have to create the service either but you do need to edit /etc/default/ddclient to set run_daemon to true.

Seems to work fine.

1 Like

I uploaded the client on my onedrive account so i can be sure its there forever :slight_smile: You cant wget it…has to be uploaded manually to the pi.
Thats nice about the patch…i couldnt get it work with cloudflare, so i stayed on the ‘more stable’ version of ddclient without the patch. My zip is the ddclient-3.8.3 version…so i guess its the same in the end ?

The only thing about the .tk names is their policy that you must have a page running under that name. But i’ve installed an apache server on the pi and i am serving a fake portfolio page for it :stuck_out_tongue:

I setup something similar with my own custom domain, using a free Cloudflare account to get the DDNS functionality. I looked at the packages available for dynamic DNS functionality but ended up modifying code I found online and putting together a shell script that does everything for me - this just gets run with cron. I think I might have needed to install a package to get the dig command to work (can’t recall).

YMMV, the code is not well documented, probably breaks all proper conventions, and if this breaks your setup I take no responsibility, but for what it’s worth, my shell script looks like this:

#!/bin/bash

# Step 1: Fill in EMAIL, TOKEN, ZONE_NAME (domain) and REC_NAME (subdomain). Your token is here: https://www.cloudflare.com/my-account
# Step 2: Create an A record on Cloudflare with the subdomain you chose
# Step 3: Run "./ddns.sh -l" to get the ZONE_ID and REC_ID for the domain and subdomain. 
#         Fill in REC_ID and ZONE_ID below
# Step 4: Run "./ddns.sh". It should tell you that record was updated or that it didn't need updating.
# Step 5: Run regularly with cron.
#         */5 * * * * /path/to/ddns.sh > /path/to/log/file

# Cloudflare parameters
EMAIL='youremailaddress'
TOKEN='yourCFtoken'
ZONE_NAME='yourtopleveldomain'
ZONE_ID='yourCFzoneid'
REC_NAME='yourCFsubdomain'
REC_ID='yourCFsubdomainrecordID'

# If specified, obtain and print records then exit
if [ "$1" == "-l" ]; then
  ZONE_ID=$( curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$ZONE_NAME" \
    -H "X-Auth-Email: $EMAIL" \
    -H "X-Auth-Key: $TOKEN" \
    -H "Content-Type: application/json" \
    | grep -Po '(?<="id":")[^"]*' \
    | head -1 )
  REC_ID=$( curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?name=$REC_NAME" \
    -H "X-Auth-Email: $EMAIL" \
    -H "X-Auth-Key: $TOKEN" \
    -H "Content-Type: application/json" \
    | grep -Po '(?<="id":")[^"]*' )
  echo Zone Name: $ZONE_NAME
  echo Zone ID: $ZONE_ID
  echo Record Name: $REC_NAME
  echo Record ID: $REC_ID
  exit 0
fi

# If specified, obtain and print A record then exit
if [ "$1" == "-a" ]; then
  CFIP=$( curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?type=A&name=$REC_NAME&match=all" \
    -H "X-Auth-Email: $EMAIL" \
    -H "X-Auth-Key: $TOKEN" \
    -H "Content-Type: application/json" \
    | grep -Po '(?<="content":")[^"]*' )
  echo Record Name: $REC_NAME
  echo A Record: $CFIP
  exit 0
fi

# Resolve current IP address via DNS
IP=$(dig +short myip.opendns.com @resolver1.opendns.com)
if ! [[ $IP =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  [ "$1" != "-s" ] && echo "Cannot determine public IP address, exiting"
  exit 1
fi

# Resolve domain to IP address
DNSIP=$(host $REC_NAME)

# Exit if domain cannot be resolved
if [ $? -eq 1 ]; then
  [ "$1" != "-s" ] && echo "Cannot resolve $REC_NAME, exiting"
  exit 1
fi

DNSIP=$(echo $DNSIP | awk '{ print $NF }')

# No action required if current IP matches resolved IP
if [ "$IP" == "$DNSIP" ]; then
  [ "$1" != "-s" ] && echo "$REC_NAME resolves to public IP ($IP), exiting"
  exit 0
fi

# At this point public IP and resolved IP records exist and do not match
[ "$1" != "-s" ] && echo "Public IP ($IP) does not match DNS record ($DNSIP), checking Cloudflare"

# Retrieve the Cloudflare A record IP address
CFIP=$( curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?type=A&name=$REC_NAME&match=all" \
  -H "X-Auth-Email: $EMAIL" \
  -H "X-Auth-Key: $TOKEN" \
  -H "Content-Type: application/json" \
  | grep -Po '(?<="content":")[^"]*' )

# If Cloudflare IP record matches public IP then there must be a DNS issue, no update required
if [ "$CFIP" == "$IP" ]; then
  [ "$1" != "-s" ] && echo "Cloudflare IP ($CFIP) matches public IP ($IP), exiting"
  exit 0
fi

# At this point the public IP does not match the IP address stored in Cloudflare
[ "$1" != "-s" ] && echo "Cloudflare IP ($CFIP) does not match public IP ($IP), update required"
[ "$1" != "-s" ] && echo "Setting Cloudflare DNS record to public IP"

# Set Cloudflare DNS record
RESULT=$( curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$REC_ID" \
  -H "X-Auth-Email: $EMAIL" \
  -H "X-Auth-Key: $TOKEN" \
  -H "Content-Type: application/json" \
  --data "{\"id\":\"$ZONE_ID\",\"type\":\"A\",\"name\":\"$REC_NAME\",\"content\":\"$IP\"}")

if [[ $RESULT == *"\"success\":false"* ]]; then
    echo "API update failed. Dumping results:"
    echo -e $RESULT
    /home/osmc/push.sh "Cloudflare API update failed, check log for details"
    exit 1
else
    echo "API update successful"
    /home/osmc/push.sh "DNS records have been updated, public IP is $IP"
    exit 0
fi

It might not be pretty, but I have had this script running every 5 minutes for the past 6 months and it’s worked perfectly, including the basic error catching that I built into it.

Each time the script updates my DDNS records it also pushes a notification to my phone - details on how that was set up is in my HowTo.

Nice, a more ‘manual update’ type of thing. heheh. I did something similar directly updating .tk records, (before i found out about cloudflare and ddclient). Thanks for taking the time to present it here.
There is another good push notification app called Prowl, http://www.prowlapp.com/ that you can do the same type of push. Its a bit cheaper than yours, and it has a mozilla plugin as well, to send addresses directly to your phone. I use that plus https://thingspeak.com/ to do various arduino type gadgets with wifi notifications.

I noticed yesterday that my .tk domain was gone. No emails, no notice, nothing… Not sure exactly what happened, but I’ve reregistered it for now.

They must have sent something…Did you check your spam folder ?
Lately dot.tk has been hunting down domain names that dont have a website on them. Even a simple crappy page will do to get them off your back.
I have 7 domain names with them and 4 only got checked so far, so i uploaded a “Soon blah blah” page for all of them.

you can also get a dollar domain from namecheap https://www.namecheap.com/promos/2016/crazy-88.aspx use their DNS which is pretty good and have a nicer domain to start with

1 Like

Nice one! Didn’t know about that. Thanks for the info :slight_smile:

Enjoy

1 Like

We will have a dynamic DNS feature included OSMC in the future.

oh Hi Sam its michael here.
Should I just leave the whole cloudflare and .tk domain thing then? is it a waste of time if I’m not going to use it in the immediate future?
Thanks Michael

Yes. It’s not going to happen for a while yet, but why bother if you don’t need it yet?

Sam

just for the fun of seeing it actually work? Ive never ever been able to get DNS working before? which as far as I know is the only way to access your local network form anywhere in the world without having to buy an expensive and rare static ipaddress.