在定制邮件服务器前,需完成以下准备工作:
example.com
,需完成DNS解析);邮件服务器的核心组件为Postfix(MTA,邮件传输代理)和Dovecot(MDA,邮件投递代理),负责邮件的发送、接收与存储。
使用Yum包管理器安装:
sudo yum update -y
sudo yum install postfix dovecot -y
安装完成后,启动服务并设置开机自启:
sudo systemctl start postfix dovecot
sudo systemctl enable postfix dovecot
Postfix的主配置文件为/etc/postfix/main.cf
,需修改以下关键参数:
myhostname = mail.example.com # 邮件服务器主机名(需与域名一致)
mydomain = example.com # 邮件所属域名
myorigin = $mydomain # 发送邮件时的“From”域
inet_interfaces = all
inet_protocols = ipv4 # 仅使用IPv4(若需IPv6则改为“all”)
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot # 使用Dovecot作为SASL认证后端
smtpd_sasl_path = private/auth # 认证套接字路径(需与Dovecot配置一致)
smtpd_sasl_security_options = noanonymous # 禁止匿名登录
smtpd_recipient_restrictions =
permit_mynetworks, # 允许本地网络(如127.0.0.0/8)
permit_sasl_authenticated, # 允许SMTP认证用户
reject_unauth_destination # 拒绝未授权的目标域
smtpd_use_tls = yes
smtpd_tls_cert_file = /etc/pki/tls/certs/localhost.crt # SSL证书路径
smtpd_tls_key_file = /etc/pki/tls/private/localhost.key # SSL私钥路径
smtpd_tls_security_level = may # 可选“encrypt”(强制加密)
配置完成后,重启Postfix使更改生效:
sudo systemctl restart postfix
Dovecot的主配置文件为/etc/dovecot/dovecot.conf
,需开启IMAP/SMTP服务并配置认证:
protocols = imap pop3 smtp # 开启IMAP、POP3、SMTP服务
listen = * # 监听所有网络接口
mail_location = maildir:~/Maildir # 邮件存储在用户家目录下的Maildir文件夹
/etc/dovecot/conf.d/10-auth.conf
,允许明文认证(SMTP需要)并指定认证机制:disable_plaintext_auth = no # 允许明文认证(生产环境建议开启SSL后设为“yes”)
auth_mechanisms = plain login # 支持PLAIN(明文)和LOGIN(明文)认证
/etc/dovecot/conf.d/10-ssl.conf
,启用SSL并指定证书路径:ssl = yes # 启用SSL
ssl_cert = </etc/pki/tls/certs/localhost.crt # SSL证书路径
ssl_key = </etc/pki/tls/private/localhost.key # SSL私钥路径
ssl_protocols = !SSLv2,!SSLv3 # 禁用不安全的SSL协议
配置完成后,重启Dovecot使更改生效:
sudo systemctl restart dovecot
邮件服务器需通过DNS记录告知互联网其身份,需添加以下记录:
记录类型:MX
主机记录:@
记录值:mail.example.com
优先级:10
记录类型:A
主机记录:mail
记录值:your_server_ip
sudo firewall-cmd --permanent --add-service=smtp # SMTP(25端口)
sudo firewall-cmd --permanent --add-service=smtps # SMTPS(465端口)
sudo firewall-cmd --permanent --add-service=imap # IMAP(143端口)
sudo firewall-cmd --permanent --add-service=imaps # IMAPS(993端口)
sudo firewall-cmd --permanent --add-service=pop3 # POP3(110端口)
sudo firewall-cmd --permanent --add-service=pop3s # POP3S(995端口)
sudo firewall-cmd --reload
sudo yum install certbot python3-certbot-nginx -y # 若使用Nginx反向隧道
sudo certbot certonly --standalone -d mail.example.com
更新Postfix与Dovecot的证书路径为Let’s Encrypt证书路径(/etc/letsencrypt/live/mail.example.com/fullchain.pem
和/etc/letsencrypt/live/mail.example.com/privkey.pem
)。telnet
或openssl
测试SMTP/IMAP服务是否正常:telnet localhost 25 # 测试SMTP服务
openssl s_client -connect localhost:993 # 测试IMAPS服务
输入SMTP命令(如HELO localhost
、MAIL FROM:<user@example.com>
、RCPT TO:<recipient@example.com>
、DATA
)验证邮件发送功能。mail.example.com
,端口:465(SMTPS)或587(SMTP+STARTTLS);mail.example.com
,端口:993(IMAPS);user@example.com
)。sudo yum install spamassassin -y
sudo systemctl start spamassassin
sudo systemctl enable spamassassin
配置Postfix调用SpamAssassin(编辑/etc/postfix/main.cf
):spamassassin_destination_recipient_limit = 1
smtpd_milters = inet:127.0.0.1:8891 # SpamAssassin的milter端口
non_smtpd_milters = $smtpd_milters
通过以上步骤,可在CentOS上定制一个基础的邮件服务器。根据实际需求,可进一步扩展功能(如虚拟域、邮件转发、负载均衡等),但需注意遵循邮件服务器最佳实践(如定期更新软件、监控日志、备份数据),确保服务器稳定与安全。