linux 下 搭建邮件邮件服务器(Postfix+Dovecot)(一)-系统账户登陆收发邮件

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

linux 下 搭建邮件邮件服务器(一)-系统账户登陆收发邮件

一、安装环境

1
2
3
4
5
6
7
# lsb_release -d
Description:    CentOS release 6.5 (Final)
# uname -i
x86_64
# ifconfig |grep 'inet addr'|grep Bcast|awk -F 'Bcast' '{print $1}'|awk -F ':' '{print $2}'
192.168.151.140  
101.230.205.140


二、安装前的准备工作

安装前说明:邮件服务依赖于DNS服务,请事先确信您的DNS服务已经为邮件应用配置完成。

1、安装所需的rpm包,这包括以下这些:

1
2
3
4
httpd,mysql,mysql-server,mysql-devel,openssl-devel,dovecot,perl-DBD-Mysql,tcl,tcl-devel,libart_lgpl,libart_lgpl-
devel,libtool-ltdl,libtool-ltdl-devel,expect 
# yum install httpd mysql mysql-server mysql-devel openssl-devel dovecot perl-DBD-Mysql tcl tcl-devel libart_lgpl 
libart_lgpl-devel libtool-ltdl libtool-ltdl-devel expect -y

2、关闭sendmail,并将它的随系统启动的功能关闭:

1
2
# service sendmail stop
# chkconfig sendmail off

若sendmail未安装,跳过此步骤。

3、 安装以下开发所用到的rpm包组:

1
Development libraries



1
2
3
Server platform Development
Desktop platform Development
Development Tools

方法:

1
# yum groupinstall "packge_group_name"



三、启动依赖的服务
1、启动mysql数据库,并给mysql的root用户设置密码:

1
2
3
# service mysqld start 
# chkconfig mysqld on
# mysqladmin -uroot password 'your_password'

2、启动saslauthd服务,并将其加入到自动队列:

1
2
# service saslauthd start
# chkconfig saslauthd on

四、安装配置postfix
如果发现postfix已安装,请用下列命令卸载。

1
2
3
4
5
6
7
8
9
10
11
# rpm -e  postfix --nodeps
# userdel postfix
# groupdel postdrop
groupadd -g 2525 postfix
useradd  -g postfix -u 2525 -s  /sbin/nologin  -M  postfix
groupadd -g 2526 postdrop
useradd  -g postdrop -u 2526 -s  /sbin/nologin  -M postdrop
# rpm -ql mysql
# rpm -ql mysql-devel
# rpm -ql cyrus-sasl
# rpm -ql cyrus-sasl-devel


安装postfix-2.10.10.tar.gz

1
2
3
4
5
6
# tar zxvf postfix-2.10.10.tar.gz 
# cd postfix-2.10.10
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 -lm -L//usr/lib64/sasl2 -lsasl2 -lssl -lcrypto'
make 
make  install

按照以下的提示输入相关路径([]号的是缺省值,"]"后的是输入值,省略的表示采用默认值)


生成别名二进制文件:

1
# newaliases



2.进行一些基本配置,测试启动postfix并进行发信

1
# vim /etc/postfix/main.cf


修改以下几项为您需要的配置

1
2
3
4
5
myhostname = mail.10fei3. top
mydomain = 10fei3. top
myorigin = 10fei3. top
mydestination = $myhostname, localhost.$mydomain,localhost,$mydomain
mynetworks = 192.168.1.0 /24 , 127.0.0.0 /8


说明:
myorigin参数用来指明发件人所在的域名,即做发件地址的伪装;
mydestination参数指定postfix接收邮件时收件人的域名,即您的postfix系统要接收到哪个域名的邮件; 
myhostname参数指定运行postfix邮件系统的主机的主机名,默认情况下,其值被设定为本地机器名;
mydomain 参数指定您的域名,默认情况下,postfix将myhostname 的第一部分删除而作为mydomain的值;
mynetworks 参数指定你所在的网络的网络地址,postfix系统根据其值区别用户是远程的还是本地的,如果是本地网络用户则允许其访问;
inet_interfaces 参数指定postfix系统监听的网络接口;

注意:
1、在postfix的配置文件中,参数行和注释行是不能处在同一行中的;
2、任何一个参数的值都不需要加引号,否则,引号将会被当作参数值的一部分来使用;
3、每修改参数即其值后执行postfix relaod 即可令其生效;但若修改了inet_interfaces,则需要重新启动postfix;
4、如果一个参数的值有多个,可以将他们放在不同的行中,只需要在其后的每个行前多置一个空格即可;postfix会把第一个字符为空格或tab的文

