2023-12-02 16:02:53 -05:00
|
|
|
---
|
|
|
|
title: Make Your Own DDNS
|
|
|
|
description: A guide on how to setup a DDNS-like system using Cron and a Deno script.
|
|
|
|
tags:
|
|
|
|
- linux
|
|
|
|
- guide
|
|
|
|
- automation
|
|
|
|
---
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
Ever tried hosting a server in your home network but annoyed at the fact that
|
|
|
|
your public IP address keeps changing? Imagine for a moment that you're running
|
|
|
|
a [Nextcloud](https://nextcloud.com) server located at `nextcloud.example.com`.
|
|
|
|
All of a sudden, the public IP changes and your domain is still pointing to the
|
|
|
|
old IP address. As a result, connectivity to your server gets disrupted. The
|
|
|
|
change can happen at any time and without warning. It can take some time for you
|
|
|
|
to find out. Once you do, you'll have to manually update the
|
|
|
|
[A-record](https://en.wikipedia.org/wiki/List_of_DNS_record_types#A) of
|
|
|
|
`nextcloud.example.com` to restore connectivity.
|
|
|
|
|
|
|
|
As you can see, spontaneous IP address changes are disruptive events; a
|
|
|
|
[static IP address](https://en.wikipedia.org/wiki/IP_address#IP_address_assignment)
|
|
|
|
or a [DDNS](https://en.wikipedia.org/wiki/Dynamic_DNS) service is essential for
|
|
|
|
running public servers. Internet service providers usually offer static IP
|
|
|
|
addresses only in their business plans. So this leaves out the vast majority of
|
|
|
|
internet users. The other option, DDNS, is a feature supported by some routers.
|
|
|
|
However, utilizing this feature typically involves having to sign-up for a
|
|
|
|
hostname on a DDNS provider like [No-IP](https://www.noip.com). If you get one
|
|
|
|
of No-IP's free accounts, then you must
|
|
|
|
[manually confirm hostnames](https://www.noip.com/support/knowledgebase/confirm-my-hostname-free-account-support-question-day)
|
|
|
|
every 30 days.
|
2023-12-02 16:02:53 -05:00
|
|
|
|
|
|
|
So, the options we have thus far require us to either:
|
|
|
|
|
|
|
|
- Pay for a more expensive internet plan.
|
2024-03-05 18:38:42 -05:00
|
|
|
- Utilize a router feature that requires creating a hostname on a third-party
|
|
|
|
DDNS provider.
|
2023-12-02 16:02:53 -05:00
|
|
|
|
|
|
|
If only there was another way...
|
|
|
|
|
|
|
|
## The Solution
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
The answer is to create our own DDNS. We can achieve a DDNS-like system with a
|
|
|
|
[shell script](https://en.wikipedia.org/wiki/Shell_script) that runs every hour;
|
|
|
|
scheduled via [Cron](https://en.wikipedia.org/wiki/Cron). The script performs
|
|
|
|
the following tasks everytime it runs:
|
2023-12-02 16:02:53 -05:00
|
|
|
|
|
|
|
1. Obtain the current public IP address and the domain's A-record.
|
|
|
|
1. Compare the public IP and the A-record's value.
|
2024-03-05 18:38:42 -05:00
|
|
|
1. If they are not the same, it updates the A-record to the value of the public
|
|
|
|
IP address.
|
2023-12-02 16:02:53 -05:00
|
|
|
|
|
|
|
This system provides some major advantages:
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
- Can be deployed on any computer with a modern
|
|
|
|
[Linux](https://en.wikipedia.org/wiki/Linux) installation.
|
2023-12-02 16:02:53 -05:00
|
|
|
- Does not depend on a DDNS provider.
|
|
|
|
- No need for a router with DDNS support.
|
2024-03-05 18:38:42 -05:00
|
|
|
- The script is flexible and can be changed to use another
|
|
|
|
[domain registrar](https://en.wikipedia.org/wiki/Domain_name_registrar).
|
2023-12-02 16:02:53 -05:00
|
|
|
|
|
|
|
## Requirements
|
|
|
|
|
|
|
|
- Basic proficiency with Linux and its command line.
|
2024-03-05 18:38:42 -05:00
|
|
|
- A computer with a [Debian](https://www.debian.org)-based distribution
|
|
|
|
installed.
|
2023-12-02 16:02:53 -05:00
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
This computer will be referred to as the _Host Computer_. It will be online
|
|
|
|
24/7 to keep the scheduled script running.
|
2023-12-02 16:02:53 -05:00
|
|
|
|
|
|
|
- Domain (or subdomain) with its DNS A-record set.
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
Your domain registrar must have an [API](https://en.wikipedia.org/wiki/API)
|
|
|
|
which provides the ability to read and update records. This guide will be
|
|
|
|
using the [GoDaddy API](https://developer.godaddy.com/doc) to update an
|
|
|
|
A-record on [GoDaddy](https://www.godaddy.com).
|
2023-12-02 16:02:53 -05:00
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
- **Optional**: Familiarity with
|
|
|
|
[JavaScript](https://en.wikipedia.org/wiki/JavaScript) or
|
|
|
|
[TypeScript](https://en.wikipedia.org/wiki/TypeScript).
|
2023-12-02 16:02:53 -05:00
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
Having some basic TypeScript knowledge allows you to modify or update the
|
|
|
|
shell script.
|
2023-12-02 16:02:53 -05:00
|
|
|
|
|
|
|
## Preparation
|
|
|
|
|
|
|
|
### Host Computer
|
|
|
|
|
|
|
|
Perform these steps on the Host Computer.
|
|
|
|
|
|
|
|
#### Update the System
|
|
|
|
|
|
|
|
Open a terminal then enter the command below to update the system.
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
```sh
|
2023-12-02 16:02:53 -05:00
|
|
|
sudo apt -y update && sudo apt -y upgrade
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Install Cron
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
Cron is included in most Linux distributions but we will install it just to be
|
|
|
|
sure:
|
2023-12-02 16:02:53 -05:00
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
```sh
|
2023-12-02 16:02:53 -05:00
|
|
|
sudo apt install cron
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Install Deno
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
[Deno](https://deno.com) is a secure runtime environment for JavaScript and
|
|
|
|
TypeScript. It is the environment for which the TypeScript-programmed shell
|
|
|
|
script will be running under.
|
2023-12-02 16:02:53 -05:00
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
At the time of writing, Deno is not available in the
|
|
|
|
[APT](https://en.wikipedia.org/wiki/APT_(software)) repositories; we will be
|
|
|
|
using the installation script instead.
|
2023-12-02 16:02:53 -05:00
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
> Refer to the Deno documentation for
|
|
|
|
> [additional installation options](https://docs.deno.com/runtime/manual/getting_started/installation).
|
|
|
|
> {.info}
|
2023-12-02 16:02:53 -05:00
|
|
|
|
|
|
|
Run the following command to install Deno:
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
```sh
|
2023-12-02 16:02:53 -05:00
|
|
|
curl -fsSL https://deno.land/x/install/install.sh | sh
|
|
|
|
```
|
|
|
|
|
|
|
|
Confirm that Deno is properly installed by checking its version:
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
```sh
|
2023-12-02 16:02:53 -05:00
|
|
|
deno --version
|
|
|
|
```
|
|
|
|
|
|
|
|
An output like the one below indicates that Deno was successfully installed:
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
```txt
|
2023-12-02 16:02:53 -05:00
|
|
|
deno 1.38.3 (release, x86_64-unknown-linux-gnu)
|
|
|
|
v8 12.0.267.1
|
|
|
|
typescript 5.2.2
|
|
|
|
```
|
|
|
|
|
|
|
|
### GoDaddy API Keys
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
Follow the [GoDaddy API setup](https://developer.godaddy.com/getstarted#setup)
|
|
|
|
to create the API secret and key. Take note of the key and secret as they will
|
|
|
|
be needed shortly.
|
2023-12-02 16:02:53 -05:00
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
> Ensure that you get the API secret and key for the **production environment**;
|
|
|
|
> not the ones for the test environment. {.warning}
|
2023-12-02 16:02:53 -05:00
|
|
|
|
2023-12-03 11:23:27 -05:00
|
|
|
## The Script
|
2023-12-02 16:02:53 -05:00
|
|
|
|
|
|
|
> The script sends requests to two endpoints from GoDaddy's Domains API:
|
|
|
|
>
|
|
|
|
> - [GET /v1/domains/{domain}/records/{type}/{name}](https://developer.godaddy.com/doc/endpoint/domains#/v1/recordGet)
|
|
|
|
> - [PUT /v1/domains/{domain}/records/{type}/{name}](https://developer.godaddy.com/doc/endpoint/domains#/v1/recordReplaceTypeName)
|
|
|
|
>
|
2024-03-05 18:38:42 -05:00
|
|
|
> Refer to the
|
|
|
|
> [GoDaddy Domains API documentation](https://developer.godaddy.com/doc/endpoint/domains)
|
|
|
|
> for the complete list of endpoints. {.info}
|
2023-12-02 16:02:53 -05:00
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
> The public IP address is obtained from an instance of
|
|
|
|
> [IpMe](https://code.fosterhangdaan.com/foster/ipme) at
|
|
|
|
> [https://ipme.fosterhangdaan.com](https://ipme.fosterhangdaan.com).
|
|
|
|
> [Ipify](https://www.ipify.org) can be used as an alternative. {.info}
|
2023-12-02 16:02:53 -05:00
|
|
|
|
|
|
|
Here is the content of the script:
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
```ts
|
2023-12-04 08:36:58 -05:00
|
|
|
#!/usr/bin/env -S deno run --allow-net
|
2023-12-02 16:02:53 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2023 Foster Hangdaan <https://www.fosterhangdaan.com>
|
|
|
|
* SPDX-License-Identifier: agpl-3.0-or-later
|
|
|
|
*
|
2023-12-03 18:53:44 -05:00
|
|
|
* A script which synchronizes a GoDaddy A-record with your public IP
|
2023-12-02 16:02:53 -05:00
|
|
|
* address.
|
2023-12-03 18:32:46 -05:00
|
|
|
*/
|
2023-12-02 16:02:53 -05:00
|
|
|
|
2023-12-04 15:08:03 -05:00
|
|
|
// Change this value to your GoDaddy domain.
|
2023-12-02 16:02:53 -05:00
|
|
|
const domain = "example.com";
|
|
|
|
|
|
|
|
// Change this value to your GoDaddy API key.
|
|
|
|
const GODADDY_API_KEY = "key";
|
|
|
|
|
|
|
|
// Change this value to your GoDaddy API secret.
|
|
|
|
const GODADDY_API_SECRET = "secret";
|
|
|
|
|
|
|
|
// If you prefer to use Ipify instead, change this URL to:
|
2023-12-04 10:49:57 -05:00
|
|
|
// https://api.ipify.org
|
2023-12-02 16:02:53 -05:00
|
|
|
const publicIpUrl = "https://ipme.fosterhangdaan.com";
|
|
|
|
|
2023-12-04 15:01:49 -05:00
|
|
|
const domainParts = domain.split(".");
|
|
|
|
const domainRoot = domainParts.slice(-2).join(".");
|
2024-03-05 18:38:42 -05:00
|
|
|
const domainName = domainParts.slice(0, -2).join(".") || "@";
|
2023-12-04 15:01:49 -05:00
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
const goDaddyUrl =
|
|
|
|
`https://api.godaddy.com/v1/domains/${domainRoot}/records/A/${domainName}`;
|
2023-12-02 16:02:53 -05:00
|
|
|
|
|
|
|
const goDaddyRequestHeaders = new Headers({
|
|
|
|
Authorization: `sso-key ${GODADDY_API_KEY}:${GODADDY_API_SECRET}`,
|
|
|
|
});
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
const [publicIpResponse, goDaddyResponse] = await Promise.all([
|
2023-12-02 16:02:53 -05:00
|
|
|
fetch(publicIpUrl),
|
|
|
|
fetch(goDaddyUrl, { headers: goDaddyRequestHeaders }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!publicIpResponse.ok) {
|
|
|
|
throw new Error(`Failed to fetch public IP address from ${publicIpUrl}.`);
|
|
|
|
} else if (!goDaddyResponse.ok) {
|
|
|
|
throw new Error("Failed to fetch A-records from GoDaddy.");
|
|
|
|
}
|
|
|
|
|
|
|
|
const goDaddyJsonResponse = await goDaddyResponse.json();
|
|
|
|
|
|
|
|
if (goDaddyJsonResponse.length === 0) {
|
|
|
|
throw new Error("No GoDaddy A-records found.");
|
|
|
|
}
|
|
|
|
|
|
|
|
const publicIP = await publicIpResponse.text();
|
|
|
|
const goDaddyIP = goDaddyJsonResponse[0]["data"];
|
|
|
|
|
|
|
|
if (publicIP !== goDaddyIP) {
|
2024-03-05 18:38:42 -05:00
|
|
|
console.log(
|
|
|
|
`The public IP address has changed. Public IP: ${publicIP}; Old IP: ${goDaddyIP}`,
|
|
|
|
);
|
2023-12-02 16:02:53 -05:00
|
|
|
goDaddyRequestHeaders.append("Content-Type", "application/json");
|
|
|
|
const response = await fetch(goDaddyUrl, {
|
|
|
|
method: "PUT",
|
|
|
|
headers: goDaddyRequestHeaders,
|
|
|
|
body: JSON.stringify([
|
|
|
|
{
|
|
|
|
data: publicIP,
|
2024-03-05 18:38:42 -05:00
|
|
|
},
|
2023-12-02 16:02:53 -05:00
|
|
|
]),
|
|
|
|
});
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error("Failed to update GoDaddy A-record.");
|
|
|
|
} else {
|
|
|
|
console.log(`GoDaddy A-record successfully updated to ${publicIP}.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Schedule the Script to Run Every Hour with Cron
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
On the Host Computer, save [the script](#the-script) as a file at
|
|
|
|
`/etc/cron.hourly/ddns.ts`. Make sure to replace some necessary values:
|
2023-12-02 16:02:53 -05:00
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
- Replace `example.com` in `const domain = "example.com";` with your GoDaddy
|
|
|
|
domain.
|
2023-12-02 16:02:53 -05:00
|
|
|
- Replace `key` in `const GODADDY_API_KEY = "key";` with your GoDaddy API key.
|
2024-03-05 18:38:42 -05:00
|
|
|
- Replace `secret` in `const GODADDY_API_SECRET = "secret";` with your GoDaddy
|
|
|
|
API secret.
|
2023-12-02 16:02:53 -05:00
|
|
|
|
|
|
|
Grant read, write, and execute permissions only to the `root` user:
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
```sh
|
2023-12-04 08:36:58 -05:00
|
|
|
sudo chown root:root /etc/cron.hourly/ddns.ts
|
|
|
|
sudo chmod 700 /etc/cron.hourly/ddns.ts
|
2023-12-02 16:02:53 -05:00
|
|
|
```
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
> Access to the script should be granted only to authorized users (such as
|
|
|
|
> `root`) since the script contains sensitive information: your GoDaddy API
|
|
|
|
> secret and key. {.warning}
|
2023-12-02 16:02:53 -05:00
|
|
|
|
|
|
|
The script should now be scheduled to run every hour by Cron.
|
|
|
|
|
|
|
|
## Conclusion
|
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
In this guide, we have setup an hourly Cron script that updates a GoDaddy
|
|
|
|
domain's A-record if the public IP address changed. You can now rest assured
|
|
|
|
that your hosted services will be protected from disruptions caused by public IP
|
|
|
|
address changes.
|
2023-12-02 16:02:53 -05:00
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
Feel free to modify the script to your needs; especially if you are using a
|
|
|
|
domain registrar other than GoDaddy.
|
2023-12-02 16:02:53 -05:00
|
|
|
|
2024-03-05 18:38:42 -05:00
|
|
|
As always, [contact me](/#contact-me) for any comments or suggestions regarding
|
|
|
|
this guide.
|