Centos7.9系统邮件服务器搭建

步骤 1: 安装Postfix(SMTP服务)
安装Postfix作为SMTP服务:
sudo yum install postfix -y
步骤 2: 配置Postfix
安装完Postfix后,修改配置文件以使其支持密码的SMTP服务。编辑配置文件:
sudo vi /etc/postfix/main.cf
在配置文件中,设置以下关键项:

queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
mail_owner = postfix
inet_interfaces = all
inet_protocols = all
mydestination = $myhostname, localhost.$mydomain, localhost
unknown_local_recipient_reject_code = 550
debug_peer_level = 2
debugger_command =
         PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
         ddd $daemon_directory/$process_name $process_id & sleep 5
sendmail_path = /usr/sbin/sendmail.postfix newaliases_path = /usr/bin/newaliases.postfix mailq_path = /usr/bin/mailq.postfix setgid_group = postdrop html_directory = no manpage_directory = /usr/share/man sample_directory = /usr/share/doc/postfix-2.10.1/samples
myhostname = smtp.example.com mydomain = example.com myorigin = /etc/mailname inet_interfaces = all inet_protocols = ipv4 mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain relayhost = mynetworks = 10.20.192.0/24, 10.50.36.0/24 home_mailbox = Maildir/ # 启用SASL认证 smtpd_sasl_auth_enable = yes smtpd_sasl_security_options = noanonymous smtpd_sasl_local_domain = $mydomain # 启用SMTP认证机制 smtpd_sasl_type = dovecot smtpd_sasl_path = private/auth # 启用TLS支持 smtpd_use_tls = yes # 启用TLS证书配置 smtpd_tls_cert_file = /etc/ssl/certs/postfix.crt smtpd_tls_key_file = /etc/ssl/private/postfix.key smtpd_client_restrictions = permit_mynetworks, permit readme_directory = /usr/share/doc/postfix-2.10.1/README_FILES

 说明:
mynetworks = 10.20.192.0/24, 10.50.36.0/24:这里配置的是允许的网络段。如果您的网络要求不同,可以根据实际情况修改此项。


步骤 3: 配置密码的SMTP账户
为邮箱账户 test@test.com 创建虚拟用户,并配置为waftest密码。编辑 /etc/postfix/virtual 文件,添加如下内容:
test@test.com test waftest

图片

确保配置正确,并使其与Postfix集成。

步骤 4: 重启Postfix服务
配置完成后,重启Postfix服务以使新配置生效:
sudo systemctl restart postfix
sudo systemctl enable postfix

步骤 5: 配置防火墙
确保防火墙允许外部访问SMTP服务(端口25)。运行以下命令:
sudo firewall-cmd --zone=public --add-port=25/tcp --permanent

yum install dovecot -y
firewall-cmd --zone=public --add-port=465/tcp --permanent
firewall-cmd --reload

图片

 
检查各个配置
检查端口
sudo yum install net-tools
sudo netstat -tuln | grep :25
sudo yum install lsof -y
如果显示的是
说明Postfix正在监听 127.0.0.1:25,这意味着它仅在本地回环接口(localhost)上监听,而没有监听在外部接口(如 10.20.192.173)上,这会导致SMTP服务器不可达

检查postfix配置
如果遇到了上面的问题需要检查一下配置:
sudo postconf -n
如果是错误配置,可能是这个导致的:
需要修改配置
sudo vi /etc/postfix/main.cf
把inet_interfaces = localhost注释掉
把inet_interfaces = all加上

安装AUTH
步骤 1: 安装并启用SMTP认证(SASL)
安装SASL认证库 首先,安装SASL库:
sudo yum install cyrus-sasl cyrus-sasl-plain cyrus-sasl-devel -y
编辑Postfix配置文件
编辑Postfix的主配置文件 /etc/postfix/main.cf,启用SASL身份验证支持:
sudo vi /etc/postfix/main.cf

启用SMTP认证服务
接下来,编辑 /etc/postfix/master.cf,确保在邮件传输通道中启用SMTP身份验证:
sudo vi /etc/postfix/master.cf
找到以下行(如果有)并确保取消注释:
submission inet n - n - - smtpd

重启Postfix服务
配置完成后,重新启动Postfix服务以使配置生效:
sudo systemctl restart postfix

sudo useradd test
sudo passwd test

图片
修改:/etc/postfix/master.cf 配置内容如下:

图片

