在使用 Ubuntu 搭建邮件服务器时,SMTP 认证是确保邮件安全发送的关键步骤。本文将详细讲解如何在 Ubuntu 系统中配置 Postfix 邮件服务器以启用 SMTP 身份验证,即使是 Linux 小白也能轻松上手。

在开始之前,请确保你已完成以下操作:
sudo apt install postfix)Postfix 默认不包含 SMTP 认证功能,我们需要安装 cyrus-sasl 相关组件来支持用户名和密码验证。
# 安装 SASL 支持包sudo apt updatesudo apt install -y sasl2-bin libsasl2-modules安装完成后,启动并启用 saslauthd 服务:
sudo systemctl start saslauthdsudo systemctl enable saslauthd
编辑 Postfix 主配置文件 /etc/postfix/main.cf:
sudo nano /etc/postfix/main.cf
在文件末尾添加或修改以下内容(请根据你的域名替换 yourdomain.com):
# 启用 SASL 认证smtpd_sasl_type = dovecotsmtpd_sasl_path = private/authsmtpd_sasl_auth_enable = yessmtpd_sasl_security_options = noanonymoussmtpd_sasl_local_domain = $myhostname# 限制只有认证用户才能通过 587 端口发信smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination# 启用 TLS 加密(推荐)smtpd_tls_auth_only = yessmtpd_tls_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pemsmtpd_tls_key_file = /etc/ssl/private/ssl-cert-snakeoil.keysmtpd_use_tls = yes# 允许外部通过 587 端口提交邮件submission inet n - y - - smtpd -o syslog_name=postfix/submission -o smtpd_tls_security_level=encrypt -o smtpd_sasl_auth_enable=yes -o smtpd_client_restrictions=permit_sasl_authenticated,reject
注意:如果你使用的是 Dovecot 作为 IMAP/POP3 服务器,建议采用
smtpd_sasl_type = dovecot并配置 Dovecot 的认证 socket。本文以简单场景为例,使用系统用户认证。
你可以使用系统用户作为 SMTP 登录账户。例如,创建一个专用邮件用户:
sudo adduser mailuser# 按提示设置密码该用户的用户名为 mailuser,密码即为你设置的密码,可用于 SMTP 认证登录。
保存配置后,重启 Postfix 使更改生效:
sudo systemctl restart postfix
使用 telnet 或 swaks 工具测试 SMTP 认证是否成功:
# 安装 swaks(如果未安装)sudo apt install -y swaks# 测试 SMTP 认证(替换 yourdomain.com 和 mailuser)swaks --to test@example.com \ --from mailuser@yourdomain.com \ --server localhost \ --port 587 \ --auth-user mailuser \ --auth-password 'your_password' \ --tls
如果返回 ===> MAIL FROM<mailuser@yourdomain.com> ===> 250 2.1.0 Ok,说明 Ubuntu SMTP认证配置 成功!
/var/log/mail.log 获取错误信息通过本教程,你已经成功完成了 Postfix SMTP设置 并启用了 SMTP 身份验证。这不仅提升了邮件系统的安全性,也为后续搭建完整的邮件服务器(如结合 Dovecot)打下基础。掌握 邮件服务器认证 和 Ubuntu邮件客户端配置 是运维人员的重要技能。
如有疑问,欢迎在评论区留言交流!
本文由主机测评网于2025-12-16发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025128412.html