作者: pipikeung 時間: 2021-1-4 07:12 標題: 安裝郵件伺服器Postfix全紀錄
本帖最後由 pipikeung 於 2021-1-8 11:26 編輯
安裝及設定Postfix
Postfix是個MTA(Mail Transfer Agent),主要在Linux系統中轉寄及寄出郵件,並使用s-nail作為MUA(Mail User Agent),即是電郵客戶端代理。
01 安裝Postfix
sudo DEBIAN_PRIORITY=low apt install postfix
按指示填上以下參數﹕
General type of mail configuration?: Internet Site
System mail name: example.com (not mail.example.com)
Root and postmaster mail recipient: The username of your primary Linux account (sammy in our examples)
Other destinations to accept mail for: $myhostname, example.com, mail.example.com,
localhost.example.com, localhost
Force synchronous updates on mail queue?: No
Local networks: 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
Mailbox size limit: 0
Local address extension character: +
Internet protocols to use: all
當然example.com要改為你郵件伺服器名稱
02 更改Postfix設定
創建Maildir,/etc/postfix/virtual
sudo postconf -e 'home_mailbox= Maildir/'
sudo postconf -e 'virtual_alias_maps= hash:/etc/postfix/virtual'
若需要將contact@example.com 及admin@example.com 轉給指定使用者(pipikeung)﹕
用文字編輯器修改﹕
sudo nano /etc/postfix/virtual
加入以下文字﹕
contact@example.com pipikeung
admin@example.com pipikeung
當然所有「example.com」都要改為你郵件伺服器的名字。
Mapping(映射) 到 /etc/postfix/virtual使內容得以生效﹕
sudo postmap /etc/postfix/virtual
最後重啟Postfix
sudo systemctl restart postfix
03 開啟防火牆指定埠,使Postfix能通過
sudo ufw allow Postfix
04 安裝郵件客戶端(Mail Client)及起始郵件目錄(Maildir) 結構﹕
以下檔案加入所需變數﹕
echo 'export MAIL=~/Maildir' | sudo tee -a /etc/bash.bashrc | sudo tee -a /etc/profile.d/mail.sh
從 /etc/profile.d/mail.sh中讀取變數(variable)
source /etc/profile.d/mail.sh
修改 /etc/s-nail.rc
sudo nano /etc/s-nail.rc
在該檔最尾加上以下文字﹕
. . .
set emptystart
set folder=Maildir
set record=+sent
驗證~/Maildir是否真實存在﹕
ls -R ~/Maildir

證實成功
05 驗證Postfix是否成功收取郵件﹕
從Gmail寄出一封信給pipikeung@pipikeung.dnsalias.net

進入s-nail驗證郵件,證實收到郵件

從Postfix寄出郵件給pipikeung,驗證能否送出
先用文字處理器開一文字檔,寫一些內容
nano ~/text_message
用s-nail 寄出
cat ~/test_message | s-nail -s '測試內部寄出電郵' pipikeung@pipikeung.dnsalias.net

證實成功﹗
06 但是由Postfix寄出去外面主機的電郵收不到,查/var/log/mail.log,得知外部電郵主機把pipikeung.dnsalias.net視作 「垃圾郵件」伺服器不接收由此伺服器送出的郵件。
解決方法﹕
「山不轉、人轉」,配置Postfix,使用Gmail SMTP來送出電郵,修改postfix的主要設定檔/etc/postfix/main.cf如下﹕
relayhost = [smtp.gmail.com]:587
smtp_tls_security_level=encrypt
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
確保檔案內有以上設定,然後儲存登出
07 設定SMTP SASL 認證
文字處理器創建/etc/postfix/sasl_passwd檔案
sudo nano /etc/postfix/sasl_passwd
在內加入以下文字﹕
[smtp.gmail.com]:587 userid@gmail.com:password
userid@gmail.com (你Gmail的帳號)
password (Gmail的密碼)
08 創建SASL密碼DB檔,使增加讀取速度
postmap /etc/postfix/sasl_passwd
09 測試昰否成功用Gmail STMP寄出郵件
echo "Test Postfix Gmail SMTP Relay" | mail -s "Postfix Gmail SMTP Relay" userid@gmial
若不成功請閱讀檢查/var/log/mail.log
tail /var/log/mail.log
發現SASL認證方式失敗,可登入Gmail選Allow less secure apps:ON後,再測試。
檢查mail.log
tail /var/log/mail.log



