码迷,mamicode.com
首页 > 其他好文 > 详细

RHEL6 postfix+dovecot 邮件服务配置

时间:2015-03-30 19:06:25      阅读:415      评论:0      收藏:0      [点我收藏+]

标签:postfix rhel6 dovecot

环境:RHEL6.6

软件:postfix-3.0.0  bind-9.8.2-0.30 mysql-5.5.42  dovecot-2.0.9-7

软件源:本地源,epel源,remi源


邮件服务器IP:192.168.1.110 

主机名为mail.mingxiao.info

客户端   IP 192.168.1.77  WinXP,Outlook Express收发邮件


请确保selinux处于permissive状态,防火墙处于关闭状态。


添加几个测试用户,为了测试方便,密码和用户名相同

[root@mail ~]# useradd jianpan

[root@mail ~]# passwd jianpan

[root@mail ~]# useradd shubiao

[root@mail ~]# passwd shubiao


步骤1:配置DNS

[root@mail ~]# yum install bind bind-utils

编辑/etc/named.conf,将options内去掉 listen-on port 53 { 127.0.0.1; }; listen-on-v6 port 53 { ::1; };这两项,如下:

options {
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        allow-query     { localhost; };
        recursion yes;
        ......

编辑/etc/named.rfc1912.zones,在尾部添加如下内容:

zone "mingxiao.info" IN {
        type master;
        file "mingxiao.info.zone";
        allow-update { none; };
        allow-transfer { none; };
};
zone "1.168.192.in-addr.arpa" IN {
        type master;
        file "192.168.1.zone";
        allow-update { none; };
        allow-transfer { none; };
};

新建/var/named/mingxiao.info.zone  并添加如下内容:

$TTL 86400
@       IN      SOA     ns.mingxiao.info        admin.mingxiao.info. (
                        20150526
                        2H
                        10M
                        3D
                        1D )
        IN      NS      ns
        IN      MX 10   mail
ns      IN      A       192.168.1.110
mail    IN      A       192.168.1.110

新建/var/named/192.168.1.zone  并添加如下内容

$TTL 86400
@       IN      SOA     ns.mingxiao.info        admin.mingxiao.info. (
                        20150526
                        2H
                        10M
                        3D
                        1D )
        IN      NS      ns.mingxiao.info.
110      IN      PTR     ns.mingxiao.info.
110      IN      PTR    mail.mingxiao.info.

更改权限及属组

[root@mail named]# chmod 640 /var/named/mingxiao.info.zone 

[root@mail named]# chmod 640 /var/named/192.168.1.zone 

[root@mail named]# chgrp named 192.168.1.zone mingxiao.info.zone


DNS指向自己主机

[root@mail named]# echo "nameserver 192.168.1.110" > /etc/resolv.conf


启动named,并测试解析是否正常,请确保解析正常。

[root@mail named]# service named start

[root@mail named]# dit -t A mail.mingxiao.info

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.30.rc1.el6 <<>> -t A mail.mingxiao.info
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 56173
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1

;; QUESTION SECTION:
;mail.mingxiao.info. IN A

;; ANSWER SECTION:
mail.mingxiao.info. 86400 IN A 192.168.1.110

;; AUTHORITY SECTION:
mingxiao.info. 86400 IN NS ns.mingxiao.info.

;; ADDITIONAL SECTION:
ns.mingxiao.info. 86400 IN A 192.168.1.110

;; Query time: 0 msec
;; SERVER: 192.168.1.110#53(192.168.1.110)
;; WHEN: Mon Mar 30 16:19:25 2015
;; MSG SIZE  rcvd: 85


步骤2:安装mysql

[root@mail ~]# yum install mysql-server mysql-devel

[root@mail ~]# service mysqld restart


步骤3:安装postfix

编译安装postfix,首先关闭原先postfix,并改名即可。

[root@mail ~]# service postfix stop

Shutting down postfix:

[root@mail ~]# mv /etc/init.d/postfix /etc/init.d/postfix.old

删除原来的postfix用户及组,并重新添加。

[root@mail ~]# userdel del postfix

[root@mail ~]# groupadd -g 2525 postfix

[root@mail ~]# useradd -g postfix -u 2525 -s /sbin/nologin -M postfix


[root@mail ~]# tar xf postfix-3.0.0.tar.gz

[root@mail ~]# cd postfix-3.0.0

make makefiles ‘CCARGS=-DHAS_MYSQL -I/usr/include/mysql -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl  -DUSE_TLS ‘

 ‘AUXLIBS=-L/usr/lib/mysql -lmysqlclient -lz -lm -L/usr/lib/sasl2 -lsasl2  -lssl -lcrypto‘

make 

make install

注意:make install 过程中会让您输入一些路径,全部敲回车使用默认即可。


提供SysV风格的服务启动脚本,新建/etc/init.d/postfix文件,并加入以下内容:

#!/bin/bash
#
# postfix      Postfix Mail Transfer Agent
#
# chkconfig: 2345 80 30
# description: Postfix is a Mail Transport Agent, which is the program #              that moves mail from one machine to another.
# processname: master
# pidfile: /var/spool/postfix/pid/master.pid
# config: /etc/postfix/main.cf
# config: /etc/postfix/master.cf
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ $NETWORKING = "no" ] && exit 3
[ -x /usr/sbin/postfix ] || exit 4
[ -d /etc/postfix ] || exit 5
[ -d /var/spool/postfix ] || exit 6
RETVAL=0
prog="postfix"
start() {
# Start daemons.
echo -n $"Starting postfix: "
        /usr/bin/newaliases >/dev/null 2>&1
/usr/sbin/postfix start 2>/dev/null 1>&2 && success || failure $"$prog start"
RETVAL=$?
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/postfix
        echo
return $RETVAL
}
stop() {
  # Stop daemons.
echo -n $"Shutting down postfix: "
/usr/sbin/postfix stop 2>/dev/null 1>&2 && success || failure $"$prog stop"
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/postfix
echo
return $RETVAL
}
reload() {
echo -n $"Reloading postfix: "
/usr/sbin/postfix reload 2>/dev/null 1>&2 && success || failure $"$prog reload"
RETVAL=$?
echo
return $RETVAL
}
abort() {
/usr/sbin/postfix abort 2>/dev/null 1>&2 && success || failure $"$prog abort"
return $?
}
flush() {
/usr/sbin/postfix flush 2>/dev/null 1>&2 && success || failure $"$prog flush"
return $?
}
check() {
/usr/sbin/postfix check 2>/dev/null 1>&2 && success || failure $"$prog check"
return $?
}
restart() {
stop
start
}
# See how we were called.
case "$1" in
  start)
