在 CentOS 上“设置邮件转发”通常有几种常见场景,下面按最常见用法给你说明,你可以对照自己的需求选择。
适用于:系统内部用户(如 root)收到邮件,转发到别的邮箱
~/.forward例如把 root 的邮件转发到外部邮箱:
vi /root/.forward
内容:
you@example.com
多个收件人:
user1@example.com
user2@example.com
✅ 立即生效,无需重启服务
✅ 适合 postfix / sendmail
适用于:所有本地用户或特定用户统一转发
编辑 Postfix 的别名文件(CentOS 默认用 postfix):
vi /etc/aliases
示例:
root: you@example.com
www: you@example.com
保存后执行:
newaliases
适用于:把 CentOS 发出的所有邮件通过外部 SMTP 转发
yum install postfix mailx -y
systemctl enable postfix
systemctl start postfix
vi /etc/postfix/main.cf
关键配置:
myhostname = centos.example.com
mydestination = $myhostname, localhost
relayhost = [smtp.example.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
vi /etc/postfix/sasl_passwd
[smtp.example.com]:587 user@example.com:password
生成数据库:
postmap /etc/postfix/sasl_passwd
chmod 600 /etc/postfix/sasl_passwd*
systemctl restart postfix
echo "test" | mail -s "test mail" you@example.com
查看日志:
tail -f /var/log/maillog
| 需求 | 推荐方式 |
|---|---|
| root 邮件转发 | /root/.forward |
| 多用户转发 | /etc/aliases |
| 服务器统一发信 | Postfix + relayhost |
| 云服务器发外部邮箱 | SMTP 转发(避免被拦截) |
如果你能告诉我:
我可以给你一份可直接复制使用的配置。