在Ubuntu上配置邮件服务器的SSL证书,通常需要以下几个步骤:
首先,确保你的Ubuntu系统已经安装了Postfix和OpenSSL。如果没有安装,可以使用以下命令进行安装:
sudo apt update
sudo apt install postfix openssl
如果你只是想快速测试,可以生成一个自签名证书。请注意,自签名证书不会被大多数客户端信任。
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/mail.key -out /etc/ssl/certs/mail.crt
按照提示输入相关信息,例如国家、组织名称等。
对于生产环境,你应该从受信任的证书颁发机构(CA)获取SSL证书。以下是使用Let’s Encrypt获取证书的步骤:
Certbot是一个自动化的工具,可以帮助你获取和续订Let’s Encrypt证书。
sudo apt install certbot python3-certbot-postfix
运行Certbot并选择Postfix作为需要配置的服务:
sudo certbot --postfix -d yourdomain.com
按照提示输入电子邮件地址、同意条款等。Certbot会自动配置Postfix并生成证书。
编辑Postfix的主配置文件/etc/postfix/main.cf
,添加或修改以下行:
smtpd_tls_cert_file=/etc/ssl/certs/mail.crt
smtpd_tls_key_file=/etc/ssl/private/mail.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
确保路径和文件名与你生成的证书路径一致。
保存配置文件后,重启Postfix服务以应用更改:
sudo systemctl restart postfix
你可以使用openssl
命令来验证SSL配置是否正确:
openssl s_client -connect yourdomain.com:587 -starttls smtp
如果一切配置正确,你应该会看到类似以下的输出:
CONNECTED(00000003)
depth=2 O = Let's Encrypt, CN = R3
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=2 O = Let's Encrypt, CN = R3
verify error:num=27:certificate not trusted
verify return:1
depth=2 O = Let's Encrypt, CN = R3
verify error:num=21:unable to verify the first certificate
verify return:1
---
Certificate chain
0 s:yourdomain.com
i:C = US, O = Let's Encrypt, CN = R3
1 s:C = US, O = Let's Encrypt, CN = R3
i:O = Digital Signature Trust Co., CN = DST Root CA X3
---
Server certificate
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
subject=CN=yourdomain.com
issuer=C = US, O = Let's Encrypt, CN = R3
---
No client certificate CA names sent
Peer signing digest: SHA256
Peer signature type: RSA-PSS
Server Temp Key: X25519, 253 bits
---
SSL handshake has read 4096 bytes and written 432 bytes
Verification error: unable to get local issuer certificate
---
New, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384
Server public key is 2048 bit
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
Early data was not sent
Verify return code: 20 (unable to get local issuer certificate)
---
如果看到“SSL handshake has read”和“Server public key is 2048 bit”,说明SSL配置成功。
确保你的防火墙允许SMTP(端口587)和IMAP/POP3(端口143/993)流量:
sudo ufw allow 587/tcp
sudo ufw allow 143/tcp
sudo ufw allow 993/tcp
sudo ufw reload
完成以上步骤后,你的Ubuntu邮件服务器应该已经成功配置了SSL证书。