start
;;
  stop)
stop
;;
  restart)
stop
start
;;
  reload)
reload
;;
  abort)
abort
;;
  flush)
flush
;;
  check)
check
;;
  status)
  status master
;;
  condrestart)
[ -f /var/lock/subsys/postfix ] && restart || :
;;
  *)
echo $"Usage: $0 {start|stop|restart|reload|abort|flush|check|status|condrestart}"
exit 1
esac
exit $?
# END

[root@mail postfix-3.0.0]# chmod +x /etc/init.d/postfix

[root@mail postfix-3.0.0]# chkconfig postfix on

[root@mail postfix-3.0.0]# service postfix start

Starting postfix:                                          [FAILED]

出现错误,首先查看日志

[root@mail postfix-3.0.0]# tail /var/log/maillog 

Mar 30 16:46:26 mail postfix/postfix-script[28627]: warning: not owned by postfix: /var/lib/postfix/./master.lock

Mar 30 16:46:26 mail postfix/postfix-script[28639]: starting the Postfix mail system

Mar 30 16:46:26 mail postfix/master[28643]: fatal: open lock file /var/lib/postfix/master.lock: cannot open file: Permission denied

Mar 30 16:46:27 mail postfix/master[28641]: fatal: daemon initialization failure

Mar 30 16:46:28 mail postfix/postfix-script[28644]: fatal: mail system startup failed

解决办法:

[root@mail postfix-3.0.0]# rm -f /var/lib/postfix/./master.lock

[root@mail postfix-3.0.0]# service postfix start

Starting postfix:                                          [  OK  ]

启动成功,查看日志,看有无错误警告信息。


[root@mail ~]# newaliases

