如何安排Let's Encrypt certbot在cron中自动续订我的证书?

Cha*_*ell 27 cron lets-encrypt

我见过冲突的建议.来自eff.org文档:

如果您正在设置cron或systemd作业,我们建议您每天运行两次...请在一小时内为续订任务选择一个随机分钟.

我也看到了每周工作的建议.

我不是cron专家,所以我更喜欢回答一下设置cron作业的详细步骤.

V-Q*_*YEN 32

我最近(2018年4月)在Ubuntu 16.04服务器上安装并运行了certbot(版本0.22.2),并在/etc/cron.d/certbot中自动创建了续订cron作业.

这是创建的cron作业:

# /etc/cron.d/certbot: crontab entries for the certbot package
#
# Upstream recommends attempting renewal twice a day
#
# Eventually, this will be an opportunity to validate certificates
# haven't been revoked, etc.  Renewal will only occur if expiration
# is within 30 days.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew
Run Code Online (Sandbox Code Playgroud)

在进行新的Cron工作之前请检查一下.

  • 值得注意的是,如果存在`/ run/systemd/system`,上面的cron作业**将不会运行`certbot renew` - 这是因为系统计时器正在运行certbot - [了解更多关于certbot和systemd timers here](https://community.letsencrypt.org/t/cerbot-cron-job/23895/5). (6认同)
  • 非常相关的答案.`certbot renew`命令(从certbot 0.10.0开始提供)解决了许多旧的`certbot-auto` hastles.有关详细信息,请参阅[Certbot docs](https://certbot.eff.org/docs/using.html#renewing-certificates). (2认同)

Cha*_*ell 30

所以我决定安排它每天运行一次.首先,我测试了自动续订,因为文档推荐:

sudo letsencrypt renew --dry-run --agree-tos
Run Code Online (Sandbox Code Playgroud)

然后我更新了crontab:

sudo crontab -e
Run Code Online (Sandbox Code Playgroud)

这是我添加的行:

12 3 * * *   letsencrypt renew >> /var/log/letsencrypt/renew.log
Run Code Online (Sandbox Code Playgroud)

这是在凌晨3:12进行的续订.我认为文档建议"在一小时内随机分钟"来分配续订服务器上的负载.所以我认为除了0,15,30或45之外的任何东西都是首选.

我考虑随机化在cron设置中的分钟,就像Jenkins允许你做的那样,但这看起来过于复杂.

最后,我使用sudo bash测试了cron命令:

0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && /usr/local/bin/certbot-auto renew
Run Code Online (Sandbox Code Playgroud)

  • 考虑升级到`certbot`,以便在证书续订成功时自动重新加载Web服务器.即`certbot renew --renew-hook'service nginx reload'`. (8认同)

dan*_*r89 18

在Debian Jessie及更高版本(包括Ubuntu)中,不执行Cron来续订Certbot。而是使用systemd计时器。查看计时器:/lib/systemd/system/certbot.timer

此计时器运行以下服务: /lib/systemd/system/certbot.service

其中包含:

[Service]
Type=oneshot
ExecStart=/usr/bin/certbot -q renew
PrivateTmp=true
Run Code Online (Sandbox Code Playgroud)

为了列出所有计时器,请在终端中执行以下命令:

systemctl list-timers

希望Certbot参与其中:

Mon 2019-02-04 08:38:45 CET 9h离开Sun 2019-02-03 15:25:41 CET 8h之前certbot.timer certbot.service

更新:

由于投了反对票。我将添加如何在基于Debian的发行版上安装Certbot(取决于您的Linux发行版,它可能会有所不同)。

但是例如在Debian Stretch中,您可以安装certbotvia 的反向端口软件包:

sudo apt-get install certbot -t stretch-backports

这将自动为您安装上面显示的文件!从而自动为您安排certbot计时器,该计时器将运行服务,并再次运行续订。

始终可以通过以下方式手动运行续订:

sudo /usr/bin/certbot renew

可以通过--force-renewal标志强制。有关更多信息,请参见续订帮助文本:

/usr/bin/certbot --help renew

归档certbot软件包的一部分(包括但不限于):

dpkg-query -L certbot
...
/lib/systemd/system/certbot.service
/lib/systemd/system/certbot.timer
...
Run Code Online (Sandbox Code Playgroud)

  • 如果您是Raspberry PI用户,*这*是您的最佳选择,请不要与crontab混为一谈。 (2认同)
  • 这篇文章值得比它所得到的更多的荣誉。对于大量使用 Debian、Ubuntu 等的人来说,不需要设置自己的 cron 并且“它只是更新”,这是一件大事。 (2认同)

kub*_*b1x 8

好的。因此,在使用 systemd 的 Debian(或 Ubuntu)上,我可能遇到了和其他人一样的问题 - cron 作业没有启动。我需要做一些其他地方没有提到的额外步骤和观察,因此为此做出单独的回答。


就我而言,该/etc/systemd/system/目录存在,因此工作/etc/cron.d/certbot在初始测试时停止。

/etc/systemd/system/certbot.timer一个指向 的指针/dev/null。这意味着它是一个掩码计时器。当我这样做时systemd unmask certbot.timer,链接被删除,但我没有任何东西可以替换它(尝试过locate certbot.timer,但我的系统上没有安装任何东西)。我仍然可以在 中看到计时器systemd list-timers --all,但它是一个空文件,因此也使用systemd disable certbot.timer. 服务/etc/systemd/system/certbot.service完全没有

因此,在实际清理所有与 certbot 相关的内容之后,/etc/systemd/system/手动创建了必要的文件

# /etc/systemd/system/certbot-renewal.service
[Unit]
Description=Certbot Renewal
[Service]
ExecStart=/usr/local/bin/certbot -q renew --post-hook "systemctl reload nginx"
Run Code Online (Sandbox Code Playgroud)
# /etc/systemd/system/certbot-renewal.timer
[Unit]
Description=Run certbot twice daily

[Timer]
OnCalendar=*-*-* 00,12:00:00
RandomizedDelaySec=43200
Persistent=true

[Install]
WantedBy=timers.target
Run Code Online (Sandbox Code Playgroud)

定时器文件内容来自这个答案

我开始运行并检查整个事情:

sudo systemctl start certbot-renewal.timer
sudo systemctl enable certbot-renewal.timer
sudo systemctl list-timers --all
sudo journalctl -u certbot-renewal.service
Run Code Online (Sandbox Code Playgroud)

还有几点注意事项:

  • 我有 certbot/usr/local/bin/certbot而不是/usr/bin/certbot(用 计算which certbot),不知道为什么。
  • 我正在使用 nginx,因此需要在 post-hook 中重新加载它以考虑更新的证书。