smtp      inet  n       -       n       -       -       smtpd
submission inet n       -       n       -       -       smtpd
smtps     inet  n       -       n       -       -       smtpd
smtps     inet  n       -       n       -       -       smtpd
  -o syslog_name=postfix/smtps
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_mynetworks,permit_sasl_authenticated,reject_unknown_client
  -o smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination


pickup    unix  n       -       n       60      1       pickup
cleanup   unix  n       -       n       -       0       cleanup
qmgr      unix  n       -       n       300     1       qmgr

tlsmgr    unix  -       -       n       1000?   1       tlsmgr
rewrite   unix  -       -       n       -       -       trivial-rewrite
bounce    unix  -       -       n       -       0       bounce
defer     unix  -       -       n       -       0       bounce
trace     unix  -       -       n       -       0       bounce
verify    unix  -       -       n       -       1       verify
flush     unix  n       -       n       1000?   0       flush
proxymap  unix  -       -       n       -       -       proxymap
proxywrite unix -       -       n       -       1       proxymap
smtp      unix  -       -       n       -       -       smtp
relay     unix  -       -       n       -       -       smtp

showq     unix  n       -       n       -       -       showq
error     unix  -       -       n       -       -       error
retry     unix  -       -       n       -       -       error
discard   unix  -       -       n       -       -       discard
local     unix  -       n       n       -       -       local
virtual   unix  -       n       n       -       -       virtual
lmtp      unix  -       -       n       -       -       lmtp
anvil     unix  -       -       n       -       1       anvil
scache    unix  -       -       n       -       1       scache

修改:/etc/dovecot/dovecot.conf配置文件内容如下:

图片

dict {
  #quota = mysql:/etc/dovecot/dovecot-dict-sql.conf.ext
  #expire = sqlite:/etc/dovecot/dovecot-dict-sql.conf.ext
}

!include conf.d/*.conf

service auth {
  unix_listener /var/spool/postfix/private/auth {
    mode = 0660
    user = postfix
    group = postfix
  }
}
auth_mechanisms = plain login


!include_try local.conf

修改:cat /etc/pam.d/dovecot 配置文件内容如下:

图片

#%PAM-1.0
auth       required     pam_nologin.so
auth       include      password-auth
account    include      password-auth
session    include      password-auth
auth       required     pam_unix.so
account    required     pam_unix.so

 sudo systemctl restart dovecot
 doveadm auth test test waftest

图片

开启debug日志监控:/etc/dovecot/conf.d/10-logging.conf

图片

 /etc/dovecot/conf.d/10-auth.conf

图片

  • %Ln 表示取 @ 之前的本地部分(local-part)。

  • 这样客户端无论输入 test@test.com 还是 test,Dovecot 都会用 test 去查询 PAM。

systemctl restart dovecot

 doveadm auth test test@test.com waftest

图片
sudo journalctl -u postfix -u dovecot -f
mkdir -p /etc/ssl/private
ls /etc/ssl/private/
sudo openssl req -new -x509 -nodes -out /etc/ssl/certs/postfix.crt -keyout /etc/ssl/private/postfix.key -days 365
sudo chmod 600 /etc/ssl/private/postfix.key
sudo chmod 644 /etc/ssl/certs/postfix.crt
sudo chown root:root /etc/ssl/private/postfix.key /etc/ssl/certs/postfix.crt

图片

lsof -i:25

lsof -i:465

systemctl restart dovecot

sudo systemctl restart postfix
tailf /var/log/maillog 

图片

 验证邮件服务器的功能:

匿名发送

图片

图片

图片

图片

 非匿名发送:

图片

图片

图片

图片

 # 通过命令执行验证:

安装sweak

yum install epel-release && yum install swaks

1. 测试端口25的匿名发送
swaks --to recipient@example.com --from sender@test.com --server your_server_ip --port 25

图片

 2.  测试端口465的认证发送

swaks --to isaac.zhub@dbappsecurity.com.cn \
      --from test@test.com \
      --server 10.20.192.33 \
      --port 465 \
      --tls-on-connect \
      --auth LOGIN \
      --auth-user test \
      --auth-password waftest

图片

使用端口587:

swaks --to isaac.zhub@dbappsecurity.com.cn \
      --from test@test.com \
      --server 10.20.192.33 \
      --port 587 \
      --tls \
      --auth LOGIN \
      --auth-user test \
      --auth-password waftest

 

posted on 2026-08-12 22:01  古风尘  阅读(12)  评论(0)    收藏  举报

导航