Ubuntu16.04搭建Postfix作为SMTP服务器

一、DNS配置

类型

名称    TTL 
 A mail  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 Siteexample.com 按回车。

 

2.3 基本配置

修改文件 /etc/postfix/main.cf

…

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加密

 

 

 

三、反垃圾设置:SPFDKIMDMARC

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

2email基础篇 SPF设置说明”, https://tieba.baidu.com/p/3166555301?red_tag=1967972912

3SSL对邮件加密的支持 Postfix+SSL配置”, http://shellyli.iteye.com/blog/1534717

 

 

posted @ 2018-01-09 18:13  圆旭  阅读(7800)  评论(0编辑  收藏  举报