注:本文介绍的是有关软件的安装过程和配置方法,不涉及原理介绍。如要了解邮件系统的运行原理,请参考附件中的链接。文中涉及技术和资料来源于网络,非本人原创,本文仅供个人总结和学习参考。
首先介绍下搭建所需的软件和部署环境:
MTA: Postfix 3.0.3
SASL: Cyrus-sasl 2.1.23 ; Courier-authlib 0.66.1(Cyrus-sasl使用Courier-authlib与MySQL建立关联)
MDA: Dovecot 2.0.9
DataBase: MySQL 5.1.73
WebMail: ExtMail 1.2(普通用户界面); ExtMan 1.1(管理员界面)
Web服务器(用于发布WebMail程序): Apache 2.2.15
服务器操作系统版本:RHEL 6.5
下面介绍搭建步骤:
1.通过yum安装mysql服务器和开发包
由于postfix的编译安装的配置参数中需要链接MySQL的开发头文件、库文件和SASL的开发头文件,因此首先安装mysql-server和mysql-devel包:
[[email protected] postfix-3.0.3]# yum install mysql-server [[email protected] postfix-3.0.3]# yum install mysql-devel
2.通过yum安装cyrus-sasl包和cyrus-sasl-devel包
[[email protected] postfix-3.0.3]# yum install cyrus-sasl [[email protected] postfix-3.0.3]# yum install cyrus-sasl-devel
3.编译安装postfix
在RHEL6.5上,默认安装了postfix,并通过postfix来发送邮件:
[[email protected] ~]# netstat -tunlp | grep 25 tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 3510/master tcp 0 0 ::1:25 :::* LISTEN 3510/master [[email protected] ~]# ps -ef | grep 3510 root 3510 1 0 2014 ? 00:01:11 /usr/libexec/postfix/master postfix 3521 3510 0 2014 ? 00:00:15 qmgr -l -t fifo -u postfix 11818 3510 0 20:41 ? 00:00:00 pickup -l -t fifo -u root 11989 11958 0 21:43 pts/0 00:00:00 grep 3510
但是默认安装的postfix,其配置的属性参数可能不满足搭建邮件服务器的要求,因此本文建议将其卸载掉,重新通过编译安装包的方式进行安装。
首先停止服务,卸载postfix安装包:
[[email protected] ~]# service postfix stop Shutting down postfix: [ OK ] [[email protected] ~]# rpm -e postfix
然后去postfix的官网(http://www.postfix.org/)下载安装包并上传到服务器,解压并进行编译安装,在编译时注意配置mysql和cyrus-sasl所提供的头文件和库文件所在的路径:
[[email protected] ~]# ls -l postfix-3.0.3.tar.gz -rw-r--r--. 1 root root 4240588 Dec 20 09:35 postfix-3.0.3.tar.gz[[email protected] ~]# tar -xf postfix-3.0.3.tar.gz [[email protected] ~]# cd postfix-3.0.3/ [[email protected] postfix-3.0.3]# make makefiles ‘CCARGS=-DHAS_MYSQL -I /usr/include/mysql -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I /usr/include/sasl -DUSE_TLS‘‘AUXLIBS= -L/usr/lib64/mysql-lmysqlclient -lz -lrt -lm -L/usr/lib64/sasl2 -lsasl2 -lssl -lcrypto‘
之后执行make&&make install操作,在执行make install操作后,安装程序会进行交互,要求对postfix的运行参数进行一些配置:
[[email protected] postfix-3.0.3]# make && make install ... Please specify the prefix for installed file names. Specify this ONLY if you are building ready-to-install packages for distribution to OTHER machines. See PACKAGE_README for instructions. install_root: [/] Please specify a directory for scratch files while installing Postfix. You must have write permission in this directory. tempdir: [/root/postfix-3.0.3] /tmp/postfix Please specify the final destination directory for installed Postfix configuration files. config_directory: [/etc/postfix] Please specify the final destination directory for installed Postfix administrative commands. This directory should be in the command search path of adminstrative users. command_directory: [/usr/sbin] Please specify the final destination directory for installed Postfix daemon programs. This directory should not be in the command search path of any users. daemon_directory: [/usr/libexec/postfix] Please specify the final destination directory for Postfix-writable data files such as caches or random numbers. This directory should not be shared with non-Postfix software. data_directory: [/var/lib/postfix] Please specify the final destination directory for the Postfix HTML files. Specify "no" if you do not want to install these files. html_directory: [no] Please specify the owner of the Postfix queue. Specify an account with numerical user ID and group ID values that are not used by any other accounts on the system. mail_owner: [postfix] Please specify the final destination pathname for the installed Postfix mailq command. This is the Sendmail-compatible mail queue listing command. mailq_path: [/usr/bin/mailq] Please specify the final destination directory for the Postfix on-line manual pages. You can no longer specify "no" here. manpage_directory: [/usr/local/man] Please specify the final destination pathname for the installed Postfix newaliases command. This is the Sendmail-compatible command to build alias databases for the Postfix local delivery agent. newaliases_path: [/usr/bin/newaliases] Please specify the final destination directory for Postfix queues. queue_directory: [/var/spool/postfix] Please specify the final destination directory for the Postfix README files. Specify "no" if you do not want to install these files. readme_directory: [no] Please specify the final destination pathname for the installed Postfix sendmail command. This is the Sendmail-compatible mail posting interface. sendmail_path: [/usr/sbin/sendmail] Please specify the group for mail submission and for queue management commands. Specify a group name with a numerical group ID that is not shared with other accounts, not even with the Postfix mail_owner account. You can no longer specify "no" here. setgid_group: [postdrop] Please specify the final destination directory for Postfix shared-library files. shlib_directory: [no] Please specify the final destination directory for non-executable files that are shared among multiple Postfix instances, such as postfix-files, dynamicmaps.cf, as well as the multi-instance template files main.cf.proto and master.cf.proto. meta_directory: [/etc/postfix] Updating /etc/postfix/main.cf.proto... ...
上述过程中要求配置参数中,以下几个配置项需要注意:
mail_owner: postfix中邮件队列的所有者,要求是系统用户的UID,因此通常是在系统中新建一个用户名为postix的用户,并将用户名填入此项。
setgid_group:
需要设置成一个不同于mail_owner用户名不同的组名。
因此为了稳妥起见,可以新建2个普通用户/用户组postfix和postdrop,需要注意的是RHEL6.5上已存在postfix用户名/组,该用户/组的UID/GID小于500,为特权用户/组,请先删除该用户/组后再添加:
[[email protected] postfix-3.0.3]# userdel postfix[[email protected] postfix-3.0.3]# userdel postdrop[[email protected] postfix-3.0.3]# groupdel postfix[[email protected] postfix-3.0.3]# groupdel postdrop[[email protected] postfix-3.0.3]# groupadd -g 2525 postfix[[email protected] postfix-3.0.3]# useradd -g postfix -u 2525 -s /sbin/nologin -M postfix[[email protected] postfix-3.0.3]# groupadd -g 2526 postdrop[[email protected] postfix-3.0.3]# useradd -g postdrop -u 2526 -s /sbin/nologin -M postdrop[[email protected] postfix-3.0.3]# id postfixuid=2525(postfix) gid=2525(postfix) groups=2525(postfix),12(mail)[[email protected] postfix-3.0.3]# id postdropuid=2526(postdrop) gid=2526(postdrop) groups=2526(postdrop)
请注意,在部署邮件服务器前,系统中已有部分文件/目录属主属组为postfix或postdrop,如果遇到相关文件/目录时,请将这些文件/目录的属主属组修改成新的postfix/postdrop的uid和gid。
4.配置postfix并测试邮件发送
postfix有两个主要的配置文件,master.cf和maincf,其中:
postfix的服务是由多个daemon进程来协调完成的,master.cf定义了为实现特定的要求,哪些进程可以运行,将以何种方式运行。
main.cf则定义了postfix运行的环境参数。
为保证postfix可以发送邮件,应对main.cf如下参数进行配置:
myhostname: 邮件系统主机名,应与操作系统的主机名相一致。
myorigin: 邮件后缀名
mydomain: 邮件域名
mydestination: 定义接收邮件的后缀名
mynetworks: 为其进行中继的网段
配置如下:
[[email protected] ~]# cat /etc/postfix/main.cf | grep "^my" myhostname = mail.asika.com mydomain = asika.com myorigin = $mydomain mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain mynetworks = 127.0.0.0/8 # 只给通过127.0.0.0,即只给来自本机的用户中继邮件
配置完成,重启postfix服务:
[[email protected] ~]# postfix stop;postfix start postfix/postfix-script: stopping the Postfix mail system postfix/postfix-script: starting the Postfix mail system
注:如果想通过SysV方式启停postfix服务,则可以搜索postfix SysV服务脚本,并使用chkconfig命令来添加postfix服务以及配置在对应的runlevel上启停postfix服务。
重启完postfix服务后,可以看到postfix服务默认监听在25端口上:
[[email protected] ~]# netstat -tunlp | grep master tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 22279/master [[email protected] ~]# ps -ef | grep 22279 root 22279 1 0 21:30 ? 00:00:00 /usr/libexec/postfix/master -w postfix 22280 22279 0 21:30 ? 00:00:00 pickup -l -t unix -u postfix 22281 22279 0 21:30 ? 00:00:00 qmgr -l -t unix -u root 22406 22040 0 21:35 pts/1 00:00:00 grep 22279
使用telnet访问25号端口,可以对postfix发送邮件功能进行测试:
[[email protected] ~]# telnet 127.0.0.1 25 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is ‘^]‘. 220 mail.asika.com ESMTP Postfix ehlo mail.asika.com 250-mail.asika.com 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-AUTH LOGIN PLAIN 250-AUTH=LOGIN PLAIN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN mail from:root 250 2.1.0 Ok rcpt to:root 250 2.1.5 Ok data 354 End data with <CR><LF>.<CR><LF> subject:hello 123 . 250 2.0.0 Ok: queued as 88B343AB
[[email protected] ~]# mail Heirloom Mail version 12.4 7/29/08. Type ? for help. "/var/spool/mail/root": 7 messages 1 new 4 unread 1 [email protected] Wed Dec 11 09:25 402/30164 "[abrt] full crash report" U 2 [email protected] Wed Dec 11 09:30 388/29682 "[abrt] full crash report" 3 [email protected] Wed Dec 11 09:52 17/714 "*** SECURITY information for localhost.localdomain ***" U 4 [email protected] Thu Dec 12 11:42 402/30159 "[abrt] full crash report" U 5 [email protected] Fri Feb 28 10:29 402/30010 "[abrt] full crash report" 6 [email protected] Fri Feb 28 10:51 388/29615 "[abrt] full crash report" >N 7 [email protected] Mon Jan 4 21:43 14/420 "hello" & 7 Message 7: From [email protected] Mon Jan 4 21:43:14 2016 Return-Path: <[email protected]> X-Original-To: root Delivered-To: [email protected] subject:hello Date: Mon, 4 Jan 2016 21:42:41 +0800 (CST) From: [email protected] Status: R 123
[[email protected] ~]# telnet 127.0.0.1 25 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is ‘^]‘. 220 mail.asika.com ESMTP Postfix ehlo mail.asika.com 250-mail.asika.com 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-AUTH LOGIN PLAIN 250-AUTH=LOGIN PLAIN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN 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:hello 123 . 250 2.0.0 Ok: queued as CE65F35F
查看/var/log/maillog日志,发现postfix发送了邮件,当收件人地址在本地时,会将邮件投递到本地用户的mailbox中,如果收件人地址后缀非本机所在域名,会尝试访问地址后缀对应的网址,试图为该邮件进行中继:
Jan 4 21:39:55 mail postfix/smtpd[22508]: connect from localhost[127.0.0.1] Jan 4 21:41:09 mail postfix/smtpd[22508]: disconnect from localhost[127.0.0.1] quit=1 commands=1 Jan 4 21:42:23 mail postfix/smtpd[22508]: connect from localhost[127.0.0.1] Jan 4 21:42:51 mail postfix/smtpd[22508]: 88B343AB: client=localhost[127.0.0.1] Jan 4 21:43:14 mail postfix/cleanup[22648]: 88B343AB: message-id=<[email protected]> Jan 4 21:43:14 mail postfix/qmgr[22281]: 88B343AB: from=<[email protected]>, size=303, nrcpt=1 (queue active) Jan 4 21:43:14 mail postfix/local[22667]: warning: dict_nis_init: NIS domain name not set - NIS lookups disabled Jan 4 21:43:14 mail postfix/local[22667]: 88B343AB: to=<[email protected]>, orig_to=<root>, relay=local, delay=33, delays=32/0.09/0/0.03, dsn=2.0.0, status=sent (delivered to mailbox) Jan 4 21:43:14 mail postfix/qmgr[22281]: 88B343AB: removed
[[email protected] ~]# tail /var/log/maillog Jan 4 21:30:38 mail postfix/master[22279]: daemon started -- version 3.0.1, configuration /etc/postfix Jan 4 21:38:44 mail postfix/smtpd[22508]: warning: dict_nis_init: NIS domain name not set - NIS lookups disabled Jan 4 21:38:44 mail postfix/smtpd[22508]: connect from localhost[127.0.0.1] Jan 4 21:39:11 mail postfix/smtpd[22508]: CE65F35F: client=localhost[127.0.0.1] Jan 4 21:39:44 mail postfix/cleanup[22512]: CE65F35F: message-id=<[email protected]> Jan 4 21:39:44 mail postfix/qmgr[22281]: CE65F35F: from=<[email protected]>, size=299, nrcpt=1 (queue active) Jan 4 21:39:44 mail postfix/smtp[22531]: CE65F35F: to=<[email protected]>, relay=none, delay=40, delays=40/0.13/0/0, dsn=4.4.3, status=deferred (Host or domain name not found. Name service error for name=c.com type=MX: Host not found, try again)
下一篇文章将介绍dovecot的安装和配置,通过dovecot来接收邮件、以及使用sasl来对postfix的收件人进行验证。