證實成功﹗
但寄件者會顯示由pipikeung@gmail.com發出。
若你不打算用Postfix寄出電郵到系統外的電郵主機,在06以後的一切可以不理﹗
後話﹕主觀覺得郵件伺服器無太大誘因自行建立,因要獲得電郵地址,有大量可用免費資源提供,例如﹕Gmail,Hotmail,Yahoo....
用Postfix因為簡單,只要安裝後在/etc/postfix/main.cf設定好幾個參數,便大致完成。
但若你想架設的伺服器的每個使用者,都有相對應的電郵地址,例如﹕pipikeung.dnsalias.net註冊使用者有amy,billy,pipikeung等等
使用者的電郵相應是﹕amy@pipikeung.dnsalias.net,billy@pipikeung.dnsalias.net及pipikeung@pipikeung.dnsalias.net
那你可考慮建個簡單的Postfix電郵伺服器。
作者: eel997 時間: 2021-1-4 09:14
多謝師兄心得!
但我有一些擔心:
- 家庭寛頻不開放對外port
- 好多時set email server會成為一個門,被人hack及發放junk mails,而如果不是經常check住都好難發覺
作者: KinChungE 時間: 2021-1-4 09:27
借post一問
如果postfix想限制人send出既"from" domain, 有冇好方法?
上網見都係教限每個user指定email address send出, 但係唔見有global限
作者: pipikeung 時間: 2021-1-4 10:03
回覆 2# eel997
同意﹗多一樣服務,必要開相應埠(port)給服務使用,當然多一份危險﹗時常檢查紀錄檔(log file)亦十分煩人,所以架任何類型的伺服器,都要在實在有需要才建立。
作者: chilun 時間: 2021-1-4 10:35
https://wiki.policyd.org/
作者: KinChungE 時間: 2021-1-4 16:51
即係postfix自己做唔到
作者: ffchung 時間: 2021-1-4 17:13
應該要有DNS Setup (MX)
作者: chilun 時間: 2021-1-4 18:35
做到,不過比較煩,你google postfix sender_access 就有資料
policyd 有GUI方便啲
作者: KinChungE 時間: 2021-1-4 19:17
我試左, 然後佢唔止寄出既block, 連收信都block埋
作者: KinChungE 時間: 2021-1-4 19:20
其實理論上, 不論開任何server都要set DNS
不過Email比較複雜, 除基本A record外, 仲有MX, TXT, PTR, DKIM....
作者: chilun 時間: 2021-1-5 00:51
你講policyd定postfix sender_access?
應該set 錯嘢
作者: KinChungE 時間: 2021-1-5 01:47
postfix sender_access
# main.cf
smtpd_sender_restrictions =
reject_unknown_recipient_domain,
reject_non_fqdn_recipient,
check_sender_access hash:/etc/postfix/sender_access,
reject
# /etc/postfix/sender_access
aaa.com OK
bbb.com OK
結果街外既信全部入唔到黎
作者: chilun 時間: 2021-1-5 09:46
postfix sender_access
# main.cf
smtpd_sender_restrictions =
reject_unknown_recipient_d ...
KinChungE 發表於 2021-1-5 01:47
try
reject_non_fqdn_sender,
reject_unknown_sender_domain,
Ref: https://wiki.centos.org/HowTos/postfix_restrictions
作者: KinChungE 時間: 2021-1-6 12:29
之前copy錯, 已經用緊呢組setting, 都係唔得
唔知點整outgoing mail先check, incoming mail唔check
作者: kirafung 時間: 2021-1-6 13:50
好詳盡教學
我會覺得用zimbra 簡單D
作者: little_keung 時間: 2021-1-7 20:55
postfix 同 zimbra 係 target做唔同O既用途, postfix 係做 MTA , 即係傳信, zimbra 係做 MUA o既 backend . 不能比較.
作者: pipikeung 時間: 2021-1-8 11:25
用什麽來收發電郵,則各有所好,若歡喜簡單可使用s-nail或Mutt
個人比較喜歡使用Mutt,作為MUA﹕
由Mutt寄給系統內的郵件收發情況


由Mutt外寄系統外主機郵件收發情況


作者: 普羅 時間: 2021-1-9 16:04
Mail server 起唔難, 要main得好先難, security同backup極之重要

