So, you have a home machine running a particular service that you’d like to access, but don’t wish to install any third party utilities or pay for dynamic DNS. As long as you have access to another server elsewhere that does have a static IP, this should be a relatively easy problem for you to solve. There are many possible solutions, but here is how I have managed it in the past. Option 2 below is my preferred option, but option 1 is probably easiest and doesn’t require DNS. Option 3 is an extra, which I’ve also tested and used myself in the past. Both options 1 and 3 have a certain advantage in that they don’t require an external DNS record to be present for your home location, or for you to run the DNS service. Anyway, enough rabbiting on, here’s the meat:
Option 1: Static Text File
Set your home server with dynamic IP to do a regular wget on a site running on your remote server. It should access a secret URL that only you know about. This doesn’t even have to be a real page on the site, it can simply 404 if you wish, as long as it shows up in your access logs. All you then need to do is schedule a script on your remote web server to grep the access logs for that secret URL, awk out the IP and dump it to a text file that you can access on the site. Boom! That text file will always contain the latest known IP address for your home server.
To summarise (text file option):
- On your home server have cron call this as often as you see fit:
wget http://remote.website.location/secret_url
- And on your remote server, have cron call this as often as you need:
grep secret_url /var/log/httpd/site-access.log | tail -n 1 | awk '{print $1}' > /var/www/site_name/remoteip.txt
- Then hit http://remote.website.location/remoteip.txt and you will find the information you require. You can put this file behind basic auth using your apache configuration or .htaccess if you wish.
Option 2: DNS
Assuming that you can run a DNS server such as bind on your remote machine, the process is even smoother. Set your home server with dynamic IP to do a wget on a site running on your remote server. Again, this doesn’t need to be a real page and can simply 404 if you wish, as long as it is recorded in a log file. Next, configure your remote server to get the ip from the access logs again. You can then set it to run a sed for the old IP in the DNS zone file, replace with the new IP and reload bind. If you set a low enough TTL on the record, then this operation will be pretty much seamless.
To summarise (DNS option):
- On your home server have cron call this as often as you see fit:
wget http://remote.website.location/secret_url
- And on your remote server, have cron call this as often as you need (assuming your home DNS name is “home.domainname.com”):
newip=`grep secret_url /var/log/httpd/site-access.log | tail -n 1 | awk '{print $1}'`; oldip=`grep home /var/named/domainname.com.external.hosts | awk '{print $5}'; sed -i "s/\b$oldip\b/$newip/" /var/named/domainname.com.external.hosts; /etc/init.d/named reload
The above is obviously making certain assumptions about your DNS configuration, file locations etc. that you will want to test before actually running. And you can also add something like if [ $oldip = $newip ]; then .....; fi in order to only actually make the DNS change if your IP has changed.
Option 3: Web Proxy
OK, this assumes that you simply want to access an http indexed folder, or some other web service on your home system.
- Configure the http service on your home system to serve the content that you want access to. We’ll use http://home.domainname.com/directory/ as an example
- Set your home system to do a wget as described in the two examples above
- Set your remote system to get your home IP from the access logs as above and dump it this time into it’s hosts file, creating an entry like so:
new.ip.add.ress home.domainname.com
- Configure your remote web server to proxy a specific location to your home DNS name like so:
ProxyPass /home_things/ http://home.domainname.com/directory/ and ProxyPassReverse /home_things/ http://home.domainname.com/directory/
Again, place as much of this behind auth and SSL as you see fit. You will need to enable mod_proxy and mod_proxy_http in your remote site apache config.
Bonus option 4: What Is My IP?
If you can find a web service such as http://www.whatismyip.org/ that will give you your IP in response to a wget, you can simply use that and then have your home system update the remote system with your home address via an ssh or scp command. You could also setup your own web service to return the IP address if you don’t wish to rely on a third party tool.