在CentOS虚拟机上搭建邮件服务器,通常需要以下几个步骤:
首先,确保你的CentOS系统是最新的,并安装必要的软件包。
sudo yum update -y
sudo yum install -y postfix dovecot
Postfix是一个常用的MTA(邮件传输代理)。
sudo vi /etc/postfix/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/
sudo vi /etc/postfix/master.cf
确保有以下行:
submission inet n - y - - smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
smtps inet n - y - - smtpd
-o syslog_name=postfix/smtps
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
sudo systemctl restart postfix
Dovecot是一个流行的IMAP/POP3服务器。
sudo vi /etc/dovecot/dovecot.conf
确保有以下行:
protocols = imap pop3
listen = *
mail_location = maildir:~/Maildir
sudo vi /etc/dovecot/conf.d/10-auth.conf
确保有以下行:
disable_plaintext_auth = no
auth_mechanisms = plain login
sudo vi /etc/dovecot/conf.d/10-ssl.conf
确保有以下行:
ssl = yes
ssl_cert = </etc/pki/tls/certs/yourdomain.crt
ssl_key = </etc/pki/tls/private/yourdomain.key
sudo doveadm passwd -u yourusername
按照提示输入密码。
sudo systemctl restart dovecot
确保防火墙允许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
你可以使用telnet
或openssl
来测试邮件服务器是否正常工作。
telnet localhost 25
你应该能看到类似以下的输出:
220 mail.yourdomain.com ESMTP Postfix
openssl s_client -connect localhost:465
你应该能看到类似以下的输出:
CONNECTED(00000003)
depth=2 C = US, O = Let's Encrypt, CN = R3
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=2 C = US, O = Let's Encrypt, CN = R3
verify return:1
depth=0 CN = mail.yourdomain.com
verify return:1
---
Certificate chain
0 s:CN = mail.yourdomain.com
i:C = US, O = Let's Encrypt, CN = R3
1 s:C = US, O = Let's Encrypt, CN = R3
i:C = ISRG, O = Let's Encrypt, CN = ISRG Root X1
---
Server certificate
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
subject=CN = mail.yourdomain.com
issuer=C = US, O = Let's Encrypt, CN = R3
---
No client certificate CA names sent
Peer signing digest: SHA256
Peer signature type: RSA-PSS
Server Temp Key: X25519, 253 bits
---
SSL handshake has read 4096 bytes and written 434 bytes
Verification error: self signed certificate
---
New, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384
Server public key is 2048 bit
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
Early data was not sent
Verify return code: 19 (self signed certificate)
---
确保你的域名DNS记录正确配置了MX记录,指向你的邮件服务器IP地址。
MX IN A yourmailserverip
你可以使用mail
命令或任何邮件客户端来测试邮件的发送和接收。
echo "Test email body" | mail -s "Test Email Subject" recipient@example.com
按照提示输入发件人邮箱地址和密码。
通过以上步骤,你应该能够在CentOS虚拟机上成功搭建一个基本的邮件服务器。