邮件服务器搭建
一、mail服务器初始化配置
1.修改主机名
[root@localhost /]# hostnamectl --static set-hostname mail.drock.top|bash
2.关闭防火墙和selinux
[root@localhost /]# systemctl stop firewalld && systemctl disable firewalld
[root@localhost /]# setenforce 0
3.同步时间
[root@localhost /]# yum -y install ntpdate && ntpdate ntp.aliyun.com
4.安装软件
[root@localhost /]# yum -y install postfix dovecot cyrus-sasl-* mailx
二、修改配置文件
1.配置postfix,备份cp /etc/postfix/main.cf /etc/postfix/main-old.cf
[root@localhost /]# cat /etc/postfix/main.cfmail_owner = postfixmyhostname = mail.liqing-test.topmydomain = liqing-test.topmyorigin = $mydomaininet_interfaces = allinet_protocols = ipv4mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain, mail.$mydomain, www.$mydomain, ftp.$mydomainlocal_recipient_maps mynetworks = 0.0.0.0/0relay_domains = $mydestinationalias_maps = hash:/etc/aliasesalias_database = hash:/etc/aliasessmtpd_banner = $myhostname ESMTP# 在最下面新增smtpd_sasl_type = dovecotsmtpd_sasl_path = private/authsmtpd_sasl_local_domain =smtpd_sasl_auth_enable = yessmtpd_sasl_security_options = noanonymousbroken_sasl_auth_clients = yessmtpd_recipient_restrictions = permit_mynetworks,permit_auth_destination,permit_sasl_authenticatedsmtpd_client_restrictions = permit_sasl_authenticated
2.配置dovectot
2.1配置监听协议:
[root@localhost /]# cat /etc/dovecot/dovecot.confprotocols = imap pop3 lmtplisten = *login_trusted_networks = 0.0.0.0/0dict {}!include conf.d/*.conf!include_try local.conf
2.2配置登录方式
[root@localhost /]# cat /etc/dovecot/conf.d/10-auth.confdisable_plaintext_auth = noauth_mechanisms = plain login!include auth-system.conf.ext
2.3配置邮件存储位置
[root@localhost /]# cat /etc/dovecot/conf.d/10-mail.confmail_location = mbox:~/mail:INBOX=/var/mail/%unamespace inbox {inbox = yes}first_valid_uid = 1000mbox_write_locks = fcntl
[root@localhost /]# cat /etc/dovecot/conf.d/10-master.confservice auth {unix_listener /var/spool/postfix/private/auth {mode = 0666user = postfixgroup = postfix}}
2.4配置ssl(关闭)
[root@localhost /]# cat /etc/dovecot/conf.d/10-ssl.confssl = no
3.配置sasl2
3.1配置系统认证
[root@localhost /]# cat /etc/sysconfig/saslauthdSOCKETDIR=/run/saslauthdMECH=shadowFLAGS=
3.2配置登录方式
[root@localhost /]# cat /etc/sasl2/smtpd.confpwcheck_method: saslauthdmech_list: PLAIN LOGINlog_level:3
4.启动服务
[root@localhost /]# systemctl restart dovecot[root@localhost /]# systemctl restart postfix[root@localhost /]# systemctl restart saslauthd[root@localhost /]# systemctl enable dovecot[root@localhost /]# systemctl enable postfix[root@localhost /]# systemctl enable saslauthd
5.创建用户并设置pass
[root@localhost /]# useradd -m drock && echo 123456 | passwd --stdin drock[root@localhost /]# su - drock[drock@mail ~]$ mkdir -p ~/mail/.imap/INBOX[drock@mail ~]$ chmod -R 750 ~/mail #(这不操作不做会在使用foxmail登录时报错{Error: Couldn't open INBOX: Permission denied})[drock@mail ~]$ exit
6.配置mailx
[root@localhost /]# vi /etc/mail.rcset from=drock@drock.topset smtp=mail.drock.topset smtp-auth-user=drockset smtp-auth-password=123456set smtp-auth=login
四、发送邮件测试
1.命令行发送邮件测试
[root@localhost /]# echo "邮件服务器测试" | mail -s "邮件服务器测试" other-email@163.com
2.使用foxmail登录并发送邮件测试
五、报错
1.权限被拒绝
在使用foxmail登录邮箱时提示权限被拒绝,这是因为在邮箱用户的家目录下mail文件权限不是750,设置为750后解决。
[drock@mail ~]$ chmod -R 750 ~/mail
2.无法找到主机
邮件在发送时会根据邮件地址的解析记录去查找mx记录,这里我在向腾讯的企业邮箱发送测试邮件时找不到腾讯的邮箱地址。我在内网的dns代理中加入了腾讯的mx记录后解决
[root@dns- ~]# grep qq /etc/dnsmasq.confmx-host=***.com,mxbiz2.qq.com,10mx-host=***.com,mxbiz1.qq.com,5
3.邮件被拒绝
在我向腾讯企业邮箱发送测试邮件时日志里出现了550邮件连接被拒绝的情况,此情况在邮件中设置白名单后解决。