阿里云centos7 邮箱转发服务配置

阿里云centos7 邮箱转发服务配置

一、文档编写目的:

网络拓扑图:

阿里云服务器屏蔽掉25端口后,内网服务器如何通过跳板机发送邮件到外网邮箱。

如果是可联网的阿里云机器,可以直接配置mailx使用465端口发送邮件。需要进行ssl验证配置。

背景知识:

  1. 1.     Alternatives –config mta  此命令用于查看和设置当前机器邮箱服务的选项,按数字键选择要使用的邮箱服务。下图选择使用postfix

Linux系统一般内置mailx,ssmtp,实现25,587,465端口的邮箱服务。

邮箱服务之间相互独立,mta设置哪个系统就使用哪个,不存在冲突。对于内置的邮箱服务如果的要卸载的话,一定要慎重,因为很多服务默认和mailx绑定,存在依赖关系,强制卸载会导致依赖服务被删除。

  1. 2.     邮箱服务常见端口及加密协议:

25端口(SMTP):25端口为SMTP(Simple Mail Transfer Protocol,简单邮件传输协议)服务所开放的,是用于发送邮件

587端口是STARTTLS协议的 属于TLS通讯协议 只是他是在STARTTLS命令执行后才对之后的原文进行保护的。

465端口是为SMTPS(SMTP-over-SSL)协议服务开放的,这是SMTP协议基于SSL安全协议之上的一种变种协议,它继承了SSL安全协议的非对称加密的高度安全可靠性,可防止邮件泄露。

二、内容概述:

  环境准备

  示例验证

  总结

  注:文档中ip均为示例ip

三、配置流程

  测试环境:centos7  阿里云服务器  内网服务器+外网跳板机

1、 内网服务器安装postfix服务

网上大量教程经常性同时安装sendmail和postfix,实际上不必要,用哪个装哪个即可,系统要是自带了直接启动即可,不需要反复卸载安装,他们之间是独立的,互不影响。只是在mta设置中用哪个必须选中。

本文档后续提到的邮箱服务默认都为postfix。

yum安装postfix

Yum install postfix  -y   

2、 配置229内网机邮件经postfix转发到152机器。

进入postfix 目录 cd /etc/postfix  所有配置文件都在这个目录

首先配置main.cf  编辑文件并在末尾添加如下行

#开启转发规则  此处配置告知postfix 发往哪里的邮件需要转发

transport_maps = hash:/etc/postfix/transport

 

#发件地址伪装  此处配置是为了保持发件地址和发件人同为授权人,如果发件人和授权人不一致,有些邮箱会拒收

smtp_generic_maps = hash:/etc/postfix/generic

sender_canonical_maps = hash:/etc/postfix/canonical

local_header_rewrite_clients = static:all

 

配置完成,搜索inet,修改

inet_interfaces = all

 

上述完成,主配置文件即配置完成。接着配置其他文件。

Vi  canonical  配置如下  所以经过postfix的发件人名称一律改为 [email protected]

root@host229     [email protected]

vi  generic    配置如下

root@host229     [email protected]

vi  transport  配置如下 含义所有发往qq.com的邮件一律先转发到152机器

qq.com        smtp:176.19.83.152

 

上述三文件,需要执行postmap生成db文件供服务使用,否则配置无效

Postmap  generic 

Postmap  canonical

Postmap  transport

 

   重启postfix 查看是否正常启动

   Systemctl restart postfix

   Tail –f  /var/log/mailog

   至此,内网机器配置完成。邮件统一转发到152跳板机

   测试命令:

   Echo test |mail –s  ‘test’  [email protected]

   通过查看maillog日志验证是否配置成功

 

日志中会显示邮件from来源,to收件人地址,relay地址;

  

3、 152跳板机配置

跳板机收到内网转发来的邮件后,此处可以直接使用587端口postfix直接发送出去,也可以使用stunnel代理465端口发送出去。

分两种情况进行配置,二选一即可,此处都进行演示:

 

     第一种 使用587端口,配置qq中继服务器发送邮件。

Yum  install libsasl2-modules postfix

      Cd /etc/postfix 修改主配置文件

      Vi main.cf  添加如下行

      

  # Enable auth  开启sasl验证

smtp_sasl_auth_enable = yes

