CentOS邮件服务器使用指南(基于Postfix+Dovecot)
example.com),用于MX记录解析;sudo systemctl stop firewalld && sudo systemctl disable firewalld sudo sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config setenforce 0 安装Postfix(MTA,邮件传输代理)、Dovecot(MDA,邮件投递代理)及SASL认证组件:
sudo yum update -y sudo yum install postfix dovecot cyrus-sasl -y 编辑Postfix主配置文件/etc/postfix/main.cf,设置核心参数:
sudo vi /etc/postfix/main.cf 关键配置说明:
myhostname = mail.example.com # 替换为你的邮件服务器主机名 mydomain = example.com # 替换为你的域名 myorigin = $mydomain inet_interfaces = all # 监听所有网络接口 inet_protocols = ipv4 # 仅使用IPv4(若需IPv6,改为"all") mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain home_mailbox = Maildir/ # 邮件存储格式为Maildir(目录结构) smtpd_sasl_auth_enable = yes # 启用SMTP认证 smtpd_sasl_security_options = noanonymous # 禁止匿名登录 smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination smtpd_use_tls = yes smtpd_tls_cert_file = /etc/pki/tls/certs/localhost.crt # 证书路径(可使用自签名证书) smtpd_tls_key_file = /etc/pki/tls/private/localhost.key 编辑Dovecot主配置文件/etc/dovecot/dovecot.conf及子配置文件:
sudo vi /etc/dovecot/dovecot.conf 关键配置说明:
protocols = imap pop3 lmtp # 启用IMAP/POP3/LMTP协议 listen = * # 监听所有IP地址 mail_location = maildir:~/Maildir # 与Postfix的home_mailbox一致 编辑SASL认证配置/etc/dovecot/conf.d/10-auth.conf:
sudo vi /etc/dovecot/conf.d/10-auth.conf 关键配置:
disable_plaintext_auth = no # 允许明文认证(测试环境可开启,生产环境建议用SSL加密) auth_mechanisms = plain login # 支持PLAIN和LOGIN认证方式 编辑邮件存储配置/etc/dovecot/conf.d/10-mail.conf:
sudo vi /etc/dovecot/conf.d/10-mail.conf 关键配置:
mail_privileged_group = mail # 允许mail组用户访问邮件目录 添加系统用户并设置密码(每个用户对应一个邮箱):
sudo useradd -m -s /sbin/nologin user1 # 创建用户(-s指定无登录shell) sudo passwd user1 # 设置密码 邮件目录会自动生成(/home/user1/Maildir),无需手动创建。
允许邮件服务相关端口通过防火墙:
sudo firewall-cmd --permanent --add-service=smtp # SMTP(端口25) sudo firewall-cmd --permanent --add-service=imap # IMAP(端口143) sudo firewall-cmd --permanent --add-service=pop3 # POP3(端口110) sudo firewall-cmd --reload 启动Postfix和Dovecot,并设置开机自启:
sudo systemctl start postfix dovecot sudo systemctl enable postfix dovecot 使用telnet测试SMTP:
连接到本地SMTP端口,模拟发送邮件:
telnet localhost 25 输入以下命令(替换为实际邮箱地址):
HELO localhost MAIL FROM:<user1@example.com> RCPT TO:<recipient@example.com> # 可替换为自己的邮箱(如Gmail) DATA Subject: Test Email This is a test email from CentOS mail server. . QUIT 若收到“250 2.0.0 Ok: queued as…”响应,说明SMTP发送成功。
使用邮件客户端测试:
配置Outlook/Foxmail等客户端,添加IMAP/SMTP账户:
mail.example.com,端口993(SSL加密);mail.example.com,端口587(STARTTLS);user1@example.com/设置的密码。sudo yum update定期更新Postfix、Dovecot等组件,修复安全漏洞。