本行视为上一行的延续;

五、为postfix提供Sysv服务脚本/etc/rc.d/init.d/postfix,内容如下(#END 之前)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/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.
#processbname: 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
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 $ "$prong 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 $ "$prong 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 $ "$prong reload"
     RETVAL=$?
     echo
     return  $RETVAL
}
abort() {
     /usr/sbin/postfix  abort 2> /dev/null  1>&2 && success || failure $ "$prong abort"
     return  $?
}
flush() {
     /usr/sbin/postfix  flush 2> /dev/null  1>&2 && success || failure $ "$prong flush"
     return  $?
}
check() {
     /usr/sbin/postfix  check 2> /dev/null  1>&2 && success || failure $ "$prong check"
     return  $?
}
resatrt() {
     stop
     start
}
# See how we were called.
case  "$1"  in
   start)
       start
       ;;
   stop)
       stop
       ;;
   restart)
       resatrt
       ;;
   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

为此脚本赋予执行权限:

1
2
3
# chmod +x /etc/rc.d/init.d/postfix
# chown postfix.postfix -R /var/lib/postfix
# chown postfix.postfix -R /var/spool/postfix


将postfix服务添加至服务列表:

1
# chkconfig --add postfix



设置其开机自动启动:

1
# chkconfig postfix on



使用此脚本重新启动服务,以测试其能否正常执行:

1
2
# service postfix restart 
# yum install -y telnet

此时可使用本地用户测试邮件收发了。


六、实现postfix基于客户端的访问控制

1、基于客户端的访问控制概览

postfix内置了多种反垃圾邮件的机制,其中包括"客户端"发邮件的限制。客户端判别机制可以设定一系列客户信息的判别条件:

1
2
3
4
5
smtpd_client_restrictions
smtpd_data_restrictions
smtpd_helo_restrictions
smtpd_recipient_restrictions
smtpd_sender_restrictions

上面的每一项参数分别用于检查smtp会话过程中的特定阶段,即客户端提供相应信息阶段,如客户端发起连接请求时,postfix就可以根据配置文件

中定义的smtpd_client_restrictions参数来判别此客户端IP的访问权限。相应的,smtpd_helo_restrictions则用于用户的helo信息判别客户端

的访问能力等等。

如果DATA命令之前的所有内容都被接受,客户端接着就可以传送邮件内容了。邮件内容通常由两部分组成,前半部分是标题(header),其可以由

header_check过滤,后半部分是邮件正文(body),其可以由check_body过滤,这两项实现的是邮件“内容检查”。

postfix的默认配置如下:

1
2
3
4
5
6
7
smtpd_client_restrictions = 
smtpd_data_restrictions = 
smtpd_end_of_data_restrictions = 
smtpd_etrn_restrictions = 
smtpd_helo_restrictions =
smtpd_recipient_restrictions = permit_mynetworks,reject_unauth_destination
smtpd_sender_restrictions =

这里限制了只有mynetworks参数中定义的本地网络中的客户端才能通过postfix转发邮件,其他客户端则不被允许,从而关闭了开放式中继(open relay)的功能。

postfix有很多个内置的限制条件,如上面的permit_mynetworks和reject_unauth_destination,但管理员也可以使用访问表(access map)来自

定义限制条件。自定义访问表的条件通常使用check_client_access,
check_helo_access,check_sender_access,check_recipient_access进行,它们后面通常跟上type:mapname格式的访问表类型和名称。其中,

check_sender_access和check_recipient_access用来检查客户端提供的邮件地址,因此,其访问表中可以使用完整的邮件地址,如

admin@hunk.com;也可以只是用域名,如hunk.com;还可以只有用户名的部分,如marion@。

 七、为postfix开启基于cyrus-sasl的认证功能

1
2
3
  vim  /etc/sysconfig/saslauthd 
  #MECH=pam
MECH=shadow

使用以下命令验证postfix是否支持cyrus风格的sasl认证,如果您的输出为以下结果,则是支持的:

1
2
3
# /usr/local/postfix/sbin/postconf -a
cyrus
dovecot


1
2
3
# postconf -a
cyrus
dovecot
1
#vim /etc/postfix/main.cf