# Set username and password  设置qq发件邮箱的授权用户和密码

smtp_sasl_password_maps = static:[email protected]:1234

smtp_sasl_security_options = noanonymous

# Turn on tls encryption   开启tls加密

smtp_tls_security_level = encrypt

header_size_limit = 4096000

# Set external SMTP relay host here IP or hostname accepted along with a port number.

设置内部中继服务的主机域名和端口

relayhost = [smtp.exmail.qq.com]:587

 

#发件地址伪装  保持发件人和授权用户一直  否则qq邮箱会拒收

smtp_generic_maps = hash:/etc/postfix/generic

sender_canonical_maps = hash:/etc/postfix/canonical

local_header_rewrite_clients = static:all

      

主配文件修改完成,接着修改地址伪装文件,参照之前的配置修改发件人为[email protected]

记得postmap生成db文件。

Systemctl restart postfix 重启服务

 发送测试邮件测试。

 

 

第二种 使用465端口,通过stunnel代理发送邮件,尝试过不通过配置stunnel直接发送465端口邮件,未配置成功,此处参照网友分享,通过stunnel代理走465端口发邮件。

首先安装stunnel代理

 

yum install -y stunnel

创建 stunnel 服务端配置文件,这里使用 5000 端口作为 stunnel 的服务端口:

 

创建加密证书文件:

 

openssl genrsa -out key.pem 2048

openssl req -new -x509 -key key.pem -out cert.pem -days 1095

cat key.pem cert.pem >> /etc/stunnel/stunnel.pem

 

修改配置文件:

vi /etc/stunnel/stunnel.conf  添加如下行:

[smtp-tls-wrapper]

accept = 5000

client = yes

cert = /etc/stunnel/stunnel.pem

connect = smtp.exmail.qq.com:465  

 

打开防火墙端口:

 

firewall-cmd --zone=public --add-port=5000/tcp --permanent

firewall-cmd --reload

创建 stunnel 服务的 systemd 配置文件:

vim /etc/systemd/system/stunnel.service  添加如下内容

[Unit]

  Description=SSL tunnel for network daemons

  After=syslog.target

 

[Service]

  ExecStart=/bin/stunnel /etc/stunnel/stunnel.conf

  ExecStop=kill -9 $(pgrep stunnel)

  ExecStatus=pgrep stunnel

  Type=forking

 

[Install]

  WantedBy=multi-user.target

 

最后启动客户端 stunnel 服务:

 

systemctl enable stunnel

systemctl start stunnel

 

 

至此stunnel配置完成,所有发送至5000端口的内容默认通过connect连接,即smtp.exmail.qq.com:465

接下来修改postfix主配置文件及地址伪装文件:

Cd /etc/postfix

Vi main.cf

 

 #### SMTP

# Enable SASL authentication

smtp_sasl_auth_enable = yes

# Disallow methods that allow anonymous authentication

smtp_sasl_security_options = noanonymous

# Location of sasl_passwd  此处发件授权用户没有使用静态模式,也可改为静态

smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd

# Enable STARTTLS encryption

# smtp_tls_security_level = encrypt

# Location of CA certificates

smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt

smtp_generic_maps = hash:/etc/postfix/generic

sender_canonical_maps = hash:/etc/postfix/canonical

local_header_rewrite_clients = static:all

# Set external SMTP relay host here IP or hostname accepted along with a port number.

relayhost = [localhost]:5000   //此处中继主机为stunnel端口

smtp_host_lookup = dns, native 

 

搜索inet_interfaces = all  修改为all

同时由于125为接收转发机器,所以要配置接收权限,哪些机器的转发请求会操作执行

搜索 mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128   175.12.97.0/24

最后边添加内网机器需要转发的ip段

至此main.cf 配置完成,接下来配置辅助文件

Vi canonical   generic  进行地址伪装  保持和授权发件用户一致

格式为: 需要伪装的用户名@主机名    伪装后的用户名@主机名

 

vi sasl/sasl_passwd  配置中继服务此处为stunnel 及授权用户和密码

[localhost]:5000  [email protected]:1234

 

重启 postfix验证是否能正常发送邮件

通过  tail –f /var/log/maillog 进行调试

猜你喜欢

转载自www.cnblogs.com/telegram/p/10718477.html
今日推荐