编辑/etc/postfix/main.cf配置文件,修改以下内容:

myhostname=mail.mingxiao.info

mydomain=mingxiao.info

myorigin=$mydomain

inet_interfaces=all

mydestination=$myhostname,localhost.$mydomain,localhost,$mydomain,ns.$mydomain

mynetworks=192.168.1.0/24,127.0.0.0/8


[root@mail ~]# service postfix restart

[root@mail ~]# telnet mail.mingxiao.info 25

Trying 192.168.1.110...
Connected to mail.mingxiao.info.
Escape character is ‘^]‘.
220 mail.mingxiao.info ESMTP Postfix
helo mail.mingxiao.info
250 mail.mingxiao.info
mail from:jianpan@mingxiao.info
250 2.1.0 Ok
rcpt to:shubiao@mingxiao.info
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
How are you.
.
250 2.0.0 Ok: queued as 9A07960408
quit
221 2.0.0 Bye
Connection closed by foreign host.


查看日志发送状态信息:可以发现状态是delevered to mailbox,发送成功。

[root@mail ~]# tail /var/log/maillog

Mar 30 17:02:46 mail postfix/local[29129]: 9A07960408: to=<shubiao@mingxiao.info>, relay=local, delay=28, delays=28/0.02/0/0.01, dsn=2.0.0, status=sent (delivered to mailbox)

Mar 30 17:02:46 mail postfix/qmgr[29109]: 9A07960408: removed

Mar 30 17:02:49 mail postfix/smtpd[29121]: disconnect from ns.mingxiao.info[192.168.1.110] helo=1 mail=1 rcpt=1 data=1 quit=1 commands=5


切换到shubiao用户

[root@mail ~]# su - shubiao

[shubiao@mail ~]$ mail

Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/shubiao": 1 message 1 new
>N  1 jianpan@mingxiao.inf  Mon Mar 30 17:02  13/503
& 1
Message  1:
From jianpan@mingxiao.info  Mon Mar 30 17:02:46 2015
Return-Path: <jianpan@mingxiao.info>
X-Original-To: shubiao@mingxiao.info
Delivered-To: shubiao@mingxiao.info
Date: Mon, 30 Mar 2015 17:02:18 +0800 (CST)
From: jianpan@mingxiao.info
Status: R
How are you.

可以看到已经收到一封邮件。


在XP上用Outlook Express测试,使用shubiao账号登陆,给jianpan用户发送一封测试邮件:

技术分享

[root@mail ~]# su - jianpan

[jianpan@mail ~]$ mail    

Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/jianpan": 1 message 1 new
>N  1 hi                    Mon Mar 30 17:25  44/1534  "test"
& 1
Message  1:
From shubiao@mingxiao.info  Mon Mar 30 17:25:58 2015
Return-Path: <shubiao@mingxiao.info>
X-Original-To: jianpan@mingxiao.info
Delivered-To: jianpan@mingxiao.info
From: "hi" <shubiao@mingxiao.info>
To: <jianpan@mingxiao.info>
Subject: test
Date: Mon, 30 Mar 2015 09:25:57 +0800
Content-Type: multipart/alternative;
boundary="----=_NextPart_000_0022_01D06ACB.879E7AC0"
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 6.00.2900.5931
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6157
Status: R
Content-Type: text/plain;
charset="gb2312"
Hi.

收信成功,但此时尚不能在windows上收取邮件,远程用户接收邮件需要有MRA(mail retrieval agent)服务器,使用pop3或imap协议接收邮件,因此安装dovecot。


步骤4:安装dovecot

[root@mail ~]# yum install dovecot

编辑/etc/dovecot/dovecot.conf,取消protocol并改为protocols = pop3


[root@mail ~]# telnet mail.mingxiao.info 110

Trying 192.168.1.110...
Connected to mail.mingxiao.info.
Escape character is ‘^]‘.
+OK Dovecot ready.
USER jianpan
+OK
PASS jianpan
Connection closed by foreign host.

出错,查看日志报什么错误

[root@mail ~]# tail /var/log/maillog

Mar 30 17:35:33 mail dovecot: pop3-login: Login: user=<jianpan>, method=PLAIN, rip=192.168.1.110, lip=192.168.1.110, mpid=29824, secured