添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
####################CYRUS-SASL##############################
broken_sasl_auth_clients =  yes
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_invalid_hostname,
     reject_unknown_sender_domain,reject_non_fqdn_sender,reject_non_fqdn_recipient,
     reject_unknown_recipient_domain,reject_unauth_pipelining,reject_unauth_destination
smtpd_sasl_auth_enable =  yes 
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
#smtpd_sasl_application_name = smtpd  ##postfix2.3之后不再使用
smtpd_sasl_path = smtpd 
smtpd_banner = Welcome to my $myhostname ESMTP,Warning: Version not Available!
#smtpd_banner = Welcome to my $myhostname ESMTP $mail_name ($mail_version)!
1
# vim /usr/lib64/sasl2/smtpd.conf


#添加如下内容:

1
2
3
log_level: 3
pwcheck_method: saslauthd
mech_list: PLAIN LOGIN
1
# /etc/init.d/saslauthd restart



#让postfix重新加载配置文件

1
# /etc/init.d/postfix reload



修改selinux

1
2
# getenforce 
Enforcing

#下面临时生效

1
2
3
# setenforce 0
# getenforce 
Permissive

#下面永久生效

1
2
# vim /etc/sysconfig/selinux
SELINUX=disabled

#


 创建系统用户zyz

1
2
# useradd zyz
# passwd zyz

更改用户 zyz 的密码 。
新的 密码:
无效的密码: 过于简单化/系统化
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。

1
2
# testsaslauthd -u zyz -p passwd
0: OK  "Success."

#passwd是你为账户设置的密码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# telnet localhost 25
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is  '^]' .
220 Welcome to our mail.10fei3. top  ESMTP,Warning: Version not Available!
ehlo mail.10fei3. top
250-mail.10fei3. top
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN

八、dovecot 配置

1
2
3
4
5
6
# cd /etc/dovecot/
# vim dovecot.conf
protocols = pop3
login_greeting = Welcome to 10fe3. top  Mail Pop3 Server.
#login_trusted_networks = 192.168.1.0/24,10.230.205.130/32
login_trusted_networks = 192.168.1.0 /24

##关于ca证书的搭建,请参考链接:http://hunkz.blog.51cto.com/6157447/1793237

1
2
3
4
5
6
7
8
9
10
11
12
# mkdir ssl
# ls ssl/
dovecot.crt  dovecot.csr  dovecot.key
# vim conf.d/10-ssl.conf 
ssl =  yes
ssl_cert = < /etc/dovecot/ssl/dovecot .crt
ssl_key = < /etc/dovecot/ssl/dovecot .key
# vim conf.d/10-mail.conf
    mail_location = maildir:~ /Maildir
# /etc/init.d/dovecot restart 
home_mailbox = Maildir/
# /etc/init.d/postfix restart


我们现在用tcpdump 在postfix、dovecot服务器上进行抓包。

无验证登陆postfix:

1
2
3
4
5
6
7
8
9
10
11
12
# telnet 192.168.1.140 25
220 Welcome to my mail.10fei3. top  ESMTP,Warning: Version not Available!
mail from:zyz@10fei3. top
250 2.1.0 Ok
rcpt to:zyz@126.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
sss
hao
.
250 2.0.0 Ok: queued as 32A582601B1

无认证登陆dovecot:

1
2
3
4
5
6
telnet 192.168.1.140 110 
+OK Welcome to 10fe3. top  Mail Pop3 Server.
user zyz
+OK
pass zyz
+OK Logged  in .



认证登陆postfix(用户名和密码需要base64编码):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# telnet 101.230.205.140 25
Trying 101.230.205.140...
Connected to 101.230.205.140.
Escape character is  '^]' .
220 *******************************************************************
500 5.5.2 Error: bad syntax
ehlo mail.10fei3. top
250-mail.10fei3. top
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
auth login
334 VXNlcm5hbWU6
enl6
334 UGFzc3dvcmQ6
enl6
235 2.7.0 Authentication successful

SSl安全登陆dovecot:

1
2
# mutt -f pops://zyz@mail.10fei3.top 
# openssl s_client -connect mail.10fei3.top:995



九、小结

问题1:

