在Ubuntu上實現郵件加密傳輸,通常涉及以下幾個步驟:
Postfix是一個流行的郵件傳輸代理(MTA),可以用來發送和接收郵件。
sudo apt update
sudo apt install postfix
編輯Postfix的主配置文件 /etc/postfix/main.cf
:
sudo nano /etc/postfix/main.cf
添加或修改以下配置項:
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = all
inet_protocols = ipv4
mydestination = $myhostname, localhost.$mydomain, $mydomain
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
home_mailbox = Maildir/
smtpd_tls_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file = /etc/ssl/private/ssl-cert-snakeoil.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
Dovecot是一個流行的IMAP和POP3服務器,可以用來接收郵件。
sudo apt install dovecot-core dovecot-imapd dovecot-lmtpd
編輯Dovecot的主配置文件 /etc/dovecot/dovecot.conf
:
sudo nano /etc/dovecot/dovecot.conf
確保以下配置項存在并正確:
protocols = imap pop3 lmtp
listen = *
ssl = yes
ssl_cert = </etc/ssl/certs/ssl-cert-snakeoil.pem
ssl_key = </etc/ssl/private/ssl-cert-snakeoil.key
確保Postfix和Dovecot都啟用了TLS加密。
sudo systemctl restart postfix
sudo systemctl restart dovecot
確保防火墻允許SMTP(端口25)、IMAP(端口143)和POP3(端口110)流量。
sudo ufw allow 25/tcp
sudo ufw allow 143/tcp
sudo ufw allow 110/tcp
sudo ufw enable
使用openssl
命令測試SMTP連接是否加密:
openssl s_client -connect mail.example.com:25 -starttls smtp
你應該看到類似以下的輸出,表示連接已加密:
CONNECTED(00000003)
depth=2 O = Digital Signature Trust Co., CN = DST Root CA X3
verify error:num=20:unable to get local issuer certificate
verify return:1
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 = mail.example.com
verify return:1
---
Certificate chain
0 s:CN = mail.example.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
2 s:O = Digital Signature Trust Co., CN = DST Root CA X3
i:/C=US/O=Let's Encrypt/CN=R3
---
Server certificate
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
subject=CN = mail.example.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 3544 bytes and written 428 bytes
Verification error: self signed 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: 19 (self signed certificate)
---
通過以上步驟,你應該能夠在Ubuntu上實現郵件服務器的加密傳輸。