Have you ever wanted to access a home computer or server but you didn’t know your IP address? That’s probably because your IP address is dynamic and changes periodically. So how do you find out what your IP address is? If you’re on your home network, then it’s easy by using any number of websites whose sole purpose is to tell you what your IP address is, for example www.whatsmyip.org. But what if you’re away from home and your IP address changes? Again, there are services to help you with this, but in this article, I’m going to show you how to easily setup your own service.
When you’re away from home, there are several ways to get your IP address. One option is to use a dynamic DNS service, but most are not free. Another option might be to log into your account at your internet providers website. Finally, you could set up your own service that automatically gets your IP address and generates a webpage for you. All it takes is a personal website outside of your network, a computer running 24/7 on your network and some simple scripts.
The ultimate goal for me was to be able to access my home server for remote administration, which runs Ubuntu and serves as my Plex Media Server, file server, and host to a few other services. Additionally, I didn’t want to be beholden to a dynamic DNS service, nor did I want to log into my internet providers website to access my IP address. I’ve found that this was slow and clunky. Furthermore, by setting up an automated process, I could create a personal webpage with multiple links back to my home network that access various computers on the network. Very slick in my opinion!
There are several components to this setup. None are very difficult, but they all work together to provide you with a way of always knowing what your home IP address is. Let me outline what’s needed and then I’ll get into the details of putting it all together. Here’s the hardware and steps needed to accomplish my goal:
- A computer on your local network running a Linux/Unix based operating system. This could be something as simple as a Raspberry Pi or a full-blown server.
- External website that is capable of parsing PHP files.
- Simple PHP webpage hosted on your external website that reads the REMOTE_ADDRESS and writes it to a file on the web server.
- Script on local computer that queries the web server.
- Crontab schedule on local computer that runs local script every 15 minutes.
- Script on web server that reads the IP address from the above file and generates a webpage with the IP address.
- Crontab schedule on web server to run script every 15 minutes.
Now this may sound complicated, but it’s really quite simple. Let me break it down for you one step at a time.
LOCAL SCRIPT
The first step is to write a simple script on your local Linux computer that is nothing more than a bash script that queries your website for a specific webpage.
Begin by creating a new file with the following code in it. I named my file ip_query.sh. Save it to a convenient location on your computer, for example: /home/username/bin/
#!/bin/bash wget www.yourwebsite.com/your_webpage.php
Notice in the code above, that I’m using the wget command, which is basically a non-interactive downloader, to access your webpage. We will be creating this webpage shortly, but make the decision now of what you want this page to be named and where it’s located on your website. It needs to be the same as what’s used in this script.
In order to run the script, it needs to be executable. To do so, use chmod as follows:
chmod 755 ./ip_query.sh
Next, let’s set up a Cron job so that the script executes every 15 minutes. To setup a schedule, you need to edit /etc/crontab as super user. Enter the following at the command line to edit the crontab file in VIM. If you’re unfamiliar with VIM, there are plenty of tutorials online.
sudo vim /etc/crontab
The file will open with VIM to allow editing. Add the following line to the crontab file.
*15 * * * * your_username /path/to/ip_query.sh > /dev/null 2>&1
Save and exit VIM. Your shell script will now run every 15 minutes on minutes 0, 15, 30, & 45 of every hour. Notice that I’m using I/O redirection to output any content or errors to /dev/null. That’s what the redirect to /dev/null 2>&1 means. For more information on crontab, see this webpage at Ubuntu Documentation. For more information regarding I/O redirection, see this webpage.
PHP WEBPAGE
This is where all the magic happens. We need to create a php webpage that your local computer will be requesting every 15 minutes. Although you’ll never see the the output of this page, it’s doing two key things to make this process work. First, it gets the IP address of your local network, and secondly, it writes it to a file on the web server.
To get started, create a file on your server named ip.address. Place this in a directory somewhere outside of your web root directory.
Next, use VIM or your favorite text editor to create a file with the following code. Name it the same as what you configured in the script on your local computer.
<?php
$IP = $_SERVER['REMOTE_ADDR'];
$FILE="/absolute/server/path/to/your/file/ip.address";
$FP = fopen($FILE
, "w") or die("Unable to open file.");
fputs($FP, $IP);
fclose($FP);
?>
Let me break down what this script is doing:
- The first line stores the remote IP address of the remote computer to the variable, $IP. In our case, the computer on our local network will be requesting this page, so our home IP address will be the value.
- The second line stores the name and location of a file on the webserver, ip.address, to a variable, $FILE. Notice that you need to define the absolute server path to the ip.address file.
- The third line uses fopen() to open $FILE and bind it to a stream, $FP.
- The value in $IP is written to $FP using fputs().
- $FP is closed with fclose().
This file will be written to every time this webpage is called, and as it’s currently configured, the contents in ip.address will be overwritten every time the webpage is called.
Save the file to your web directory making sure that its location matches the path in the script you created earlier on your local computer.
WEB SERVER SCRIPT
Now we need to create a script on the web server that is scheduled every 15 minutes. When executed, it will read the IP address from the file, ip.address, and create another webpage with this address displayed on it. The content and format of this webpage is entirely up to you. The key element is that this webpage displays the IP address that is retrieved from the file, ip.address. Furthermore, this webpage is accessible to you from anywhere in the world, giving you your home IP address. If you’re concerned with security, then you could password protect the directory that this webpage is in using an htaccess file. See this webpage for more information on how to password protect a directory with htaccess.
Create a file and enter the following code:
#!/bin/bash
FILE="/absolute/path/to/ip.address"
IP=$(head -n 1 $FILE)
PAGE="<!DOCTYPE html>
<html>
<head>
<title>IP Address</title>
<meta http-equiv=\"Content-Type\" content=\"text/html charset=UTF-8\" />
<meta http-equiv=\"Cache-Control\" content=\"no-cache\" />
<meta http-equiv=\"Pragma\" conten=\"no-cache\" />
<style>
body { font-family: Verdana,Tahoma,Arial,Helvetica; font-size:9pt}
.header { font-size: 16pt; font-weight: 900; }
</style>
</head>
<body bgcolor=\"#ffffff\">
<a href=\"http://$IP\">$IP</a>
</body>
</html>"
echo "$PAGE" > /path/to/your/web/directory/ip.html
Let me explain this script:
- First, it reads the first line from the file, ip.address, and stores the value in a variable $IP. This value will be your home IP address.
- Next, the html code for your webpage that you’ll be accessing from anywhere in the world is defined and stored to the variable $PAGE. Note that this webpage simply displays the IP address as a hyperlink, but you can get as creative as you want here.
- Finally, the contents of $PAGE are written to a file called ip.html, which should be saved somewhere in your web root directory. This will be the page that you access whenever you want to know your home IP address. Again, if you don’t want it publicly accessible, then password protect the directory that this file is in using an htaccess file.
Once you’re created the above script, make it executable using chmod:
chmod 755 ./ip_write.sh
As you did before, set up a Cron job on the webserver to execute the script every 15 minutes. Edit /etc/crontab as super user using the VIM text editor:
sudo vim /etc/crontab
Add the following line to the crontab file, save, and close.
05,20,35,50 * * * * your_username /path/to/ip_write
.sh > /dev/null 2>&1
Notice that this line is very similar to the entry used on your local computer with the exception of the schedule. I chose to offset the execution of this script from the local script by five minutes. Whereas the local computer script was executed on minutes 0, 15, 30, and 45 of every hour, this script executes on minutes 5, 20, 35, and 50.
CONCLUSION
And that’s it. You should now have a webpage accessible to you from anywhere in the world that displays your home IP address. And the best thing is you’re not paying for another service or relying on a third party other than your web host.