Postfix 是一种 Mail Transfer Agent 邮件传送代理
(MTA) 是 Ubuntu 缺省的邮件代理程序. 它在 Ubuntu 的 main 仓库, 意味着它将接收到安全方面的升级。 这个指南说明了如何安装和配置 postfix 和设置为一个使用安全连接的 SMTP 服务器。
为了安装 Postfix 、 SMTP-AUTH 和 TLS 执行下列步骤:
apt-get install postfix libsasl2-2 sasl2-bin libsasl2-modules procmail
运行:
dpkg-reconfigure postfix
在被询问时,插入如下的内容 (如果你有一个域名的话,使用你的域名替换掉 server1.example.com ):
然后运行下面的命令:
postconf -e 'smtpd_sasl_local_domain =' postconf -e 'smtpd_sasl_auth_enable = yes' postconf -e 'smtpd_sasl_security_options = noanonymous' postconf -e 'broken_sasl_auth_clients = yes' postconf -e 'smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination' postconf -e 'inet_interfaces = all' echo 'pwcheck_method: saslauthd' >> /etc/postfix/sasl/smtpd.conf echo 'mech_list: plain login' >> /etc/postfix/sasl/smtpd.conf mkdir /etc/postfix/ssl cd /etc/postfix/ssl/ openssl genrsa -des3 -rand /etc/hosts -out smtpd.key 1024 chmod 600 smtpd.key openssl req -new -key smtpd.key -out smtpd.csr openssl x509 -req -days 3650 -in smtpd.csr -signkey smtpd.key -out smtpd.crt openssl rsa -in smtpd.key -out smtpd.key.unencrypted mv -f smtpd.key.unencrypted smtpd.key openssl req -new -x509 -extensions v3_ca -keyout cakey.pem -out cacert.pem -days 3650 postconf -e 'smtpd_tls_auth_only = no' postconf -e 'smtp_use_tls = yes' postconf -e 'smtpd_use_tls = yes' postconf -e 'smtp_tls_note_starttls_offer = yes' postconf -e 'smtpd_tls_key_file = /etc/postfix/ssl/smtpd.key' postconf -e 'smtpd_tls_cert_file = /etc/postfix/ssl/smtpd.crt' postconf -e 'smtpd_tls_CAfile = /etc/postfix/ssl/cacert.pem' postconf -e 'smtpd_tls_loglevel = 1' postconf -e 'smtpd_tls_received_header = yes' postconf -e 'smtpd_tls_session_cache_timeout = 3600s' postconf -e 'tls_random_source = dev:/dev/urandom' postconf -e 'myhostname = server1.example.com'
文件 /etc/postfix/main.cf 现在看起来如下:
# See /usr/share/postfix/main.cf.dist for a commented, more complete version smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu) biff = no # appending .domain is the MUA's job. append_dot_mydomain = no # Uncomment the next line to generate "delayed mail" warnings #delay_warning_time = 4h myhostname = server1.example.com alias_maps = hash:/etc/aliases alias_database = hash:/etc/aliases myorigin = /etc/mailname mydestination = server1.example.com, localhost.example.com, localhost relayhost = mynetworks = 127.0.0.0/8 mailbox_command = procmail -a "$EXTENSION" mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = all smtpd_sasl_local_domain = smtpd_sasl_auth_enable = yes smtpd_sasl_security_options = noanonymous broken_sasl_auth_clients = yes smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject _unauth_destination smtpd_tls_auth_only = no smtp_use_tls = yes smtpd_use_tls = yes smtp_tls_note_starttls_offer = yes smtpd_tls_key_file = /etc/postfix/ssl/smtpd.key smtpd_tls_cert_file = /etc/postfix/ssl/smtpd.crt smtpd_tls_CAfile = /etc/postfix/ssl/cacert.pem smtpd_tls_loglevel = 1 smtpd_tls_received_header = yes smtpd_tls_session_cache_timeout = 3600s tls_random_source = dev:/dev/urandom
重启 postfix 服务:
/etc/init.d/postfix restart
验证通过 saslauthd 完成。
我们不得不调整一些东西使它工作的更好。 因为 Postfix 运行 chrooted 在 /var/spool/postfix 我们修改一些相对路径使其在一个假想的 root 下运行。 (例如. /var/run/saslauthd 变成 /var/spool/postfix/var/run/saslauthd ):
首先我们编辑 /etc/default/saslauthd 为了激活 saslauthd. 删除 # 于 START=yes 行的前面并且正在 PWDIR, PARAMS, 和 PIDFILE 行:
# This needs to be uncommented before saslauthd will be run automatically START=yes PWDIR="/var/spool/postfix/var/run/saslauthd" PARAMS="-m ${PWDIR}" PIDFILE="${PWDIR}/saslauthd.pid" # You must specify the authentication mechanisms you wish to use. # This defaults to "pam" for PAM support, but may also include # "shadow" or "sasldb", like this: # MECHANISMS="pam shadow" MECHANISMS="pam"
笔记 : 如果你喜欢, 你可以使用 "shadow" 代替 "pam". 它将使用 MD5 来传输密码,这是一种更安全的做法。 用户名和密码的验证需要使用你服务器上的用户。
下一步, 我们更新 dpkg "状态" 于 /var/spool/portfix/var/run/saslauthd. saslauthd 初始化脚本来建立缺少的目录并拥有适当的权限:
dpkg-statoverride --force --update --add root sasl 755 /var/spool/postfix/var/run/saslauthd
dpkg-statoverride --force --update --add root sasl 755 /var/spool/postfix/var/run/saslauthd
最后,运行 saslauthd:
/etc/init.d/saslauthd start
为了查看 SMTP-AUTH 和 TLS 是否正确运行,现在运行如下命令:
telnet localhost 25
当你连接到你的 postfix 邮件服务器后,输入
ehlo localhost
如果你看到如下行
250-STARTTLS 250-AUTH
250-STARTTLS 250-AUTH
在其它的中间, 所有的事情都在正常运行.
输入 quit
返回到系统终端.
一/测试SMTP发送邮件 [root@mail ~]# telnet localhost 25 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. 220 mail.zpp.com ESMTP Postfix helo localhost //宣告客户端地址 250 mail.zpp.com mail from:[email protected] //告知服务器发件人地址 250 2.1.0 Ok rcpt to:[email protected] //告知服务器收件人地址 250 2.1.5 Ok Data //告知服务器要传送数据了 354 End data with <CR><LF>.<CR><LF> subject:I Love You! //主题 hello! You are my super star! he he he ! //邮件内容以点结束 . 250 2.0.0 Ok: queued as 27ECCE01ED Quit //退出 221 2.0.0 Bye Connection closed by foreign host. 二/测试POP3接收邮件 [root@mail ~]# telnet localhost 110 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. +OK Dovecot ready. user zpp002 //使用邮件用户“zpp002”登录 +OK pass 123.com //键入密码123.com +OK Logged in. List //查看邮件列表 +OK 1 messages: 1 485 . retr 1 //收取并查看第一封邮件内容 +OK 485 octets Return-Path: <[email protected]> X-Original-To: [email protected] Delivered-To: [email protected] Received: from localhost (localhost.localdomain [127.0.0.1]) by mail.zpp.com (Postfix) with SMTP id 27ECCE01ED for <[email protected]>; Thu, 28 Jan 2021 03:57:40 +0800 (CST) subject:I Love You! Message-Id: <[email protected]> Date: Thu, 28 Jan 2021 03:57:40 +0800 (CST) From: [email protected] To: undisclosed-recipients:; hello! You are my super star! he he he ! . quit +OK Logging out. Connection closed by foreign host.
这些指南将教会你如何使用 Postfix 邮件服务器从基本的到高级设置。
Postfix 基本设置指南 将教会你 Posfix 的概念和如果做 Postfix 的基本设置和运行. 如果你是 Postfix 新手,推荐首先阅读这个指南.
Postfix 虚拟邮箱和病毒过滤指南 将教会你如何设置一个虚拟邮箱使用 non-linux 帐号,每个用户将使用他们的 e-mail 地址和 POP3/IMAP 服务来验证 和 ClamSMTP 防毒软件过滤所有发进发出邮件已知的病毒.
Postfix 完整虚拟邮件系统指南 将帮助你,如果你作为一个 ISP 管理一个大的虚拟域名,或者在一个大公司里面需要管理多个邮件域名,这个手册是适合的,你将看到如下的方法: