Postfix on Ubuntu server 10.04

Postfix 是ubuntu服务器的默认MTA。快速,容易管理,安全。与 sendmail兼容。这里解释如何安装和配置postfix。解释如何使用安全连接建立起一个SMTP服务器(为了安全发送邮件)。

注意:这个指南没有说到如何建立 Postfix Virtual Domains,关于 Virtual Domains 和其他高级配置,参考 https://help.ubuntu.com/10.04/serverguide/C/postfix.html#postfix-references

安装

安装使用下面的命令行:

sudo apt-get install postfix

基本配置

配置 postfix, 运行下面的命令:

sudo dpkg-reconfigure postfix

用户界面出现,在每一屏上,选择下面的值:

Internet Site
mail.example.com
steve
mail.example.com, localhost.localdomain, localhost
No
127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 192.168.0.0/24
0
+
all

使用你接收邮件的域名来替换 mail.example.com, 实际的网络和类范围替换192.168.0.0/24 ,适当的用户名替换 steve。

现在,你可以确定使用哪一种mailbxo格式。缺省情况下,Postfix使用mbox作为默认的mailbox格式。无须直接编辑配置文件,你可以使用postconf命令来配置postfix参数。配置参数将保存在文件 /etc/postfix/main.cf 文件中。以后,你想重新配置 postfix, 你可以运行这个命令或者手动修改配置文件。

为 Maildir 配置 mailbox格式:

sudo postconf -e ‘home_mailbox = Maildir/’
这将把新邮件保存到 /home/username/Maildir , 因此你需要配置你的 Mail Deliery Agent(MDA)使用同一个路径。

SMTP 认证

SMTP-AUTH 允许客户通过一个认证机制(SASL)来标识自己。Transport Layer Security(TLS)对认证过程进行加密。一旦认证通过,SMTP服务器将允许客户来传递邮件。

1. 配置Postfix, SMTP-AUTH 使用 SASL(Devocot SASL)

sudo postconf -e ‘smtpd_sasl_type = dovecot’
sudo postconf -e ‘smtpd_sasl_path = private/auth-client’
sudo postconf -e ‘smtpd_sasl_local_domain =’
sudo postconf -e ‘smtpd_sasl_security_options = noanonymous’
sudo postconf -e ‘broken_sasl_auth_clients = yes’
sudo postconf -e ‘smtpd_sasl_auth_enable = yes’
sudo postconf -e ‘smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination’
sudo postconf -e ‘inet_interfaces = all’

注意: smtpd_sasl_path 配置是一个相对 Postfix queue 目录的路径。

2. 下一步,为TLS获得一个数字证书。参考 https://help.ubuntu.com/10.04/serverguide/C/certificates-and-security.html 获得更详细的信息。这个例子同样使用一个Certificate Authority(CA)。生成CA证书的方法,参考 https://help.ubuntu.com/10.04/serverguide/C/certificates-and-security.html#certificate-authority 。

注意:你可以从一个certificate authority 那里获得数字证书。不像 web 客户端, SMTP 客户很少抱怨 “self-signed certificates” 因此,你可以自己创建证书。参考 https://help.ubuntu.com/10.04/serverguide/C/certificates-and-security.html#creating-a-self-signed-certificate 获得更详细的信息。

3. 一旦你拥有证书,配置Postfix给进出的邮件提供TLS加密:

sudo postconf -e ‘smtpd_tls_auth_only = no’
sudo postconf -e ‘smtp_use_tls = yes’
sudo postconf -e ‘smtpd_use_tls = yes’
sudo postconf -e ‘smtp_tls_note_starttls_offer = yes’
sudo postconf -e ‘smtpd_tls_key_file = /etc/ssl/private/server.key’
sudo postconf -e ‘smtpd_tls_cert_file = /etc/ssl/certs/server.crt’
sudo postconf -e ‘smtpd_tls_loglevel = 1′
sudo postconf -e ‘smtpd_tls_received_header = yes’
sudo postconf -e ‘smtpd_tls_session_cache_timeout = 3600s’
sudo postconf -e ‘tls_random_source = dev:/dev/urandom’
sudo postconf -e ‘myhostname = mail.example.com’

4. 如果你使用自己的 Certificate Authority 来做签证,输入:

sudo postconf -e ‘smtpd_tls_CAfile = /etc/ssl/certs/cacert.pem’

更多关于证书的信息,参考 https://help.ubuntu.com/10.04/serverguide/C/certificates-and-security.html 。

注意: 运行执行所有的命令后,Postfix已经为 SMTP-AUTH 配置好了,已经为 TLS 加密创建了一个self-signed certificate 。

现在,文件 /etc/postfix/main.cf 看起来内容如下:

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/ssl/private/smtpd.key
smtpd_tls_cert_file = /etc/ssl/certs/smtpd.crt
smtpd_tls_CAfile = /etc/ssl/certs/cacert.pem
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes
smtpd_tls_session_cache_timeout = 3600s
tls_random_source = dev:/dev/urandom

———————————————-

postfix 的初始配置已经完成。运行下面的命令来重启 postfix 守护:

sudo /etc/init.d/postfix restart

Postfix支持SMTP-AUTH,基于SASL。然而,在使用SMTP-AUTH之前,仍然需要设置SASL认证。

配置SASL

Postfix 支持两种SASL实现,分别是 Cyrus SASL 和 Dovecot SASL 。要让 Dovecot SASL 能够工作,需要安装 dovecot-common 包。从一个终端提示符下输入:

sudo apt-get install dovecot-common

下一步编辑 /etc/dovecot/dovecot.conf 。在 auth default 段,取消 socket listen 的注释,并且修改如下:

socket listen {
#master {
# Master socket provides access to userdb information. It’s typically
# used to give Dovecot’s local delivery agent access to userdb so it
# can find mailbox locations.
#path = /var/run/dovecot/auth-master
#mode = 0600
# Default user/group is the one who started dovecot-auth (root)
#user =
#group =
#}
client {
# The client socket is generally safe to export to everyone. Typical use
# is to export it to your SMTP server so it can do SMTP AUTH lookups
# using it.
path = /var/spool/postfix/private/auth-client
mode = 0660
user = postfix
group = postfix
}
}

为了让 Outlook 客户端使用 SMTPAUTH,在 auth default 段增加 “login” :

mechanisms = plain login

一旦你的 Dovecot 配置完成,重启:

sudo /etc/init.d/dovecot restart

Postfix-Dovecot

使用 dovecot-postfix 包,是配置Postfix支持SMTP-AUTH的另一种方法。这个包将安装 Dovecot ,并且配置 Postfi 来用它,支持 SASL 认证和作为一个 MDA。这个同时配置 Dovecot 支持 IMAP,IMAPS,POP3和POP3S。

注意:你可能或者不可能在你的邮件服务器上运行 IMAP, IMPAS, POP3 或者 POP3S。例如,如果你配置你的服务器成为一个mail gateway,spam/virus filter等等。在这些例子中,很容易使用上面的命令来配置 Postfix 支持 SMTPAUTH。

要安装这个包,从一个终端提示符下输入:

sudo apt-get install dovecot-postfix

现在,你有个可工作的邮件服务器了,但还有一些选项,你希望更深入的自定义。例如,这个包使用来自 ssl-cert 包的 certificate 和 key ,在一个产品环境,你会使用为主机使用一个certificate和key。参考 https://help.ubuntu.com/10.04/serverguide/C/certificates-and-security.html 获得更详细的信息。

一旦你有一个主机自定义的 certificate 和 key,修改 /etc/postfix/main.cf 下面的选项:

smtpd_tls_cert_file = /etc/ssl/certs/ssl-mail.pem
smtpd_tls_key_file = /etc/ssl/private/ssl-mail.key

然后重启 Postfix :

sudo /etc/init.d/postfix restart

测试

SMTP-AUTH 配置完成,现在是时候测试了。

要查看 SMTP-AUTH 和 TLS 是否工作,运行下面的命令:

telnet mail.example.com 25

和 postfix mail server建立连接后,输入:

ehlo mail.example.com

如果你看到回显的信息中包含下面的内容,那么工作正常。输入quit退出。

250-STARTTLS
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250 8BITMIME

发现故障并修理

这一段讲述发现问题后,进行检测的一般方法。

+ Escaping chroot

出于安全的考虑,Ubuntu 的 postfix 缺省安装到一个 chroot 环境。当出现问题时,这会增加解决问题的难度。

要关闭 chroot 操作,找到 /etc/postfix/master.cf 配置文件的这一行:

smpt inet n - - - - smtpd

修改如下:

smtp inet n - n - - smtpd

然后,你使用新的配置来重启 postfix:

sudo /etc/init.d/postfix restart

+ Log File

三个文件:

/var/log/mail.log
/var/log/mail.err
/var/log/mail.warn

可使用tail -f 命令实时查看:

tail -f /var/log/mail.err

++ 修改TLS活动日志水平,设置 smtpd_tls_loglevel 选项的值,从 1 到 4。

sudo postconf -e ‘smtpd_tls_loglevel = 4′

++ 如果你无法发送或者接收某个域名的的邮件,你可以增加域名到 debug_peer_list 参数:

sudo postconf -e ‘debug_peer_list = problem.domain’

++ 你可以增加Postfix 守护进程的 verbosity,编辑 /etc/postfix/master.cf ,在条目后增加 -v。

smtp unix – – – – – smtp -v

修改配置后,需要 Postfix来重载。执行 sudo /etc/init.d/postfix reload

++ 当SASL发生问题时,需要增加日志的信息数量,在 /etc/dovecot/dovecot.conf 文件中设置下面的选项:

auth_debug=yes
auth_debug_passwords=yes

注意:修改 dovecot 的配置后,需要重载 : sudo /etc/init.d/dovecot reload

注意:上面的改动会彻底增加发送到日志文件的信息数量。记住在调试完毕后,将配置修改回来,并且进行重载。

+ References

管理一个 Postfix 服务器是非常复杂的任务。有时需要到Ubuntu社区获得更多有经验的帮助。

freenode.net 的 #ubuntu-server IRC

The Book of Postfix

http://www.postfix.org/documentation.html

This entry was posted in Ubuntu. Bookmark the permalink.

Leave a Reply