设为首页 收藏本站
查看: 3578|回复: 1

[经验分享] linux 下 搭建邮件邮件服务器(一)-系统账户登陆收发邮件

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-7-7 09:21:46 | 显示全部楼层 |阅读模式
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




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
[iyunv@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



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-240562-1-1.html 上篇帖子: 扫描仪通过SMTP中继服务器发送通知邮件失败 下篇帖子: linux 下 搭建邮件邮件服务器(Postfix+Dovecot)(二) 邮件服务器 收发邮件 linux
累计签到:16 天
连续签到:4 天
发表于 2017-12-14 09:41:51 | 显示全部楼层
感谢楼主分享···

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表