Lukasz Boryn c12604ebb4 feat(template): publish the Mithlond self-hosted Gitea template
A spare-machine Gitea forge, hardened, backed up and with CI, as a template.
Personal values are placeholders listed in the README.
2026-10-16 15:04:18 +02:00

251 lines
8.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
type: Guide
purpose: Keep the A records on the home IP with ddclient.
answers:
- "How do the DNS records follow a changing home IP?"
---
# 05 — DDNS (ddclient)
Keep the A records from [04](04-dns-records.md) on the home IP as it changes.
## 1. Configure ddclient
The config lives in a directory, not at `/etc/ddclient.conf`:
```bash
sudo vim /etc/ddclient/ddclient.conf
```
Replace entire contents with:
```conf
syslog=yes
ssl=yes
protocol=cloudflare
usev4=webv4
webv4=ipify-ipv4
login=token
password=YOUR_CLOUDFLARE_API_TOKEN
zone=example.com
ttl=1
git.example.com,example.com
```
Replace `YOUR_CLOUDFLARE_API_TOKEN` with the token from [04](04-dns-records.md) step 2. The domain (`example.com`) and the hostnames are already filled in.
The final line is a comma-separated host list, no spaces — the forge and the
portfolio apex, both updated in one run. Both records must already exist in
Cloudflare ([04](04-dns-records.md) step 1); ddclient edits records, it does not create them. If the
apex is missing here, `git.` keeps updating happily and the portfolio record
silently goes stale at the next IP change, which presents as a Caddy or Docker
fault and is neither.
Three things about this config that differ from most guides you will find:
- **`usev4=webv4`, not `use=web`.** ddclient 4.0 deprecated the `use=`/`web=`
pair in favour of address-family-explicit options. The old spelling still
parses but logs a deprecation warning on every run.
- **`webv4=ipify-ipv4`, not `ifconfig.me`.** `webv4` accepts either a built-in
provider name or a full URL. `ifconfig.me` is neither — it is not in ddclient's
built-in list, and as a URL it is missing its scheme. `ipify-ipv4` is built in
(`https://api.ipify.org/`) and returns a bare IP with no scraping.
- **No `daemon=`.** The packaged unit hardcodes `--daemon 5m` on the command
line, which overrides anything the config file says. Setting `daemon=60` here
looks like it works and silently does nothing — see step 2.
`pid=` is also omitted: the unit runs ddclient with `--foreground` under
`Type=exec`, so systemd tracks the process directly and no pidfile is used.
Secure the file (contains API token):
```bash
sudo chmod 600 /etc/ddclient/ddclient.conf
sudo chown root:root /etc/ddclient/ddclient.conf
```
The unit declares no `User=`, so ddclient runs as root and can read a
root-owned `0600` file. The package ships this file as `root:ddclient 0640`;
tightening it to `0600 root:root` removes the group read without breaking
anything.
## 2. Set the polling interval
The packaged unit passes `--daemon 5m`, and a command-line option beats the
config file. Left alone, ddclient notices a new IP up to 5 minutes late — so
`ttl=1` on the DNS record buys you nothing. Override the interval where it is
actually read, with a drop-in:
```bash
sudo systemctl edit ddclient
```
Add:
```ini
[Service]
Environment=daemon_interval=60
```
A drop-in is used here for the same reason as
[`99-hardening.conf` in 03](03-ssh-hardening.md) — it survives package upgrades
and keeps the change visible in a diff, rather than editing a vendor unit in
place.
Confirm it took effect:
```bash
systemctl show ddclient -p Environment # expect: daemon_interval=60
```
Check `Environment`, **not** `ExecStart`. The vendor unit is:
```ini
Environment=daemon_interval=5m
ExecStart=/usr/bin/ddclient --daemon ${daemon_interval} --foreground
```
`systemctl show -p ExecStart` prints argv with variables **unexpanded**, so it
reports `--daemon ${daemon_interval}` forever, no matter what the drop-in sets.
Reading `ExecStart` here looks like the override silently failed when it is in
fact working.
> **Daily IP change.** Many residential ISPs force a reconnect every 24 hours, which assigns a new public IP. If your router is also set to restart nightly (e.g. 3 am), expect a short outage (~1–2 min for the router, plus up to 1 min for ddclient to detect the new IP and Cloudflare's 1-min TTL to expire) — roughly 3–4 min of nightly downtime. The 60-second interval set above keeps that window small; at the stock 5 minutes it is closer to 7–8 min. No manual action needed: ddclient re-points the A record automatically. The server stays powered on through a router reboot and reconnects via DHCP.
## 3. Enable and start ddclient
```bash
sudo systemctl enable ddclient
sudo systemctl start ddclient
```
Check status:
```bash
sudo systemctl status ddclient
sudo journalctl -u ddclient -n 20
```
Successful update log shows:
```
SUCCESS: [cloudflare][git.example.com]> IPv4 address set to <your IP>
```
### After any failed attempt, clear the cache before retrying
ddclient records failures in `/var/cache/ddclient/ddclient.cache` and then
refuses to retry that host for 5 minutes:
```
skipped update from <nothing> to 87.156.0.132 because it has been less than
5 minutes since the previous update attempt ..., which failed
```
A `systemctl restart` does not clear this — the cooldown lives in the cache file,
not in the process. So a config fix appears not to work, and the log shows the
*old* error being skipped rather than the new attempt. Wipe the cache instead:
```bash
sudo systemctl stop ddclient
sudo rm -f /var/cache/ddclient/ddclient.cache
sudo systemctl start ddclient
sleep 8
sudo journalctl -u ddclient -n 10 --no-pager
```
The cache is pure derived state; ddclient rebuilds it on the next run. Deleting
it also clears stale `wildcard` / `backupmx` recap warnings left behind when an
earlier run used a different protocol.
### `no zone ID found for zone <name>`
**Check the spelling in `zone=` first.** ddclient echoes the zone name back from
your config, so a typo (`exmaple.com`) produces an error that reads exactly
like a permissions problem. Only once the name is verified character-by-character
should you suspect token scope — and widen the token then, not pre-emptively.
Audit the whole file at once, with the secret masked, rather than one restart per
typo:
```bash
sudo sed 's/^password=.*/password=REDACTED/' /etc/ddclient/ddclient.conf
```
### `badauth: Bad authorization (username or password)` from `[dyndns2]`
The protocol tag in brackets is the tell. If it says `dyndns2` rather than
`cloudflare`, `protocol=` failed to parse (e.g. `claudflare`) and ddclient fell
back to its default protocol, which then failed to authenticate. This is a typo,
not a credential problem. Look further up the journal for the real cause:
```
WARNING: [file /etc/ddclient/ddclient.conf, line 4][globals][protocol]>
invalid value 'claudflare': invalid protocol: claudflare
```
An invalid `protocol=` is a warning, not a fatal error — ddclient keeps running
and reports a misleading auth failure downstream.
## 4. Test DNS resolution
From any machine:
```bash
dig git.example.com +short
# should return your home IP
```
`dig` is not installed on the dev machine and is not in a default Arch install
(`pacman -S bind`). Without it, either:
```bash
resolvectl query git.example.com
```
or, to bypass the local resolver cache entirely:
```bash
curl -s -H 'accept: application/dns-json' \
'https://cloudflare-dns.com/dns-query?name=git.example.com&type=A'
```
Prefer the DoH query right after creating the record. `systemd-resolved`
negative-caches an `NXDOMAIN` from before the record existed, so `resolvectl` can
report `not found` for minutes after the record is live and resolving worldwide.
DNS propagation can take up to 5 minutes on first setup.
## 5. Confirm ddclient can actually change the record
A `SUCCESS` on first run proves ddclient authenticated — not that it can rewrite
the record. If the A record already held the correct IP (likely, since you just
created it), the update was a no-op and Cloudflare's `modified_on` stays at the
creation time.
The real proof arrives with the next IP change. If your ISP forces a daily
reconnect, it happens within 24 hours, so check the following day:
```bash
sudo journalctl -u ddclient --since yesterday | grep SUCCESS
```
Expect a second `SUCCESS` line carrying a *different* address. Until you have
seen that, treat write access as unverified.
## Checklist
- [ ] ddclient host line lists both `git.example.com` and `example.com`
- [ ] TTL set to 1 minute
- [ ] `/etc/ddclient/ddclient.conf` is `0600 root:root` and holds the real token
- [ ] Token exists nowhere except that file (not in chat, shell history, or this repo)
- [ ] `systemctl show ddclient -p Environment` shows `daemon_interval=60`
- [ ] ddclient enabled and active, `SUCCESS` in logs tagged `[cloudflare]`
- [ ] `git.example.com` resolves to the correct IP from outside the local resolver cache
- [ ] Second `SUCCESS` observed after an IP change (proves write, not just auth)
## Next step
Continue with [06-docker-deploy.md](06-docker-deploy.md).