在 Linux 的 CLI 下要檢示 TLS/SSL 憑證的到期日, 可以用 OpenSSL client 完成。指令的格式如下:
1 |
$ echo | openssl s_client -connect server_name:port_no | openssl x509 -noout -dates |
把上面的 server_name 換成主機的位置, port_no 換成埠號.
例如要檢查 www.opencli.com 的憑證到期日, 便用以下指令:
1 |
$ echo | openssl s_client -connect www.opencli.com:443 | openssl x509 -noout -dates |
會得出以下結果:
depth=2 O = Digital Signature Trust Co., CN = DST Root CA X3
verify return:1
depth=1 C = US, O = Let’s Encrypt, CN = R3
verify return:1
depth=0 CN = www.opencli.com
verify return:1
DONE
notBefore=Jan 30 21:07:04 2021 GMT
notAfter=Apr 30 21:07:04 2021 GMT
verify return:1
depth=1 C = US, O = Let’s Encrypt, CN = R3
verify return:1
depth=0 CN = www.opencli.com
verify return:1
DONE
notBefore=Jan 30 21:07:04 2021 GMT
notAfter=Apr 30 21:07:04 2021 GMT
如果要檢視在本機憑證的到期日, 可以直接輸入憑證的檔案檢視, 例如要檢查 Let’s Encrypt 憑證的到期日, 可以這樣:
$ openssl x509 -enddate -noout -in /etc/letsencrypt/live/www.opencli.com/cert.pem
或
$ openssl x509 -enddate -noout -in /etc/letsencrypt/live/www.opencli.com/fullchain.pem
如果配合 -checkend 參數, 還可以檢查憑證在未來一段時間後是否有效, 例如想檢查憑證在 7 天 (604800 秒) 後是否仍然有效, 可以這樣:
$ openssl x509 -enddate -noout -in /etc/letsencrypt/live/www.opencli.com/cert.pem -checkend 604800
如果憑證在 7 天後仍然有效, 會回傳以下訊息:
Certificate will not expire