Idempotent ACME Certificate Renewals with systemd Timers
Automated TLS renewal rarely fails on day one; it fails eighty days later because a new certificate lineage was added without updating the renewal unit, or because a service needs a file copy with restricted group permissions rather than direct access to /etc/letsencrypt/live.
1. Separate Validation from Service Reloads
Using authenticator = webroot with a dedicated directory such as /var/lib/codex-acme keeps ACME HTTP-01 challenges isolated from application roots. Port 80 only needs a minimal server block serving /.well-known/acme-challenge/ while returning 404 everywhere else.
2. Atomic Deploy Hooks
When a non-root daemon consumes TLS keys, copying certificates with install(1) inside /etc/letsencrypt/renewal-hooks/deploy/ preserves exact ownership and permissions:
#!/usr/bin/env bash
set -euo pipefail
if [[ "${RENEWED_LINEAGE:-}" == /etc/letsencrypt/live/site-main ]]; then
install -o root -g tls-readers -m 0644 "$RENEWED_LINEAGE/fullchain.pem" /etc/app/tls/fullchain.pem
install -o root -g tls-readers -m 0640 "$RENEWED_LINEAGE/privkey.pem" /etc/app/tls/privkey.pem
fi
/usr/sbin/nginx -t
/usr/bin/systemctl reload nginx
3. Always Test with --dry-run and Check ARI
Modern certbot versions track Let’s Encrypt ACME Renewal Information (ARI) timestamps directly inside /etc/letsencrypt/renewal/*.conf. Whenever you add a new certificate or modify a systemd drop-in, run both a non-sleeping dry run and a quick grep for ari_retry_after across all lineages:
certbot renew --dry-run --no-random-sleep-on-renew
grep -H '^ari_retry_after' /etc/letsencrypt/renewal/*.conf
If any lineage lacks an ARI entry after the timer fires, it was skipped by an explicit --cert-name filter.