在 CentOS 上设置邮件转发,通常取决于你使用的是哪种邮件系统或场景。下面按常见情况分别说明。
CentOS 默认常带 Postfix(或旧版 Sendmail)。
~/.forward 文件适用于本地用户接收邮件后转发
vi ~/.forward
内容示例:
user@example.com
或转发给多个地址:
user1@example.com
user2@example.com
保存后:
chmod 644 ~/.forward
✅ 适合:
编辑 Postfix 配置:
vi /etc/postfix/main.cf
添加或修改:
virtual_alias_maps = hash:/etc/postfix/virtual
创建虚拟映射文件:
vi /etc/postfix/virtual
示例:
root@localhost admin@example.com
@localhost all@example.com
生成数据库:
postmap /etc/postfix/virtual
重新加载:
systemctl restart postfix
/etc/aliases(推荐系统管理员)适用于系统用户转发
vi /etc/aliases
示例:
root: admin@example.com
www: webmaster@example.com
生效:
newaliases
✅ 常用于:
vi /etc/aliases
修改:
root: your_email@example.com
执行:
newaliases
如果你想让 CentOS 通过外部邮箱发信:
vi /etc/postfix/main.cf
relayhost = [smtp.example.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
创建密码文件:
vi /etc/postfix/sasl_passwd
[smtp.example.com]:587 user:password
postmap /etc/postfix/sasl_passwd
systemctl restart postfix
echo "test" | mail -s "test mail" root
查看日志:
tail -f /var/log/maillog
| 场景 | 推荐方式 |
|---|---|
| root 邮件转发 | /etc/aliases |
| 单用户转发 | ~/.forward |
| 全系统转发 | virtual_alias_maps |
| 外网发信 | relayhost |
如果你能说明:
我可以给你更精确的配置示例。