Let's Encrypt verifies domain name validity through DNS TXT records
When we use letsencrypt to obtain the free HTTPS certificate, letsencrypt needs to verify the domain name. By default it authenticates like this:
- The
certbotprogram places a file in the root directory of thewebdirectory. - The server of
letsencryptaccesses this file through the domain name to verify that the domain name you applied for belongs to you. But sometimes we want to setHTTPSfor a certain host on the intranet. Because the host on the intranet cannot be accessed by the server ofletsencrypt, an error ofConnection refusedwill occur incertbot --nginx certonly.
In order to solve the above problem, we can change the verification method and use DNS records to verify the domain name.
Use certbot to obtain the certificate
Run the sudo certbot --manual --preferred-challenges dns certonly command, enter the domain name and agree to record the local IP to start obtaining the certificate. Then certbot will pop up the following prompt:
At this time, the certbot program will be paused, waiting for us to add DNS records.
-
Add DNS TXT record After seeing the above prompt, modify the DNS record of the domain name and add a TXT record. The host name is
_acme-challenge, and the content is the random stringIMdfdsfsJDqBRyRaaEgPPQlEuvtxJQAgWZTIVbLuzDi8Ugenerated byletsencrypt. -
Verification successful After adding the
DNSrecord, we can view the content of the domain name throughdig -t txt _acme-challenge.example.com. After the domain name takes effect, press the Enter key in thecertbotprogram and the program will continue to run.letsencryptsuccessfully verified theDNSrecord, and the certificate application was successful.
-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.example.com with the following value:
IMdfdsfsJDqBRyRaaEgPPQlEuvtxJQAgWZTIVbLuzDi8U
Once this is deployed,
-------------------------------------------------------------------------------
Press Enter to Continue
Automatic renewal
Now that the DNS record verification is successful, how to automatically renew? After all, letsencrypt is only for 3 months, and it is tiring to have to renew it manually often.
-
Use the
certboot renewcommand to renew. -
The
--force-renewparameter represents forced SSL certificate renewal -
The
--manual-auth-hookparameter represents a custom verification script. The content of my script here is to update the domain nameDNSrecord, which is the TXT record with the host name_acme-challengementioned above. -
As for how to write the custom script here, you need to look at the
DNSservice provider. EachDNSservice provider is different.
Here is my own update
TXTrecord script, which is only applicable tofreedns,{dns_cookie},{domain_id},{data_id}. Please modify it yourself.
References
certbot renew --force-renew --manual-auth-hook /root/renewdns.sh
#/bin/bash
echo CERTBOT_VALIDATION = ${CERTBOT_VALIDATION}
curl -b "dns_cookie={dns_cookie}" -d "type=TXT" -d "subdomain=_acme-challenge" -d "domain_id={domain_id}" -d "data_id={data_id}" -d "address=%22${CERTBOT_VALIDATION}%22" https://freedns.afraid.org/subdomain/save.php?step=2
sleep 600