在CentOS上设置邮件服务器并配置域名的步骤如下:
首先,确保你的CentOS系统已经安装了Postfix和Dovecot,这两个软件包分别用于邮件传输代理(MTA)和邮件投递代理(MDA)。
sudo yum install postfix dovecot
Postfix是邮件传输代理,负责发送和接收邮件。
sudo vi /etc/postfix/main.cf
在main.cf
文件中添加或修改以下内容:
myhostname = mail.yourdomain.com # 替换为你的域名
mydomain = yourdomain.com # 替换为你的域名
myorigin = $mydomain
inet_interfaces = all
inet_protocols = ipv4
mydestination = $myhostname, localhost.$mydomain, $mydomain
mynetworks = 127.0.0.0/8 [::1]/128
home_mailbox = Maildir/
确保你的域名有正确的MX记录和A记录(或AAAA记录)。例如:
Dovecot是邮件投递代理,负责接收和存储邮件。
sudo vi /etc/dovecot/dovecot.conf
在dovecot.conf
文件中启用以下插件:
plugins = $plugins sieve
编辑/etc/dovecot/conf.d/10-mail.conf
文件,设置邮件存储路径:
mail_location = maildir:~/Maildir
确保防火墙允许SMTP(端口25)、IMAP(端口143)和POP3(端口110)流量。
sudo firewall-cmd --permanent --add-service=smtp
sudo firewall-cmd --permanent --add-service=imap
sudo firewall-cmd --permanent --add-service=pop3
sudo firewall-cmd --reload
启动Postfix和Dovecot服务,并设置为开机自启:
sudo systemctl start postfix
sudo systemctl enable postfix
sudo systemctl start dovecot
sudo systemctl enable dovecot
使用telnet
或openssl
命令测试邮件服务器是否正常工作。
telnet mail.yourdomain.com 25
你应该会看到类似以下的输出:
220 mail.yourdomain.com ESMTP Postfix
openssl s_client -connect mail.yourdomain.com:143
你应该会看到类似以下的输出:
...
* OK Dovecot ready.
...
为了提高安全性,建议配置SSL/TLS。
sudo yum install certbot
sudo certbot certonly --standalone -d mail.yourdomain.com
编辑/etc/postfix/main.cf
文件,添加以下内容:
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.yourdomain.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.yourdomain.com/privkey.pem
smtpd_use_tls = yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
编辑/etc/dovecot/conf.d/10-ssl.conf
文件,添加以下内容:
ssl = yes
ssl_cert = </etc/letsencrypt/live/mail.yourdomain.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/mail.yourdomain.com/privkey.pem
重启Postfix和Dovecot服务:
sudo systemctl restart postfix
sudo systemctl restart dovecot
完成以上步骤后,你的CentOS邮件服务器应该已经成功设置并配置了域名。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: 如何设置CentOS系统以定期更新安全补丁