在CentOS系統上設置郵件服務器通常涉及安裝和配置Postfix作為郵件傳輸代理(MTA)和Dovecot作為郵件服務器軟件(MDA)以及相關的數據庫服務,如MariaDB。以下是配置CentOS郵件服務器的基本步驟:
首先,確保你的系統是最新的,并安裝Postfix和Dovecot。
sudo yum update -y
sudo yum install postfix dovecot dovecot-pgsql dovecot-sieve dovecot-managesieved
編輯Postfix的主配置文件 /etc/postfix/main.cf。
sudo vi /etc/postfix/main.cf
添加或修改以下配置:
myhostname = mail.example.com
mydomain = example.com
myorigin = mydomain
inet_interfaces = all
inet_protocols = ipv4
mydestination = myhostname, localhost.mydomain, localhost, mydomain
home_mailbox = Maildir/
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = myhostname
smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination
smtpd_tls_security_level = may
smtpd_tls_cert_file = /etc/pki/tls/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file = /etc/pki/tls/private/ssl-cert-snakeoil.key
smtpd_use_tls = yes
編輯Dovecot的主配置文件 /etc/dovecot/dovecot.conf。
sudo vi /etc/dovecot/dovecot.conf
添加或修改以下配置:
mail_location = maildir:/Maildir
protocols = imap pop3
listen = *
ssl = yes
ssl_cert = /etc/pki/tls/certs/ssl-cert-snakeoil.pem
ssl_key = /etc/pki/tls/private/ssl-cert-snakeoil.key
編輯Dovecot的SASL認證配置文件 /etc/dovecot/conf.d/10-auth.conf。
sudo vi /etc/dovecot/conf.d/10-auth.conf
確保以下配置存在:
disable_plaintext_auth = no
auth_mechanisms = plain login
編輯Dovecot的IMAP和POP3配置文件 /etc/dovecot/conf.d/10-mail.conf。
sudo vi /etc/dovecot/conf.d/10-mail.conf
確保以下配置存在:
mail_privileged_group = mail
啟動Postfix和Dovecot服務,并設置它們在系統啟動時自動啟動。
sudo systemctl start postfix
sudo systemctl enable postfix
sudo systemctl start dovecot
sudo systemctl enable dovecot
確保防火墻允許SMTP(端口25)、IMAP(端口143)和POP3(端口110)流量。
sudo firewall-cmd --permanent --add-servicesmtp
sudo firewall-cmd --permanent --add-serviceimap
sudo firewall-cmd --permanent --add-servicepop3
sudo firewall-cmd --reload
你可以使用 telnet 或 openssl 來測試郵件服務器是否正常工作。
telnet localhost 25
你應該看到類似以下的輸出:
220 mail.example.com ESMTP Postfix
然后你可以發送一封測試郵件:
HELO localhost
MAIL FROM:sender@example.com
RCPT TO:recipient@example.com
DATASubject: Test Email
This is a test email.
QUIT