DNS-01 Challenge with Cloudflare and Certbot
certbot certonly --dns-cloudflare. Certbot adds and removes the validation TXT record automatically, so you can get wildcard certificates and issue on servers that are not reachable on port 80.When to use the DNS-01 challenge with Cloudflare
| Situation | HTTP-01 | DNS-01 |
|---|---|---|
| Wildcard certificate (*.example.com) | Not possible | Required |
| Server not reachable on port 80 | Fails | Works |
| Issue before DNS points to the new server | Fails | Works |
| Simple single-site server | Easiest | More setup |
Step 1: create a scoped API token
In the Cloudflare dashboard go to My Profile, API Tokens, then Create Token. Use a custom token with the permission Zone, DNS, Edit, and limit it to the zone you need under Zone Resources. Avoid the legacy Global API Key, since it grants far wider access. Copy the token once, as it is not shown again.
Step 2: store credentials safely
sudo mkdir -p /root/.secrets
sudo nano /root/.secrets/cloudflare.ini
# add this line:
dns_cloudflare_api_token = YOUR_TOKEN_HERE
sudo chmod 600 /root/.secrets/cloudflare.ini
Certbot warns if the file is readable by others. Never commit this file to version control.
Step 3: install the plugin and request the certificate
sudo apt install python3-certbot-dns-cloudflare
sudo certbot certonly \
--dns-cloudflare \
--dns-cloudflare-credentials /root/.secrets/cloudflare.ini \
-d example.com -d "*.example.com"
Certbot creates a _acme-challenge TXT record, waits for it to propagate, validates, and then deletes it. If validation fails because propagation is slow, raise the wait with --dns-cloudflare-propagation-seconds 60.
Use the certificate
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
Renewal reuses the same credentials file. Test with sudo certbot renew --dry-run and add a deploy hook to reload the web server. See renewing without downtime.
Troubleshooting
- "Error determining zone identifier": the token lacks access to that zone, or the domain is not on this Cloudflare account.
- Authentication errors: check you used
dns_cloudflare_api_tokenand not the old email and key fields with a token. - TXT record not found: verify with the DNS lookup using the
_acme-challengename, and extend the propagation wait. - CAA blocks issuance: check with the CAA checker.
- Wildcard does not cover the bare domain: list both names, as shown above.
How DNS-01 challenge validation works with the Cloudflare proxy
DNS-01 works whether or not the proxy (orange cloud) is on, because validation happens through DNS records. A certificate on your origin lets you use Full (Strict) mode. See Let's Encrypt vs Cloudflare SSL for how they fit together, and confirm the result with the SSL checker.
Related reading: Cloudflare Origin certificates, free wildcard certificates and checking that a certificate matches its private key.
Frequently Asked Questions
Is a wildcard certificate safe to use?
It is as safe as your key storage. If the key leaks, every subdomain is affected, so restrict access and avoid copying it to many servers.
Does DNS-01 work with other DNS providers?
Yes. Certbot has plugins for many providers, and some tools use a generic delegation approach.
Why a scoped token and not the Global API Key?
A scoped token can only edit DNS for chosen zones, which limits damage if the file is leaked.
How long are the certificates valid?
Let's Encrypt certificates last 90 days today, 64 days from 10 February 2027 and 45 days from 16 February 2028 (Let's Encrypt announcement). Renewal reuses the same credentials file, and Certbot renews once less than a third of the lifetime is left, which is 30 days on a 90-day certificate, about 21 on a 64-day one and 15 on a 45-day one (Certbot docs).
✅ Post-Installation Verification & SEO Checklist
After applying the steps above, run through this 5-point checklist to prevent browser warnings and protect your Google SEO rankings:
- Verify full CA chain trust and TLS 1.3 support using our Live SSL Checker.
- Scan for Mixed Content HTTP links and check HSTS/CSP headers on Security Headers Checker.
- Enforce a permanent 301 HTTP-to-HTTPS redirect with our HTTPS Redirect Generator.
- Confirm both root and
wwwversions resolve cleanly to one canonical HTTPS URL. - Verify your HTTPS property and
sitemap.xmlinside Google Search Console and Bing Webmaster Tools.