Raspberry Pi as a self-hosted Server

Configuration of Dynamic DNS

In order to register a domain name for your IP address, you must first create a DynDNS account using a provider of your choice. There are many providers supporting this service free of charge. An excellent choice is http://freedns.afraid.org/ where you can choose from a broad number of domain names. After creating an account, all you need to do is to register a subdomain for the external IP address you have determined above.

To support both IPv4 and IPv6, you should add two records. An “A” record, pointing to the external IP address of your router, and an “AAAA” record with the IPv6 address of your Raspberry Pi.

Please note that the blue (G) behind the subdomain name indicates that Google is allowed to crawl and index your page. This is important if you want your blog to be found by search engines such as Google or Bing. If you don’t see a blue (G) behind your subdomain name, you can kindly ask afraid.org to activate this service for you.

An MX entry is only required if you’re going to use your Raspberry Pi as a e-mail server (see below).

IPv6 should be already enabled by default on a fresh Debian Wheezy installation. If not, make sure that ipv6 is enabled at boot time (check /etc/modules for the line ipv6 and do a “lsmod” to check whether the ipv6 module is loaded). To determine the IPv6 address of your RasPi, type “ifconfig” into your console. You should see an inet6 address line next to your active network device (eth0 or wlan0).

In order to notify the DynDNS service about IPv4 address changes, you have to enter your account data into your router’s DynDNS configuration page.

fritzbox2If you are behind a NAT, your router might be unable to see your real IP address. In case you’re a lucky owner of an AVM Fritz Box, you can try using the freetz.org firmware mod. The freetz firmware has some build-in scripts to determine your external IP address. Furthermore, it provides the inadyn-mt update client,  which is able to maintain up to five different DynDNS accounts.

In order to notify afraid.org about an IPv6 change, you should run a python script on your RasPi, rather than on your router, which calls the update link on each IPv6 address change:

import sys
import os
import time
import stat
from urllib import urlopen

# FreeDNS Update Key
update_key = "<enter your secret update hash here>"

# Open URL to return the external IP
external_ip = os.popen('curl -s icanhazip.com').read()
#print external_ip

# FreeDNS Update URL
update_url = "http://freedns.afraid.org/dynamic/update.php?" + update_key + "&address=" + external_ip

# The file where the last known external IP is written
ip_file = "freedns_ip"

# Create the file if it doesnt exist otherwise update old IP
if not os.path.exists(ip_file):
 fh = open(ip_file, "w")
 last_external_ip = "Unknown"
 #print "Created FreeDNS IP log file: " + ip_file
 fh = open(ip_file, "r")
 last_external_ip = fh.read()
 last_external_ip = last_external_ip.rstrip('\n')

# Check old IP against current IP and update if necessary
if last_external_ip != external_ip:
 #print "External IP updated FROM (" + last_external_ip + ") TO (" + external_ip + ")"
 fh = open(ip_file, "w")
 last_ip_update = time.ctime(os.stat(ip_file).st_mtime)
 #print "External IP (" + external_ip + ") has not changed. Last update was " + last_ip_update

To check your update URL, click onto the “Dynamic DNS” link of your http://freedns.afraid.org/ account. On the bottom of the page wget and curl example lines for crontab are given. In case your’re using IPv6, you should add an “&address=<ipv6>” of your RasPi to the end of the update URL as shown below:

http://freedns.afraid.org/dynamic/update.php?<hash value>&address=<IPv6 address>

If you want to configure both IPv4 and IPv6, make sure to turn Link updates of the same IP together? OFF! Otherwise your AAAA record will be changed to an A record. You can toggle this setting on the Dynamic DNS Update URLs page at http://freedns.afraid.org.

If you have configured everything correctly, you should be able to “ping” your domain name. Depending what kind of services you want to run on your RasPi, you must open the respective ports on your router. Find your router’s port forwarding configuration page and open port 80 (for a httpd server such as Apache, Nginx or Lighttpd). Open port 25 if you’re going to run your own e-mail server.