・certbotでサーバー証明書を取得してSSLを利用した安全なメールサーバーを構築する方法を知りたい。
・具体的な手順を分かりやすく教えてほしい。
こういった疑問に答えます。
本記事の内容
この記事を書いている私は、某SIerに勤務しながら、
WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。
こういった私が、解説していきます。
私が実機で試したコマンドや画像を載せて書いています。
記事の信頼性担保に繋がると思います。
- 本記事の内容
- certbotでサーバー証明書を取得してSSLを利用した安全なメールサーバーを構築
certbotでサーバー証明書を取得してSSLを利用した安全なメールサーバーを構築
事前確認① ホスト名からIPアドレスの名前解決
[root@SV-VEGA ~]# host sv-vega.planet.space.com sv-vega.planet.space.com has address XXX.XXX.XXX.XXX
手順② Let’s Encrypt を利用したサーバー証明書の取得
certbot certonly --standalone -d ドメイン
[root@SV-ALTAIR ~]# certbot certonly --standalone -d sv-vega.planet.space.com Saving debug log to /var/log/letsencrypt/letsencrypt.log Requesting a certificate for sv-vega.planet.space.com Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/sv-vega.planet.space.com/fullchain.pem Key is saved at: /etc/letsencrypt/live/sv-vega.planet.space.com/privkey.pem This certificate expires on 2023-12-04. These files will be updated when the certificate renews. Certbot has set up a scheduled task to automatically renew this certificate in the background. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
取得したサーバー証明書を、メールサーバーの所定のディレクトリに配置します。
手順③ Postfixのmain.cfの編集
vi /etc/postfix/main.cf
752 ### SSL/TLS Settings ←★★追記
753 smtp_tls_security_level = may ←★★追記
754 smtpd_tls_cert_file = /etc/letsencrypt/live/sv-vega.planet.space.com/fullchain.pem ←★★追記
755 smtpd_tls_key_file = /etc/letsencrypt/live/sv-vega.planet.space.com/privkey.pem ←★★追記
756 smtpd_tls_session_cache_database = btree:/var/lib/postfix/smtpd_scache ←★★追記
757 smtpd_tls_session_cache_timeout = 3600s ←★★追記
758 smtpd_tls_received_header = yes ←★★追記
759 smtpd_tls_loglevel = 1 ←★★追記
手順④ Postfixのmaster.cfの編集
vi /etc/postfix/master.cf
smtps inet n - n - - smtpd ←★★コメント解除して有効化 # -o syslog_name=postfix/smtps -o smtpd_tls_wrappermode=yes ←★★コメント解除して有効化 -o smtpd_sasl_auth_enable=yes ←★★コメント解除して有効化 # -o smtpd_reject_unlisted_recipient=no # -o smtpd_client_restrictions=$mua_client_restrictions # -o smtpd_helo_restrictions=$mua_helo_restrictions # -o smtpd_sender_restrictions=$mua_sender_restrictions # -o smtpd_recipient_restrictions= -o smtpd_relay_restrictions=permit_sasl_authenticated,reject ←★★コメント解除して有効化 # -o milter_macro_daemon_name=ORIGINATING
708 # The full pathname of a file with the Postfix SMTP server RSA certificate 709 # in PEM format. Intermediate certificates should be included in general, 710 # the server certificate first, then the issuing CA(s) (bottom-up order). 711 # 712 #smtpd_tls_cert_file = /etc/pki/tls/certs/postfix.pem ←★★コメントで無効化 713 714 # The full pathname of a file with the Postfix SMTP server RSA private key 715 # in PEM format. The private key must be accessible without a pass-phrase, 716 # i.e. it must not be encrypted. 717 # 718 #smtpd_tls_key_file = /etc/pki/tls/private/postfix.key ←★★コメントで無効化 719 720 # Announce STARTTLS support to remote SMTP clients, but do not require that 721 # clients use TLS encryption (opportunistic TLS inbound). 722 # 723 #smtpd_tls_security_level = may ←★★コメントで無効化
手順⑤ Postfixを再起動して変更を反映
[root@SV-VEGA ~]# postconf -n [root@SV-VEGA ~]# postfix check
systemctl restart postfix
systemctl status postfix
[root@SV-VEGA ~]# systemctl restart postfix [root@SV-VEGA ~]# systemctl status postfix ● postfix.service - Postfix Mail Transport Agent Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2023-09-05 11:27:09 JST; 5s ago Process: 11504 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS) Process: 11526 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS) Process: 11524 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS) Process: 11521 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS) Process: 11518 ExecStartPre=/usr/sbin/restorecon -R /var/spool/postfix/pid/master.pid (code=exite> Main PID: 11594 (master) Tasks: 3 (limit: 24822) Memory: 4.3M CGroup: /system.slice/postfix.service tq11594 /usr/libexec/postfix/master -w tq11595 pickup -l -t unix -u mq11596 qmgr -l -t unix -u 9月 05 11:27:09 SV-VEGA systemd[1]: postfix.service: Succeeded. 9月 05 11:27:09 SV-VEGA systemd[1]: Stopped Postfix Mail Transport Agent. 9月 05 11:27:09 SV-VEGA systemd[1]: Starting Postfix Mail Transport Agent... 9月 05 11:27:09 SV-VEGA postfix/master[11594]: daemon started -- version 3.5.8, configuration /etc> 9月 05 11:27:09 SV-VEGA systemd[1]: Started Postfix Mail Transport Agent.
SMTPS(650番)ポートがLISTENであることを確認
netstat -nat
[root@SV-VEGA ~]# netstat -nat Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:110 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:143 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:465 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:587 0.0.0.0:* LISTEN tcp6 0 0 :::110 :::* LISTEN tcp6 0 0 :::143 :::* LISTEN
手順⑥ Dovecotの10-ssl.confの編集
vi /etc/dovecot/conf.d/10-ssl.conf
ssl
2 ## SSL settings 3 ## 4 5 # SSL/TLS support: yes, no, required. <doc/wiki/SSL.txt> 6 # disable plain pop3 and imap, allowed are only pop3+TLS, pop3s, imap+TLS and imaps 7 # plain imap and pop3 are still allowed for local connections 8 ssl = required ←★★変更
ssl_cert, ssl_key
10 # PEM encoded X.509 SSL/TLS certificate and private key. They're opened before 11 # dropping root privileges, so keep the key file unreadable by anyone but 12 # root. Included doc/mkcert.sh can be used to easily generate self-signed 13 # certificate, just make sure to update the domains in dovecot-openssl.cnf 14 ssl_cert = </etc/letsencrypt/live/sv-vega.planet.space.com/fullchain.pem ←★★変更 15 ssl_key = </etc/letsencrypt/live/sv-vega.planet.space.com/privkey.pem ←★★変更
手順⑦ Dovecotの10-master.confの編集
vi /etc/dovecot/conf.d/10-master.conf
17 service imap-login { 18 inet_listener imap { 19 port = 0 ←★★変更 20 } 21 inet_listener imaps { 22 port = 993 ←★★コメント解除して有効化 23 ssl = yes ←★★コメント解除して有効化 24 }
38 service pop3-login { 39 inet_listener pop3 { 40 port = 0 ←★★変更 41 } 42 inet_listener pop3s { 43 port = 995 ←★★コメント解除して有効化 44 ssl = yes ←★★コメント解除して有効化 45 } 46 }
手順⑧ Dovecotを再起動して変更を反映
[root@SV-VEGA ~]# doveconf -n
[root@SV-VEGA ~]# systemctl restart dovecot [root@SV-VEGA ~]# systemctl status dovecot ● dovecot.service - Dovecot IMAP/POP3 email server Loaded: loaded (/usr/lib/systemd/system/dovecot.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2023-09-05 11:40:35 JST; 6s ago Docs: man:dovecot(1) https://doc.dovecot.org/ Process: 11676 ExecStop=/usr/bin/doveadm stop (code=exited, status=0/SUCCESS) Process: 11680 ExecStartPre=/usr/libexec/dovecot/prestartscript (code=exited, status=0/SUCCESS) Main PID: 11688 (dovecot) Tasks: 4 (limit: 24822) Memory: 4.7M CGroup: /system.slice/dovecot.service tq11688 /usr/sbin/dovecot -F tq11689 dovecot/anvil tq11690 dovecot/log mq11691 dovecot/config 9月 05 11:40:35 SV-VEGA systemd[1]: dovecot.service: Succeeded. 9月 05 11:40:35 SV-VEGA systemd[1]: Stopped Dovecot IMAP/POP3 email server. 9月 05 11:40:35 SV-VEGA systemd[1]: Starting Dovecot IMAP/POP3 email server... 9月 05 11:40:35 SV-VEGA systemd[1]: Started Dovecot IMAP/POP3 email server. 9月 05 11:40:35 SV-VEGA dovecot[11688]: master: Dovecot v2.3.16 (7e2e900c1a) starting up for imap,>
POP3S(995番)、IMAPS(993番)ポートがLISTENであることを確認
netstat -nat
[root@SV-VEGA ~]# netstat -nat Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:465 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:993 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:995 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:587 0.0.0.0:* LISTEN tcp6 0 0 :::993 :::* LISTEN tcp6 0 0 :::995 :::* LISTEN
手順⑨ firewallに許可サービスを追加
smtps,imaps,pop3s
[root@SV-VEGA ~]# firewall-cmd --add-service={smtps,imaps,pop3s} --zone=public --permanent success [root@SV-VEGA ~]# firewall-cmd --reload success
public (active) target: default icmp-block-inversion: no interfaces: ens18 sources: services: dhcpv6-client imap imaps pop3 pop3s smtp smtp-submission smtps ports: protocols: forward: no masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
手順⑩ メールソフトで接続確認
POP3S(995)とSMTPS(465)
IMAPS(993)とSMTPS(465)