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

1、DNS配置shell

类型服务器

名称    TTL 
 A mail  128.199.254.32  1小时 

MXdom

 @ mail.example.com(优先:10) 1小时 

TXToop

@  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记录,post

两个TXT记录用于“反垃圾邮件”,其中DKIM须要本机配置,后文会详细说明。测试

 

 

2、Postfix安装及配置加密

2.1 事先设置好hostname的话,Postfix能够自动配置好不少参数,节省时间。spa

# echo “example.com” > /etc/hostname

 

2.2 安装postfix.net

# apt update
# apt install mailutils

 

默认选项为Internet Siteexample.com 按回车。unix

 

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

 

 

 

3、反垃圾设置: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

相关文章
相关标签/搜索