活到老学到老  

记录遇到问题的点点滴滴。

【CentOS邮件系统搭建三】 Postfix和SMTP认证和TLS开启

7年前发布  · 3253 次阅读
  smtp  Postfix  ssl  tls  Auth 

1、安装所有需要的软件包

yum -y install cyrus-sasl cyrus-sasl-devel cyrus-sasl-gssapi cyrus-sasl-md5 cyrus-sasl-plain postfix bind-utils

postfix配置文件备份

cp /etc/postfix/main.cf /etc/postfix/main.cf_orig

2. 使用postconf配置SMTP认证和TLS

/usr/sbin/postconf -e 'smtpd_sasl_local_domain ='
/usr/sbin/postconf -e 'smtpd_sasl_auth_enable = yes'
/usr/sbin/postconf -e 'smtpd_sasl_security_options = noanonymous'
/usr/sbin/postconf -e 'broken_sasl_auth_clients = yes'
/usr/sbin/postconf -e 'smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination'
/usr/sbin/postconf -e 'inet_interfaces = all'
/usr/sbin/postconf -e 'mynetworks = 127.0.0.0/8, 10.50.1.0/24'

3. 设置postfix登录

vim /etc/sasl2/smtpd.conf
pwcheck_method: saslauthd
mech_list: plain login

4. 创建SSL证书签名

mkdir /etc/postfix/ssl
cd /etc/postfix/ssl/
openssl genrsa -des3 -rand /etc/hosts -out smtpd.key 1024
chmod 600 smtpd.key

5. 创建密钥的签名请求

openssl req -new -key smtpd.key -out smtpd.csr


6. 创建签名请求和密钥的SSL证书

openssl x509 -req -days 3650 -in smtpd.csr -signkey smtpd.key -out smtpd.crt


7. 创建RSA密钥

openssl rsa -in smtpd.key -out smtpd.key.unencrypted
mv smtpd.key.unencrypted smtpd.key


8. 创建CA密钥和证书

openssl req -new -x509 -extensions v3_ca -keyout cakey.pem -out cacert.pem -days 3650


9. 配置postfix的TLS

/usr/sbin/postconf -e 'smtpd_tls_auth_only = no'
/usr/sbin/postconf -e 'smtp_use_tls = yes'
/usr/sbin/postconf -e 'smtpd_use_tls = yes'
/usr/sbin/postconf -e 'smtp_tls_note_starttls_offer = yes'
/usr/sbin/postconf -e 'smtpd_tls_key_file = /etc/postfix/ssl/smtpd.key'
/usr/sbin/postconf -e 'smtpd_tls_cert_file = /etc/postfix/ssl/smtpd.crt'
/usr/sbin/postconf -e 'smtpd_tls_CAfile = /etc/postfix/ssl/cacert.pem'
/usr/sbin/postconf -e 'smtpd_tls_loglevel = 1'
/usr/sbin/postconf -e 'smtpd_tls_received_header = yes'
/usr/sbin/postconf -e 'smtpd_tls_session_cache_timeout = 3600s'
/usr/sbin/postconf -e 'tls_random_source = dev:/dev/urandom'


10. 配置postfix主机名和域名

/usr/sbin/postconf -e 'myhostname = vsv01.atbnet.local'
/usr/sbin/postconf -e 'mydomain = atbnet.local'


11. 检查postfix配置文件

more /etc/postfix/main.cf


12. 添加域名A记录
smtp IN A 10.50.1.50


13. 停止sendmail 启动postfix, saslauthd

/etc/init.d/sendmail stop
/etc/init.d/postfix start
/etc/init.d/saslauthd start


14. 检查maillog 错误/故障和启动日志

tail /var/log/maillog
....
Mar 10 04:21:55 vsv01 sendmail[6074]: alias database /etc/aliases rebuilt by andy
Mar 10 04:21:55 vsv01 sendmail[6074]: /etc/aliases: 76 aliases, longest 10 bytes, 765 bytes total
Mar 10 04:21:55 vsv01 postfix/postfix-script: starting the Postfix mail system
Mar 10 04:21:55 vsv01 postfix/master[6120]: daemon started -- version 2.3.3, configuration /etc/postfix
....

15. 配置服务启动所需的运行等级

/sbin/chkconfig --level 345 sendmail off
/sbin/chkconfig --level 345 postfix on
/sbin/chkconfig --level 345 saslauthd on


16. 测试postfix是否运行和SMTP的认证/TLS

telnet localhost 25

Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
220 vsv01.atbnet.local ESMTP Postfix
ehlo localhost
250-vsv01.atbnet.local
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
quit
221 2.0.0 Bye
Connection closed by foreign host.

If the below is in the statement returned by the server then TLS and PLAIN/LOGIN logins are configured correctly:
250-STARTTLS
250-AUTH PLAIN LOGIN

 

17. 检查防火墙路由允许25端口

/sbin/iptables -nvL
vim /etc/sysconfig/iptables

 

-A INPUT -i lo -j ACCEPT
-A INPUT -s 10.50.1.0/255.255.255.0 -p tcp --dport 25 -j ACCEPT