Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Sendmail 郵件服務器部署全流程:從基礎配置到多域中繼實戰

編輯:關於伺服器

一、基礎環境準備

# 安裝核心包(CentOS/RHEL 5/6/7 通用)
yum install -y sendmail sendmail-cf m4 dovecot cyrus-sasl

# 啟動服務並設置開機自啟
service sendmail start
service saslauthd start
service dovecot start
chkconfig sendmail on
chkconfig saslauthd on
chkconfig dovecot on

二、單機收發郵件快速驗證

  1. 創建測試用戶:useradd mailuser && echo "Pass123" | passwd --stdin mailuser
  2. 本機發信測試:
    mail -s "Test" mailuser@localhost <<< "Body"
    su - mailuser -c "mail"
  3. 查看隊列:mailq 或 sendmail -bp

三、核心配置文件詳解

文件角色關鍵修改點
/etc/mail/sendmail.mc主配置源文件(M4 宏)修改 DAEMON_OPTIONS 監聽 0.0.0.0:25;啟用 FEATURE(\`access_db')、FEATURE(\`virtusertable') 等
/etc/mail/access訪問控制/中繼授權格式:域名/IP RELAY/OK/DISCARD;修改後需 makemap hash /etc/mail/access < /etc/mail/access
/etc/mail/local-host-names本地郵件域列表每行一個域名(如 example.com),郵件將本地投遞而非轉發
/etc/mail/virtusertable虛擬用戶映射user@domain.com local_user;修改後 makemap hash ...
/etc/dovecot/dovecot.confIMAP/POP3 服務配置 mail_location = maildir:~/Maildir、啟用 auth_mechanisms = plain login

四、生產環境關鍵配置步驟

1. 修改監聽地址(允許外部連接)

# /etc/mail/sendmail.mc 第 116 行左右
dnl DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
DAEMON_OPTIONS(`Port=smtp,Addr=0.0.0.0, Name=MTA')dnl

重新生成 sendmail.cf 並重啟:

m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
service sendmail restart

2. 設置主機名與 DNS(關鍵)

# /etc/sysconfig/network
HOSTNAME=mail.example.com

# /etc/hosts
127.0.0.1   mail.example.com localhost
<公網IP>    mail.example.com

# /etc/resolv.conf
nameserver <本機或內網DNS>
# 確保 mail.example.com A 記錄、MX 記錄、PTR 記錄(反向解析)均正確

3. 配置中繼授權(防止成為開放中繼)

# /etc/mail/access
example.com       RELAY
192.168.1.0/24    RELAY
10.0.0.0/8        OK
spammer.com       DISCARD

# 生效
makemap hash /etc/mail/access < /etc/mail/access
service sendmail restart

4. 啟用 SMTP 認證(SASL)+ TLS 加密

# /etc/mail/sendmail.mc 追加
TRUST_AUTH_MECH(`LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `LOGIN PLAIN')dnl
define(`confAUTH_OPTIONS', `A p')dnl
DAEMON_OPTIONS(`Port=smtps, Name=TLSMTA, M=s')dnl

# 生成證書(自簽或申請正式證書)
openssl req -new -x509 -days 3650 -nodes -out /etc/mail/cert.pem -keyout /etc/mail/key.pem
chmod 600 /etc/mail/key.pem

# 重新生成 cf 並重啟
m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
service sendmail restart
service saslauthd restart

五、多域中繼案例:163.com ↔ sina.com 互通

  1. 雙方均配置 named.conf 添加 forwarders { 對方IP; }; 實現 DNS 轉發。
  2. 雙方 /etc/mail/access 互加 RELAY:
# 163.com 服務器
163.com   OK
sina.com  RELAY

# sina.com 服務器
sina.com  OK
163.com   RELAY

生效:makemap hash ... && service sendmail restart

六、客戶端配置(Outlook/Foxmail)

  • 接收服務器:mail.example.com:110 (POP3) / 995 (POP3S) / 143 (IMAP) / 993 (IMAPS)
  • 發送服務器:mail.example.com:25 / 465 (SMTPS) / 587 (Submission)
  • 勾選「我的服務器需要身份驗證」,使用完整郵箱地址作為用戶名。

七、常見故障排查清單

現象排查方向關鍵命令/日誌
無法發送(連接超時)防火牆 25/465/587 端口、ISP 封鎖 25 端口telnet mail.example.com 25、iptables -nL
郵件進垃圾箱/被拒收缺少 SPF/DKIM/DMARC、IP 信譽差、PTR 未設置dig txt example.com、郵件原文檢視 Authentication-Results
本地用戶收不到外域郵件local-host-names 缺域、MX 記錄錯誤、Dovecot 未啟動cat /etc/mail/local-host-names、netstat -lnpt | grep dovecot
發信隊列堆積遠程服務器拒收、DNS 解析失敗、磁盤滿mailq、/var/log/maillog、df -h

八、遷移建議:Postfix / Exim 更現代化

Sendmail 配置複雜、安全歷史漏洞多、性能不如 Postfix。新項目建議直接部署 Postfix + Dovecot + Roundcube,配置語法簡潔,生態成熟,社區活躍。遷移時可保留 /etc/mail/access、virtusertable 等數據文件,僅重寫主配置邏輯。

Copyright © Windows教程網 All Rights Reserved