This script allows you to automatically update DNS records for your domain/subdomains on Porkbun using their API. It ensures your records are always in sync with your current public IP.
bash <(curl -Ls "https://raw.githubusercontent.com/StafLoker/ddns-porkbun-script/main/install.sh")
To install a specific version, use the following command. For example, to install version v1.0.1
:
VERSION=v1.0.1 && bash <(curl -Ls "https://raw.githubusercontent.com/StafLoker/ddns-porkbun-script/main/install.sh") $VERSION
-
Download the Project:
Clone or download the project from the GitHub repository (replace${VERSION}
with the version of the latest release):sudo mkdir -p /opt/ddns-porkbun-script cd /opt/ddns-porkbun-script sudo wget https://github.com/StafLoker/ddns-porkbun-script/archive/refs/tags/${VERSION}.tar.gz sudo tar -xzvf ${VERSION}.tar.gz sudo rm ${VERSION}.tar.gz
This will download the latest version of the script and extract it into the
/opt/ddns-porkbun-script
directory. -
Install
jq
:
jq
is a lightweight and flexible command-line JSON processor, required to parse API responses.sudo apt install jq
-
Create
keys.env
file:
Store your API keys securely in an environment file. Replacepk
andsk
with your actual API and Secret API keys from Porkbun.echo 'PORKBUN_API_KEY="pk"' > keys.env echo 'PORKBUN_SECRET_API_KEY="sk"' >> keys.env
Make sure to secure this file:
chmod 600 keys.env
-
Configure your domain and subdomains:
Create thedata.json
file to include your domain and subdomains. Example format:{ "domain": "example.com", "concurrency": true, "ipv4": true, "subdomains_type_a": [ "sub1", "sub2" ], "ipv6": false, "subdomains_type_aaaa": [ "sub3", "sub4" ] }
-
Make the script executable:
Ensure the script has executable permissions:chmod +x ddns-porkbun-script.sh
For security reasons, it is recommended to run the script as a dedicated system user. Follow these steps to create the user:
-
Create the user:
Replace/opt/ddns-porkbun-script
with your installation directory.sudo useradd -r -d /opt/ddns-porkbun-script -c "User for the DDNS Porkbun script" ddns-system
-
Set ownership of the installation directory:
Ensure the userddns-system
owns the installation directory.sudo chown -R ddns-system:ddns-system /opt/ddns-porkbun-script
You can automate the script using either cron
or systemd
. Below are instructions for both methods.
-
Open crontab for editing:
crontab -e
-
Add the following entries:
- Run every hour: Updates DNS records every hour.
- Run on system reboot: Ensures DNS updates upon system startup.
# Every 1 hour 0 * * * * /opt/ddns-porkbun-script/ddns-porkbun-script.sh # At system reboot @reboot /opt/ddns-porkbun-script/ddns-porkbun-script.sh
-
Save and exit the crontab editor.
-
Updating
ddns-porkbun-script.sh
for Cron Compatibility
Ensure the script uses absolute paths forkeys.env
anddata.json
:# Load keys source /opt/ddns-porkbun-script/keys.env # Load JSON DATA_FILE="/opt/ddns-porkbun-script/data.json"
-
Verify the Cron job:
Execute the script manually to confirm it works without errors:/opt/ddns-porkbun-script/ddns-porkbun-script.sh
-
Create the systemd service file:
Replace/opt/ddns-porkbun-script
with your installation directory.sudo bash -c "cat > /etc/systemd/system/ddns-porkbun.service <<EOF [Unit] Description=DDNS Porkbun Update Service After=network.target [Service] User=ddns-system ExecStart=/opt/ddns-porkbun-script/ddns-porkbun-script.sh [Install] WantedBy=multi-user.target EOF"
-
Create the systemd timer file:
Replace1h
with your desired execution interval (e.g.,15min
,1h
).sudo bash -c "cat > /etc/systemd/system/ddns-porkbun.timer <<EOF [Unit] Description=Run DDNS Porkbun script every 1h [Timer] OnBootSec=5min OnUnitActiveSec=1h Unit=ddns-porkbun.service [Install] WantedBy=timers.target EOF"
-
Reload systemd and enable the timer:
sudo systemctl daemon-reload sudo systemctl enable --now ddns-porkbun.timer
-
Verify the timer is active:
systemctl list-timers --all
- Environment File: Ensure the
keys.env
file is not accessible to other users on the system. Usechmod 600
to restrict permissions. - Avoid Hardcoding Keys: Use the
source
command to load environment variables securely.
To test the script manually, run:
/opt/ddns-porkbun-script/ddns-porkbun-script.sh
Verify that the DNS records on Porkbun are updated to match your current public IP.
Check logs using journalctl
:
journalctl -t ddns-porkbun | tail
This project is released under the MIT License. See the LICENSE file for more details.