Mar 30 17:35:33 mail dovecot: pop3(jianpan): Error: user jianpan: Initialization failed: mail_location not set and autodetection failed: Mail storage autodetection failed with home=/home/jianpan

Mar 30 17:35:33 mail dovecot: pop3(jianpan): Error: Invalid user settings. Refer to server log for more information.

解决办法:mail_location没有设置,在配置文件中设置即可,即为邮件存放位置。

[root@mail ~]# vim /etc/dovecot/conf.d/10-mail.conf

mail_location = mbox:~/mail:INBOX=/var/mail/%u


[root@mail ~]# telnet mail.mingxiao.info 110

Trying 192.168.1.110...
Connected to mail.mingxiao.info.
Escape character is ‘^]‘.
+OK Dovecot ready.
USER jianpan
+OK
PASS jianpan
-ERR [IN-USE] Couldn‘t open INBOX: Internal error occurred. Refer to server log for more information. [2015-03-30 17:38:04]
Connection closed by foreign host.

出错,查看日志,发现是权限问题。

Mar 30 17:38:04 mail dovecot: pop3-login: Login: user=<jianpan>, method=PLAIN, rip=192.168.1.110, lip=192.168.1.110, mpid=29869, secured

Mar 30 17:38:04 mail dovecot: pop3(jianpan): Error: chown(/home/jianpan/mail/.imap/INBOX, -1, 12(mail)) failed: Operation not permitted (egid=500(jianpan), group based on /var/mail/jianpan)

Mar 30 17:38:04 mail dovecot: pop3(jianpan): Error: mkdir(/home/jianpan/mail/.imap/INBOX) failed: Operation not permitted

Mar 30 17:38:04 mail dovecot: pop3(jianpan): Error: Couldn‘t open INBOX: Internal error occurred. Refer to server log for more information. [2015-03-30 17:38:04]

Mar 30 17:38:04 mail dovecot: pop3(jianpan): Couldn‘t open INBOX top=0/0, retr=0/0, del=0/0, size=0

解决方法:

[root@mail ~]# vim /etc/dovecot/conf.d/10-mail.conf

mail_access_groups = mail


[root@mail ~]# telnet mail.mingxiao.info 110

Trying 192.168.1.110...
Connected to mail.mingxiao.info.
Escape character is ‘^]‘.
+OK Dovecot ready.
USER jianpan
+OK
PASS jianpan
+OK Logged in.
LIST
+OK 2 messages:
1 1522
2 1526
.

可以使用dovecot接收邮件了。


测试发邮件给shubiao,在windows使用outlook express接收邮件是否成功。

[root@mail postfix-3.0.0]# telnet 192.168.1.110 25
Trying 192.168.1.110...
Connected to 192.168.1.110.
Escape character is ‘^]‘.
220 mail.mingxiao.info ESMTP Postfix
helo mail.mingxiao.info
250 mail.mingxiao.info
mail from:jianpan@mingxiao.info
250 2.1.0 Ok
rcpt to:shubiao@mingxiao.info
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
Hahaa!  
.

在outlook express点击接收所有邮件,发现仍是不允许,查看日志信息。

Mar 30 18:04:42 mail dovecot: pop3-login: Disconnected (tried to use disabled plaintext auth): rip=192.168.1.77, lip=192.168.1.110

Mar 30 18:05:00 mail dovecot: pop3-login: Disconnected (tried to use disabled plaintext auth): rip=192.168.1.77, lip=192.168.1.110

解决办法:编辑/etc/dovecot/conf.d/10-auth.conf,将disable_plaintext_auth = no

默认的是disable_plaintext_auth=yes,如果客户端没有配置使用SSL/TLS,就不得不设置成disable_plaintext_auth=no


重启dovecot服务,重新点击接收所有邮件。接收邮件成功。

技术分享


基本邮件收发功能已经实现,下面要实现webmail功能...未完待续。

www.mingxiao.info

本文出自 “在路上” 博客,请务必保留此出处http://mingxiao.blog.51cto.com/8124243/1626402

RHEL6 postfix+dovecot 邮件服务配置

标签:postfix rhel6 dovecot

原文地址:http://mingxiao.blog.51cto.com/8124243/1626402

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!