在CentOS Stream 8上搭建郵件服務器,你可以選擇使用Postfix作為MTA(郵件傳輸代理),Dovecot作為MDA(郵件交付代理)。以下是詳細的步驟:
首先,更新你的系統并安裝所需的軟件包:
sudo dnf update -y
sudo dnf install -y postfix dovecot dovecot-pgsql dovecot-sieve dovecot-managesieved
/etc/postfix/main.cf
編輯 /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 [::1]/128
home_mailbox = Maildir/
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination
smtpd_tls_security_level = may
smtpd_tls_cert_file = /etc/pki/tls/certs/smtpd.crt
smtpd_tls_key_file = /etc/pki/tls/private/smtpd.key
smtpd_use_tls = yes
如果你還沒有SSL證書,可以使用Let’s Encrypt生成:
sudo yum install -y certbot python3-certbot-postfix
sudo certbot --postfix -d mail.example.com
按照提示完成證書的生成和配置。
/etc/dovecot/dovecot.conf
編輯 /etc/dovecot/dovecot.conf
文件,確保包含以下內容:
protocols = imap pop3
listen = *
mail_location = maildir:~/Maildir
ssl = yes
ssl_cert = </etc/pki/tls/certs/smtpd.crt
ssl_key = </etc/pki/tls/private/smtpd.key
編輯 /etc/dovecot/conf.d/10-auth.conf
文件,確保包含以下內容:
disable_plaintext_auth = no
auth_mechanisms = plain login
編輯 /etc/dovecot/conf.d/10-mail.conf
文件,確保包含以下內容:
mail_plugins = $mail_plugins sieve
創建Sieve腳本目錄并設置權限:
sudo mkdir -p /var/spool/dovecot/sieve
sudo chown -R vmail:vmail /var/spool/dovecot/sieve
創建一個簡單的Sieve腳本 /var/spool/dovecot/sieve/default.sieve
:
require ["fileinto", "imap4flags", "envelope"];
if header :contains "subject" "urgent" {
fileinto "Urgent";
} else {
fileinto "Inbox";
}
啟動并啟用Postfix和Dovecot服務:
sudo systemctl start postfix
sudo systemctl enable postfix
sudo systemctl start dovecot
sudo systemctl enable dovecot
確保防火墻允許SMTP、IMAP和POP3端口:
sudo firewall-cmd --permanent --add-service=smtp
sudo firewall-cmd --permanent --add-service=imap
sudo firewall-cmd --permanent --add-service=pop3
sudo firewall-cmd --reload
你可以使用 telnet
或 openssl
命令測試郵件服務器是否正常工作:
telnet mail.example.com 25
輸入以下命令進行測試:
HELO localhost
MAIL FROM:<sender@example.com>
RCPT TO:<recipient@example.com>
DATA
Subject: Test Email
This is a test email.
.
QUIT
如果一切配置正確,你應該能夠成功發送和接收郵件。
確保你的DNS記錄中包含MX記錄,指向你的郵件服務器地址。例如:
example.com. IN MX 10 mail.example.com.
完成以上步驟后,你的CentOS Stream 8郵件服務器應該已經搭建完成并可以正常使用了。