Update Google Dynamic DNS With Raspberry PI
Introduction
Have a google domain and want to automatically update a dynamic DNS synthetic record with a Raspberyy Pi? This page is for you!
If you want more information on dynamic DNS it can be googled or check out this page:
https://stevessmarthomeguide.com/dynamic-dns/
In short, it is usually used to update a DNS record with an IP address that changes (like a home or small office IP address), hence dynamic DNS.
Add A Google Dynamic DNS Record
First, add a dynamic DNS record in your Google Domain by following this guide: https://support.google.com/domains/answer/6147083?hl=en
At the time of writing, the documentation was wrong and the dynamic DNS option was moved to the "Synthetic Records" section:
3. Select "Dynamic DNS" in the dropdown and create a host in the "Subdomain" box. This would be "myhome" and click "Add".
4. The full hostname will look like "myhome.mydomain.com".
5. A new record will be made with a dropdown arrow that exposes a username/password and other information about the record. The username and password is used as an authentication mechanism for updating the record.
Update The Dynamic Record Via Raspberry Pi
Google documents a few options for updating this record via an API here:
In short, what is being done is an HTTP POST request sent to the API with certain parameters to update the Dynamic DNS record. To do this, a few things are needed:
A Raspberry Pi running on a local network.
The username / password from the created record.
Login to your Raspberry Pi.
To POST to the API, "cURL" can be used for this. cURL is a small program that makes it easy to send HTTP requests.
curl -X POST https://<USERNAME>:<PASSWORD>@domains.google.com/nic/update?hostname=<YOUR DYNAMIC DNS RECORD>
curl -X POST https://abcd1234qwerty:derpderpderp@domains.google.com/nic/update?hostname=test.themikekyle.com
If the test was successfull it will show "good <IP ADDRESS>". The IP address is the address in which the request came from. In most instances this is the IP of the person's home router. If there was any result besides "good" then something likely went wrong. If you POST again, it will show "nochg" which means the IP did not change, however it is still a success.
The google doumentation outlines the result codes and any errors which may show up.
Refresh the browser with the Google Domains tab and check the synthetic Dynamic DNS record, the IP address should be updated with a new value. If it still shows 0.0.0.0 something is wrong. It is now possible to resolve this record to an IP address. Try it with:
ping myhome.mydomain.com
Automate The Process
Next is to create a cron job in the Raspoberry Pi so it runs at a predetermined interval.
Open crontab. It may ask what editor to use the first time.
crontab -e
Add a new entry at the bottom to run the curl command from earlier.
3. The " 0 * * * * " in the front of the entry indicates to run the script at every 0 mnute, or every hour rather. There are many flexible schedule options cron can run. It would also be possible to test the new entry by running every minute first.
*/1 * * * * (this would be every minute)
Save and Exit.
4. Check the new entry with:
crcontab -l
5. Check logs to ensure it ran:
cat /var/log/syslog
Done! Feel free to leave feedback below if this helped.