DNS01验证方式手动获取Lets Encrypt 证书

背景

以前用Lets Encrypt获取ssl证书都是用最傻瓜的方式。

1
2
3
4
5
6
7
8
$ sudo letsencrypt certonly
Saving debug log to /var/log/letsencrypt/letsencrypt.log

How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Nginx Web Server plugin (nginx)
2: Spin up a temporary webserver (standalone)
3: Place files in webroot directory (webroot)

通常我会选2,然后就得把nginx先停了,然后跑一遍,然后再启动nginx。
这个其实也不麻烦,但是无论选择哪个选项,都得有一个前置条件就是本机的80端口是开放的。

这个对于vps或者公有云上的一些主机来说根本没什么问题,但是对于藏在家里路由器后面的主机,不想默认使用80端口的话,就略麻烦了。
更何况有些服务提供商禁用80端口。

这个时候,其实Lets Encrypt提供了使用DNS服务器获取证书的方式。

Lets Encrypt 验证主机身份的方式

在获取证书的时候,需要验证想要获取证书的域名的确是本人所有的,这个验证被叫Challenge。
Lets Encrypt提供了好几种验证方式,具体可以查看
Challenge Type

其中,最常见的验证方式就是之前一直用的方式,就是lets Encrypt的客户端(certbot)会在你指定的服务器目录放一个随机生成内容的文件,然后lets Encrypt会通过你指定的域名去访问你放在这个端口的文件,验证里面内容是否正确。
这种验证方式被成为HTTP-01 Challenge。

我们今天使用另外一种方式,DNS-01,手动获取证书,这个方法最大的好处是不需要开放80端口。

DNS-01 方式获取证书

首先,带参数运行certbot。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ sudo certbot certonly --manual \
> --server https://acme-v02.api.letsencrypt.org/directory \
> --preferred-challenges dns \
> -d www.bocchi.tokyo \
> -m gyorou@tjjtds.me \
> --agree-tos \
> --manual-public-ip-logging-ok
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Cert is due for renewal, auto-renewing...
Renewing an existing certificate
Performing the following challenges:
1 ---
dns-01 challenge for www.bocchi.tokyo

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.www.bocchi.tokyo with the following value:

M4v67OXrYU-nXcveB8Oajqavh6ZWY3tLUMqrZTaFjGQ

Before continuing, verify the record is deployed.

按这个指示,我们上dns的服务提供商,新建一个hostname 是_acme-challenge.www.bocchi.tokyo的TXT记录。
里面的内容就是下面这串随机生成的字符串。

过了一会儿,我们验证一下这个DNS记录有没有生效。

1
dig @01.dnsv.jp -t TXT _acme-challenge.www.bocchi.tokyo

其中01.dnsv.jp 是DNS的服务器。

当看到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
~ dig @01.dnsv.jp -t TXT _acme-challenge.www.bocchi.tokyo

; <<>> DiG 9.10.6 <<>> @01.dnsv.jp -t TXT _acme-challenge.www.bocchi.tokyo
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52094
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;_acme-challenge.www.bocchi.tokyo. IN TXT

;; ANSWER SECTION:
_acme-challenge.www.bocchi.tokyo. 3600 IN TXT "M4v67OXrYU-nXcveB8Oajqavh6ZWY3tLUMqrZTaFjGQ"

;; AUTHORITY SECTION:
bocchi.tokyo. 86400 IN NS 01.dnsv.jp.
bocchi.tokyo. 86400 IN NS 02.dnsv.jp.
bocchi.tokyo. 86400 IN NS 03.dnsv.jp.
bocchi.tokyo. 86400 IN NS 04.dnsv.jp.

就知道DNS Record已经生效,这个时候,再按下会车键,Continue,验证就完成了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Press Enter to Continue
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/www.bocchi.tokyo/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/www.bocchi.tokyo/privkey.pem
Your cert will expire on 2022-07-09. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. To non-interactively renew *all* of your certificates, run
"certbot renew"
- If you like Certbot, please consider supporting our work by:

Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le