Many home and small business internet connections use dynamic IP addresses that can change periodically. This makes it challenging to reliably access services (such as a web server or home network) hosted on that connection, as the IP address changes over time.
Dynamic DNS (DDNS) is a valuable technology that provides reliability, remote access, and portability for internet-connected devices and services that would otherwise be impacted by dynamic IP addresses. DDNS allows you to associate a static domain name with the dynamically changing IP address, enabling you to consistently access your services using the same domain name. This is particularly useful for accessing home networks, security cameras, or other internet-connected devices when you’re away from home. DDNS services are often free or low-cost, especially compared to the alternative of using a static IP address, which usually requires paying an additional fee to your ISP.
Without using DDNS, you can still access the internet-connected service at your home if the dynamic IP address is periodically emailed to you. Now, let me show you how this can be achieved.
First, connect an Orange-Pi board running Ubuntu to the broadband router. You can find the public IP address of the broadband router using the following curl command in the Ubuntu terminal.
curl https://api.ipify.org
Next, you can send the public IP address of the broadband router to your email account using the mail command-line tool in Ubuntu. Here’s how you can accomplish this:
First, you’ll need to install the mailutils package, as the mail command is part of this package. You can install the package using your Ubuntu package manager.
sudo apt-get install mailutils
After installing the mailutils package, you’ll need to install a Mail Transfer Agent (MTA), which is a critical component of the email infrastructure. MTAs facilitate the reliable and secure transfer of email messages between senders and recipients on the internet. They work closely with other email components to provide a complete email delivery system.
To install the Postfix MTA, you can run the following command in the Ubuntu terminal:
sudo apt-get install postfix
During the Postfix installation process, you’ll be prompted to configure the mail system. When asked about the type of mail configuration, select “Internet Site.” Once the installation is complete, open the Postfix main configuration file, which is typically located at /etc/postfix/main.cf
.
sudo nano /etc/postfix/main.cf
In the configuration file, add the following line, replacing your-smtp.com
with the SMTP server domain name provided by your ISP. Assuming the SMTP server does not require a username and password for authentication when sending emails:
relayhost = your-smtp.com:25
After making any changes to the Postfix configuration file (main.cf
), you’ll need to restart the Postfix service for the changes to take effect. You can do this by running the following command in the Ubuntu terminal:
sudo systemctl restart postfix
Now, let’s see how you can send the public IP address via email. To do this, we’ll create a Bash script named email_publicIP.sh
.
#!/bin/bash
# Get the public IP address
public_ip=$(curl -s https://api.ipify.org)
# Compose the email
echo "Hello" | mail -s "Your public IP address: $public_ip" -r abc@123.com admin@123.com
The message after the echo
command is the content of the email. This message is passed to the mail
command using a pipeline. The message after the -s
option is the subject of the email. The email address after the -r
option is the sender, and the one at the end is the receiver.
Go ahead and run the Bash script, and check if there are any error messages in the response. The -r
parameter is very important – without it, the sender email will use user@hostname
of the Ubuntu as the sender, and the SMTP server will check if the hostname is a valid domain. The SMTP server may refuse to send the email if it cannot find the domain on the internet.
If there are any error messages after executing the command, check the Postfix logs for any error messages or clues about the issue. You can view the logs saved in /var/log/mail.log
.
To run this Bash script every hour, you can add the bash
command into the crontab scheduler. Open your crontab editor by running crontab -e
in the terminal. Then, add the following line:
0 * * * * bash /usr/local/vpnserver/email_publicIP.sh
This will run the email_publicIP.sh
script every hour on the hour (0 minutes past the hour) and send the output as an email to the specified email account.
In an upcoming blog post, I will use the method described here to obtain the public IP address of a router. This will allow me to access a VPN server that is located behind the Network Address Translation (NAT) of that router.