Certbot fails to issue TLS certs with Cloudflare DNS
Certbot and Cloudflare DNS is a classic pairing for self-hosted services, but the DNS‑01 challenge often fails silently when the token lacks permissions or environment variables aren’t loaded. As a Lead DevOps, I’ve hit this while setting up Let’s Encrypt for homelab services, and here’s the minimal command cycle that always works when the order is kept.
1. Preparation of CF_API_TOKEN and CF_API_EMAIL env vars
Certbot‑dns‑cloudflare reads CF_API_TOKEN and CF_API_EMAIL from the environment. Without them the plugin cannot authenticate to the API and the challenge fails with an auth error. Export them before running the certificate request or source a .env in your script.
Note: Store the token in a secure store or a variables file loaded by the service system, not hard‑coded in systemd unit files or docker‑compose environment, otherwise key rotation forces container recreation.
2. Choose –dns-cloudflare over –dns-cloudflare-propagation-seconds
The official Certbot plugin for Cloudflare uses the flag --dns-cloudflare. The flag --dns-cloudflare-propagation-seconds is not part of the upstream release and is likely a remnant of a custom script or an old wrapper. Using a non‑existent flag makes Certbot fail to locate the plugin and suggests standard DNS plugins that do not work with Cloudflare.
If you previously used propagation-seconds, simply remove it from the script — the plugin automatically selects a safe propagation interval, and explicit specification often triggers timeouts.
3. Check API token DNS:Edit permissions
The most frequent cause of failure is an under‑privileged Cloudflare token. The minimum required scope is DNS:Edit for your zone. A token with only DNS:Read or without editing capability cannot create the TXT record needed for validation.
| Permission | Required for Certbot DNS-01 |
|---|---|
DNS:Edit | Yes – writes TXT records |
DNS:Read | No – only lists records |
Account:Read | Optional – metadata |
Global API Key | Works if it includes DNS:Edit scope |
Verify rights in the Cloudflare interface: Account → API Tokens. Confirm the selected token is bound to the domain’s zone and the DNS:Edit checkbox is enabled. If using a legacy global key, ensure it has access to the target zone.
4. Debug cf-token-error: Global API Key vs OAuth
The error cf-token-error indicates an incorrect token format or missing scopes. Frequently, users generate an OAuth token during application registration but forget to tick the DNS:Edit box in the creation flow. OAuth tokens follow a strict permission model, and without the explicit DNS:Edit scope the plugin receives 403 Forbidden.
- Global API Key (legacy): Functions when created with
DNS:Editrights for a specific zone. - OAuth token: Requires adding the
DNS:Editscope at application creation time.
After changing the token, reload the environment variables and run certbot again. If the error persists, invoke curl -X POST ... against the Cloudflare API manually to confirm the token can manipulate zone records.
5. Avoid –agree-tos flag in scripts
Certbot must accept the Terms of Service. Without --agree-tos and with closed stdin it enters interactive mode, waiting for user input. In Cron or CI this causes indefinite hanging and job failure.
You can also set the CB_AGREE_TOS=1 environment variable (depending on Certbot version) or configure /etc/letsencrypt/cli.ini with agree_tos = True.
6. Cron setup: –non-interactive –deploy-hook
Automatic renewal in Cron should be friction‑free. --non-interactive disables all prompts, and --deploy-hook reloads the service only after a new certificate is successfully obtained, making the update chain auditable and safe.
The deploy‑hook fires only on certificate change, avoiding unnecessary service restarts on every script invocation.
Always test the one‑liner command manually before placing it in Cron. Mismatched CF_API_EMAIL, incorrect token scope, or a missing zone in the token immediately surface errors in interactive mode, saving you the “why isn’t Cron writing logs?” cycle.
Work with Certbot and Cloudflare DNS becomes routine when you externalize environment setup and permission checks into an automated script. Follow the order: export env, use the correct plugin flag, watch scopes—and TLS certificates issue without unnecessary questions.