Ubuntu16.04搭建Postfix作為SMTP服務器
一、DNS配置
類型 |
名稱 | 值 | TTL |
A | 128.199.254.32 | 1小時 | |
MX |
@ | mail.example.com(優先:10) | 1小時 |
TXT |
@ | v=spf1 mx:mail.example.com ip4:128.199.254.32 ~all | 1小時 |
TXT | mail._domain | keyv=DKIM1;k=rsa; p=MIGfMA0GCSqGSI… | 1小時 |
其中可以先配置A和MX記錄,
兩個TXT記錄用於“反垃圾郵件”,其中DKIM需要本機配置,後文會詳細說明。
二、Postfix安裝及配置
2.1 事先設置好hostname的話,Postfix可以自動配置好很多參數,節省時間。
# echo “example.com” > /etc/hostname
2.2 安裝postfix
# apt update # apt install mailutils
默認選項為Internet Site,填example.com 按回車。
2.3 基本配置
修改文件
… myhostname = mail.example.com mydomain = example.com … mydestination = $myhostname, localhost.$mydomain, $mydomain inet_interfaces = loopback-only
然後重啟服務。
# systemctl restart postfix
2.4 現在已經可以發郵件了,測試一下看能不能收到。
$ echo “body of the email” | mail -s “subject line” your_email_address
2.5 (可選配置)使用TLS加密
三、反垃圾設置:SPF,DKIM與DMARC
3.1 SPF只是添加DNS記錄即可,這裏重點說一下DKIM的安裝配置
# apt install opendkim opendkim-tools
3.2 編輯文件 /etc/opendkim.conf 追加如下配置到文件最下邊:AutoRestart YesAutoRestartRate 10/1h
UMask 002 Syslog yes SyslogSuccess Yes LogWhy Yes Canonicalization relaxed/simple ExternalIgnoreList refile:/etc/opendkim/TrustedHosts InternalHosts refile:/etc/opendkim/TrustedHosts KeyTable refile:/etc/opendkim/KeyTable SigningTable refile:/etc/opendkim/SigningTable Mode sv PidFile /var/run/opendkim/opendkim.pid SignatureAlgorithm rsa-sha256
UserID opendkim:opendkim Socket inet:12301@localhost
3.3 編輯/etc/default/opendkim,註釋掉原用的SOCKET配置,增加新配置:
SOCKET="inet:12301@localhost"
3.4 編輯/etc/postfix/main.cf, 增加如下配置:
milter_protocol = 6 milter_default_action = accept
其中, 當postfix版本為2.6+,milter_protocol=6; 版本為2.3到2.5,milter_protocol=2;
查看postfix版本信息:
$ postconf -d | grep mail_version
如果已經有smtpd_milters 和 non_smtpd_milters配置,追加如下:
smtpd_milters = unix:/spamass/spamass.sock, inet:localhost:12301 non_smtpd_milters = unix:/spamass/spamass.sock, inet:localhost:12301
如果沒有相關配置,直接用:
smtpd_milters = inet:localhost:12301 non_smtpd_milters = inet:localhost:12301
3.5 執行如下命令
$ sudo mkdir /etc/opendkim $ sudo mkdir /etc/opendkim/keys
3.6 創建 /etc/opendkim/TrustedHosts , 前三行不要改動
127.0.0.1 localhost 192.168.0.1/24 *.example.com
3.7 創建/etc/opendkim/KeyTable
mail._domainkey.example.com example.com:mail:/etc/opendkim/keys/example.com/mail.private
3.8 創建/etc/opendkim/SigningTable
*@example.com mail._domainkey.example.com
3.9 執行如下命令:
$ cd /etc/opendkim/keys $ sudo mkdir example.com $ cd example.com $ sudo opendkim-genkey -s mail –d example.com $ sudo chown opendkim:opendkim mail.private
3.10 接下來創建一個DNS TXT記錄:
其中,名字為mail._domainkey
值要參考/etc/opendkim/keys/example.com/mail.txt, 格式為:
v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC5N3lnvvrYgPCRSoqn+awTpE+iGYcKBPpo8HHbcFfCIIV10Hwo4PhCoGZSaKVHOjDm4yefKXhQjM7iKzEPuBatE7O47hAx1CJpNuIdLxhILSbEmbMxJrJAG0HZVn8z6EAoOHZNaPHmK2h4UUrjOG8zA5BHfzJf7tGwI+K619fFUwIDAQAB
3.11重啟服務,即可。
$ sudo service postfix restart $ sudo service opendkim restart
如有錯誤,請查看日誌:/var/log/mail.err 和/var/log/mail.log
參考文檔:
[1]“如何在Ubuntu 16.04上安裝並配置Postfix作為只發送SMTP服務器” , http://blog.csdn.net/zstack_org/article/details/69525954
[2]“email基礎篇 SPF設置說明”, https://tieba.baidu.com/p/3166555301?red_tag=1967972912
[3]“用SSL對郵件加密的支持 Postfix+SSL配置”, http://shellyli.iteye.com/blog/1534717
Ubuntu16.04搭建Postfix作為SMTP服務器