1
2
3
4
5
6
7
8
[root@localhost ~] # postfix start 
postfix /postfix-script : warning: not owned by postfix:  /var/lib/postfix/ .
postfix /postfix-script : warning: not owned by postfix:  /var/lib/postfix/ . /master .lock
postfix /postfix-script : warning: not owned by postfix:  /var/spool/postfix/private
postfix /postfix-script : warning: not owned by postfix:  /var/spool/postfix/public
postfix /postfix-script : warning: not owned by group postdrop:  /var/spool/postfix/public
postfix /postfix-script : starting the Postfix mail system
postfix /postfix-script : fatal: mail system startup failed

解决:

1
2
3
4
  chown  -R postfix.root  /var/lib/postfix/
  chown  -R postfix.root  /var/spool/postfix/private
  chown  -R postfix.root  /var/spool/postfix/public
  chown  -R postfix.postdrop  /var/spool/postfix/public



问题2:

1
2
3
4
5
Jun  7 17:06:43 localhost dovecot: pop3(zyz): Error:  chown ( /home/zyz/mail/ .imap /INBOX , -1, 12(mail)) failed: Operation not 
permitted (egid=2528(zyz), group based on  /var/mail/zyz )
Jun  7 17:06:43 localhost dovecot: pop3(zyz): Error:  mkdir ( /home/zyz/mail/ .imap /INBOX ) failed: Operation not permitted
Jun  7 17:06:43 localhost dovecot: pop3(zyz): Error: Couldn't  open  INBOX: Internal error occurred. Refer to server log  for 
more  information. [2016-06-07 17:06:43]

解决

1
2
su  - zyz
mkdir  -p  /home/zyz/mail/ .imap /INBOX


问题3:

1
2
3
Jun  7 17:15:44 localhost dovecot: pop3(hunk): Error: user hunk: Initialization failed: mail_location not  set  and 
autodetection failed: Mail storage autodetection failed with home= /home/hunk
Jun  7 17:15:44 localhost dovecot: pop3(hunk): Error: Invalid user settings. Refer to server log  for  more  information.

解决:

1
su  - hunkmkdir -p  /home/hunk/mail/ .imap /INBOX

问题4:

foxmail等客户端不能收邮件,能发送邮件
解决:

1
2
3
4
5
6
# vim /etc/postfix/main.cf
home_mailbox = Maildir/
# /etc/init.d/postfix restart
# vim /etc/dovecot/conf.d/10-mail.conf 
mail_location = maildir:~ /Maildir
# /etc/init.d/dovecot restart







      本文转自独弹古调  51CTO博客,原文链接:http://blog.51cto.com/hunkz/1794302,如需转载请自行联系原作者




相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
相关文章
|
12天前
|
消息中间件 安全 Unix
SSH配置多台服务器之间的免密登陆以及登陆别名
SSH配置多台服务器之间的免密登陆以及登陆别名
23 1
|
15天前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
19天前
|
Linux
linux下搭建tftp服务器教程
在Linux中搭建TFTP服务器,需安装`tftp-server`(如`tftpd-hpa`)。步骤包括:更新软件包列表,安装`tftpd-hpa`,启动并设置开机自启,配置服务器(编辑`/etc/default/tftpd-hpa`),添加选项,然后重启服务。完成后,可用`tftp`命令进行文件传输。例如,从IP`192.168.1.100`下载`file.txt`: ``` tftp 192.168.1.100 &lt;&lt;EOF binary put file.txt quit EOF ```
28 4
|
1月前
|
Linux Shell 网络安全
【Shell 命令集合 网络通讯 】Linux 与SMB服务器进行交互 smbclient命令 使用指南
【Shell 命令集合 网络通讯 】Linux 与SMB服务器进行交互 smbclient命令 使用指南
48 1
|
1天前
|
监控 安全 Linux
Linux系统之安装ServerBee服务器监控工具
【4月更文挑战第22天】Linux系统之安装ServerBee服务器监控工具
35 2
|
1天前
|
Ubuntu Linux Shell
linux免交互登陆远程主机并执行命令(密钥对和Expect)
linux免交互登陆远程主机并执行命令(密钥对和Expect)
|
5天前
|
网络协议 安全 Linux
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
|
11天前
|
Linux 数据安全/隐私保护
Linux基础与服务器架构综合小实践
【4月更文挑战第9天】Linux基础与服务器架构综合小实践
1235 8
|
23天前
|
Ubuntu Linux 虚拟化
【Linux】ubuntu安装samba服务器
【Linux】ubuntu安装samba服务器
|
24天前
|
Linux
Linux安装bind9搭建自己的域名服务器
Linux安装bind9搭建自己的域名服务器
11 0