阿里雲centos7 郵箱轉發服務配置linux
1、文檔編寫目的:vim
網絡拓撲圖:centos
阿里雲服務器屏蔽掉25端口後,內網服務器如何經過跳板機發送郵件到外網郵箱。安全
若是是可聯網的阿里雲機器,能夠直接配置mailx使用465端口發送郵件。須要進行ssl驗證配置。服務器
背景知識:網絡
- 1. Alternatives –config mta 此命令用於查看和設置當前機器郵箱服務的選項,按數字鍵選擇要使用的郵箱服務。下圖選擇使用postfix
Linux系統通常內置mailx,ssmtp,實現25,587,465端口的郵箱服務。app
郵箱服務之間相互獨立,mta設置哪一個系統就使用哪一個,不存在衝突。對於內置的郵箱服務若是的要卸載的話,必定要慎重,由於不少服務默認和mailx綁定,存在依賴關係,強制卸載會致使依賴服務被刪除。(linux中卸載更新操做必定要慎而又慎)tcp
- 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 進行調試