阿里云服务器 centos7 中继邮箱+转发服务 详细配置

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

1、文档编写目的:vim

网络拓扑图:centos

 

 

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

若是是可联网的阿里云机器,能够直接配置mailx使用465端口发送邮件。须要进行ssl验证配置。服务器

背景知识:网络

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

 

 

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

邮箱服务之间相互独立,mta设置哪一个系统就使用哪一个,不存在冲突。对于内置的邮箱服务若是的要卸载的话,必定要慎重,由于不少服务默认和mailx绑定,存在依赖关系,强制卸载会致使依赖服务被删除。(linux中卸载更新操做必定要慎而又慎)tcp

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

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

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

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

2、内容概述:

  环境准备

  示例验证

  总结

  注:文档中ip均为示例ip

3、配置流程

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

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

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

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

yum安装postfix

Yum install postfix  -y   

二、 配置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的发件人名称一概改成 admin@qq.com

root@host229     admin@qq.com

vi  generic    配置以下

root@host229     admin@qq.com

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’  jason@qq.com

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

 

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

  

三、 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:admin@qq.com: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

      

主配文件修改完成,接着修改地址假装文件,参照以前的配置修改发件人为admin@qq.com

记得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  admin@qq.com:1234

 

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

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

相关文章
相关标签/搜索