1. 程式人生 > >阿裏雲centos7 郵箱轉發服務配置

阿裏雲centos7 郵箱轉發服務配置

service fig lte ane log日誌 sas user 文檔 重啟

阿裏雲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]

[email protected] [email protected]

vi generic 配置如下

[email protected] [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 進行調試

阿裏雲centos7 郵箱轉發服務配置