本文將嘗試闡述在Rethat 系統中使用Postfix+Dovecot+MySQL搭建郵件服務器,以及解決搭建過程中遇到的問題。
? Postfix: 是一個標準的MTA「Mail Transfer Agent」服務器,它負責通過SMTP協議管理發送到本機的郵件以及由本機發向外界的郵件。在本例中,Postfix會把郵件的本地投遞工作「接受到郵 件之后將郵件存檔到本地磁盤」交給Dovecot的LMTP服務「Local Mail Transfer Protocol service」處理。當然,當大家想通過服務器向外界發送郵件時,Postfix還將負責驗證權限以確保服務器不被濫用。「很多郵件服務器根本沒有對 SMTP做用戶驗證,這將導致任何匿名用戶都可以通過服務器向外界發送郵件,從而使得服務器變成垃圾中轉站」
? Dovecot: 是一個非常優秀的IMAP/POP服務器用以接收外界發送到本機的郵件。通常,Dovecot的工作內容包括:驗證用戶身份以確保郵件不會被泄露。在本例 中,Dovecot將負責所有的「身份驗證」工作,我們會配置Dovecot查詢本地的MySQL數據庫以確認用戶身份
? MySQL: 不必多說,它將存儲所有的用戶信息,其中包括:需要監聽的域名信息、用戶郵箱地址、登錄密碼、郵箱別名「alias」等
DNS配置
首先,你需要申請一個域名。譬如我設置的域名admindomain.com
設置完以后,DNS的MX記錄及TTL時間600大致如下:
主機記錄 記錄類型 記錄值
* A xx.xx.xx.xx(我的ip)
@ MX mail.admindomain.com
imap A xx.xx.xx.xx(我的ip)
mail A xx.xx.xx.xx(我的ip)
pop A xx.xx.xx.xx(我的ip)
smtp A xx.xx.xx.xx(我的ip)
MySQL安裝及配置
本例中我們使用MySQL數據庫保存Postfix需要服務的虛擬域名、用戶帳號及密碼、郵件別名三個重要的信息。
請注意:
其實Postfix和Dovecot是完全可以不使用數據庫的,二者都可以通過各種配置文件完成「零數據庫」的郵件服務器。但是,既然可以使用數據庫這么 方便,為什么不用呢?將這些需要配置的信息存儲在數據庫中「如:用戶帳號及密碼等」,對今后的維護來說是非常方便的事情。不是么?!
MySQL安裝
yum install mysql
vi my.cnf
修改相關的mysql參數并啟動這里不詳述,
mysql_install_db --user=mysql ##初始化
/bin/sh /usr/bin/mysqld_safe --user=mysql & ###啟動
數據庫及用戶初始化
接下來,我們需要新建一個MySQL用戶及一個數據庫:
1. 使用root口令登錄MySQL
mysql -u root -p
2. 輸入root口令
3. 新建一個數據庫,名稱叫做mailserver:
create database mailserver character set utf8;
4. 輸入如下命令以新建一個用戶mailserver,并指定密碼為mailserver123:
create user mailserver@'localhost' identified by 'mailserver123';
5. 將數據庫mailserver的所有權限賦給用戶mailserver:
grant all on mailserver.* to mailserver@'localhost' identified by 'passwd';
6. 退出root用戶:
exit;
7. 使用mailserver用戶登錄:
mysql -u mailserver -p
8. 輸入mailserver帳號的口令
9. 將默認數據庫切換為mailserver數據庫:
use mailserver;
新建表
1. 建立域名記錄表:
CREATE TABLE `virtual_domains` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`))
ENGINE=InnoDB DEFAULT CHARSET=utf8;
2. 新建virtual_users表,記錄用戶的郵件地址及密碼「千萬不要保存明文密碼」:
CREATE TABLE `virtual_users` (
`id` int(11) NOT NULL auto_increment,
`domain_id` int(11) NOT NULL,
`password` varchar(106) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
3. 新建virtual_aliases表,該表是郵件服務器別名表「郵件服務器的別名alias:
CREATE TABLE `virtual_aliases` (
`id` int(11) NOT NULL auto_increment,
`domain_id` int(11) NOT NULL,
`source` varchar(100) NOT NULL,
`destination` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE)
ENGINE=InnoDB DEFAULT CHARSET=utf8
插入測試數據
為了便于查看結果,接下來給上述三張表種插入一些測試數據:
1. 給virtual_domains表插入測試數據,大致如下:
insert into virtual_domains(id,name) values(1,'mail.admindomain.com');
請注意:
上述表種id字段是自增列,可以不賦值。但無論如何,接下來的兩張表種我們將需要上述表種的逐漸列id的值。給virtual_users表添加用戶數據:
insert into virtual_users(id,domain_id,password,email)
values (1,1,ENCRYPT('test1passwd', CONCAT('$7$', SUBSTRING(SHA(RAND()), -16))),'test1@admindomain.com');
insert into virtual_users(id,domain_id,password,email)
values (2,1,ENCRYPT('test2passwd', CONCAT('$7$', SUBSTRING(SHA(RAND()), -16))),'test2@admindomain.com');
2. 給virtual_aliases表添加別名數據:
insert into virtual_aliases(id,domain_id,source,destination)
values (1,1,'all@admindomain.com','test1@mydomain.com');
insert into virtual_aliases(id,domain_id,source,destination)
values (1,1,'all@admindomain.com',’test2@mydomain.com');
請注意:
通過上述別名表的數據,當有人給all@admindomain.com發送郵件時,系統將自動將郵件轉發給test1@admindomain.com和test2@admindomain.com
這種場景,在公司內部「發送通知」等情況下適用
Postfix安裝及配置
Postfix是郵件發送的核心服務器,所有向內、向外投遞的郵件都需要經過Postfix通過SMTP協議完成。接下來的內容,大家需要修改Postfix相關的一些參數,它們是:
? 告訴Postfix如何連接MySQL數據庫,并讓Postfix通過數據庫種的表確定收發郵件的域名、用戶帳號及密碼、郵件別名等
? 告訴Postfix將收到的郵件轉發給Dovecot的LMTP服務以完成本地投遞
? 告訴Postfix所有的連接都需要STARTTLS加密,如果有必要「廢話啊,當然必須這樣」
? 開放本地端口25、465、587之一或全部
Postfix的安裝
在命令行種執行以下命令以安裝Postfix:
yum install postfix
Postfix的配置
1. 備份Postfix的配置文件/etc/postfix/main.cf,先!
cp /etc/postfix/main.cf /etc/postfix/main.cf_backup_20160314
請注意
任何時候,對任何配置進行修改之前,先做好備份總是非常必要的
同時,這也是一個非常良好的操作習慣
2. 使用vi編輯器打開/etc/postfix/main.cf文件將要把Postfix默認的用戶驗證參數屏蔽
vi /etc/postfix/main.cf
# TLS parameters
#smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
#smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key? #smtpd_use_tls=yes
#smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
#smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
3. 復制如下內容,并將其插入到上述注釋代碼之后:
smtpd_tls_cert_file=/etc/dovecot/dovecot.pem
smtpd_tls_key_file=/etc/dovecot/private/dovecot.pem
smtpd_use_tls=yes
smtpd_tls_auth_only = yes
#Enabling SMTP for authenticated users, and handing off authentication to Dovecot
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions =? permit_sasl_authenticated,permit_mynetworks, reject_unauth_destination
4. 按照如下方式修改mydestination一行的值:
mydestination = localhost
請注意:
將mydestination的值修改為localhost,以便Postfix能夠通過MySQL表中相關數據決定需要接受/發送郵件的域名,這樣更具有通用性
5. 在文檔種加入以下內容,以便告訴Postfix不要使用LDA「Local Delivery Agent」轉而使用Dovecot的LMTP完成本地郵件投遞:
#Handing off local delivery to Dovecot's LMTP, and telling it where to store mail
virtual_transport = lmtp:unix:private/dovecot-lmtp
6. 在文檔中加入以下內容,以便告訴Postfix去MySQL數據庫種尋找域名、用戶帳號密碼及郵件別名等信息:
virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias-maps.cf
7. 最終,修改完成以后的/etc/postfix/main.cf文件大致應該如下:
# See /usr/share/postfix/main.cf.dist for a commented, more complete version
# Debian specific:? Specifying a file name will cause the first
# line of that file to be used as the name.? The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
# appending .domain is the MUA's job.
append_dot_mydomain = no
# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h
readme_directory = no
# TLS parameters
#smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
#smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
#smtpd_use_tls=yes
#smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
#smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_tls_cert_file=/etc/dovecot/dovecot.pem
smtpd_tls_key_file=/etc/dovecot/private/dovecot.pem
smtpd_use_tls=yes
smtpd_tls_auth_only = yes
#Enabling SMTP for authenticated users, and handing off authentication to Dovecot
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions =
permit_sasl_authenticated,
permit_mynetworks,
reject_unauth_destination
# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.
myhostname = host.mydomain.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
#mydestination = example.com, hostname.mydomain.com,localhost.mydomain.com, localhost
mydestination = localhost
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
#Handing off local delivery to Dovecot's LMTP, and telling it where to store mail
virtual_transport = lmtp:unix:private/dovecot-lmtp
#Virtual domains, users, and aliases
virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias-maps.cf
8. 按下ESC鍵并輸入如下內容以保存并退出
wq!
9. 新建/etc/postfix/mysql-virtual-mailbox-domains.cf文件并輸入如下內容:
user = mailserver
password = passwd
hosts = 127.0.0.1
dbname = mailserver
query = SELECT 1 FROM virtual_domains WHERE name='%s'
10. 重啟Postfix服務
service postfix restart
11. 測試上述內容是否正確,如果上述內容配置正確,則如下命令執行后返回結果應該為1:
postmap -q admindomain.com mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
12. 新建/etc/postfix/mysql-virtual-mailbox-maps.cf文件并輸入如下內容:
user = mailserver
password = passwd
hosts = 127.0.0.1
dbname = mailserver
query = SELECT 1 FROM virtual_users WHERE email='%s'
13. 重啟Postfix服務
service postfix restart
14. 測試上述配置是否正確,如果上述內容配置正確,則如下命令執行后返回結果應該為1:
postmap -q test1@admindomain.com mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
15. 新建/etc/postfix/mysql-virtual-alias-maps.cf文件并輸入如下內容:
user = mailserver
password = passwd
hosts = 127.0.0.1
dbname = mailserver
query = SELECT destination FROM virtual_aliases WHERE source='%s'
16. 重啟Postfix服務
service postfix restart
17. 測試上述配置是否正確,如果上述配置正確,則如下命令執行后返回結果應該是之前添加的別名帳號:
postmap -q all@admindomain.com mysql:/etc/postfix/mysql-virtual-alias-maps.cf
18. 使用vi編輯器打開/etc/postfix/master.cf文件「請注意修改之前先備份」,找到submission和smtps所在的兩行,并將其注釋去掉。這樣做的目的是允許Postfix通過587和465端口發送郵件
19. 重啟Postfix服務
service postfix restart
Dovecot安裝及配置
Dovecot在本例中充當IMAP、POP服務器的角色,同時它也將負責用戶登錄時用戶身份的驗證「Dovecot會將真正的驗證工作交給 MySQL處理」。因為使用SSL,Dovecot將會使用993「IMAP協議」及995「POP協議」與外界交流,若服務器有iptable之類的玩 意兒,請開放相關端口。
這部分的內容配置起來相對簡單,但是需要配置的文件繁多。大體上,我們需要配置如下的信息:
1. 開啟Dovecot的IMAP、POP3、LMTP協議
2. 告知Dovecot本地郵件的投檔路徑
3. 連接Dovecot和MySQL數據庫以驗證用戶身份
4. 配置SSL加密相關信息
Dovecot的安裝
通過如下命令安裝Dovecot最新版:
yum install dovecot dovecot-mysql
Dovecot的配置
需要修改的配置文件有:
? /etc/dovecot/dovecot.confDovecot的主配置文件
? /etc/dovecot/conf.d/10-mail.confDovecot將要操作的磁盤路徑相關配置信息
? /etc/dovecot/conf.d/10-auth.conf用戶驗證相關配置信息
? /etc/dovecot/conf.d/auth-sql.conf.extSQL-Type驗證相關配置信息
? /etc/dovecot/dovecot-sql.conf.extDovecot與數據庫連接相關配置信息
? /etc/dovecot/conf.d/10-master.confDovecot本地socket相關配置信息
? /etc/dovecot/conf.d/10-ssl.conf關于SSL的相關配置信息
請注意:
在修改上述文件之前,請一定先做好備份以方便恢復
修改/etc/dovecot/dovecot.conf文件
使用vi編輯器打開/etc/dovecot/dovecot.conf文件并在文件種加入如下內容:
!include conf.d/*.conf
# Enable installed
protocols!include_try /usr/share/dovecot/protocols.d/*.protocol
protocols = imap pop3 lmtp
如果以上內容已經存在,只需要把該行的#號去掉即可
上述內容大致的意思是:告訴Dovecot啟用所有.conf文件;并開啟Dovecot的imap、pop3、lmtp等相關協議使之正常工作
修改/etc/dovecot/conf.d/10-mail.conf文件
打開文件并找到mail_location相關信息,將其指定到本地磁盤的某個路徑,這個路徑將來會存放收到的郵件,如下所示:
mail_location = maildir:/var/mail/vhosts/%d/%n
同時,找到文件中mail_privileged_group相關信息并將起修改為:
mail_privileged_group = mail
保存文件并退出
在命令行種輸入如下內容以查看/var/mail路徑的權限:
ls -ld /var/mail
顯示的內容大致應該是:
drwxrwsr-x 2 root mail 4096 May? 11 15:08 /var/mail
創建/var/mail/vhosts/文件夾給每個需要啟用的域名:
mkdir -p /var/mail/vhosts/mydomain.com
輸入如下命令以新建vmail用戶組及用戶并賦權限
groupadd -g 5000 vmail
useradd -g vmail -u 5000 vmail -d /var/mail
接下來修改一下/var/mail/目錄的權限,使vmail能夠訪問:
chown -R vmail:vmail /var/mail
修改/etc/dovecot/conf.d/10-auth.conf文件
找到文件中disable_plaintext_auth并取消注釋
disable_plaintext_auth = yes
找到文件中auth_mechanisms并將其修改為如下值:
auth_mechanisms = plain login
默認情況下,Dovecot是允許Ubuntu系統用戶登錄使用的,我們需要將其禁用。找到文件種如下內容并將其注釋:
#!include auth-system.conf.ext
開啟Dovecot的MySQL支持,取消!include auth-sql.conf.ext的注釋符號:
#!include auth-system.conf.ext
!include auth-sql.conf.ext
#!include auth-ldap.conf.ext
#!include auth-passwdfile.conf.ext
#!include auth-checkpassword.conf.ext
#!include auth-vpopmail.conf.ext
#!include auth-static.conf.ext
修改/etc/dovecot/conf.d/auth-sql.conf.ext文件
在文件中加入如下內容:
passdb {
driver = sql
args = /etc/dovecot/dovecot-sql.conf.ext
}
userdb {
driver = static
args = uid=vmail gid=vmail home=/var/mail/vhosts/%d/%n
}
修改/etc/dovecot/dovecot-sql.conf.ext文件
取消文件中driver行的注釋,并將其修改為如下:
driver = mysql
取消文件中connect行的注釋,并將其修改為如下:
connect = host=127.0.0.1 dbname=mailserver user=mailserver password=passwd
取消文件中default_pass_scheme行的注釋,并將其修改為如下:
default_pass_scheme = SHA512-CRYPT
取消文件中password_query行的注釋,并將起修改為如下:
password_query = SELECT email as user, password FROM virtual_users WHERE email='%u';
保存退出
在命令行種輸入如下內容以修改目錄權限:
chown -R vmail:dovecot /etc/dovecot
chmod -R o-rwx /etc/dovecot
修改/etc/dovecot/conf.d/10-master.conf文件
打開文件做如下修改「通過將端口設置為0,以禁用非SSL加密的IMAP和POP3協議」:
service imap-login {
inet_listener imap {
port = 0
}
...
}
service pop3-login {
inet_listener pop3 {
port = 0
}
...
}
找到文件中的service lmtp并將其修改如下:
service lmtp {
unix_listener /var/spool/postfix/private/dovecot-lmtp {
mode = 0600
user = postfix
group = postfix
}
# Create inet listener only if you can't use the above UNIX socket
#inet_listener lmtp {
#Avoid making LMTP visible for the entire internet
#address =
#port =
#}
}
找到文件中service auth并將其內容修改如下:
service auth {
# auth_socket_path points to this userdb socket by default. It's typically
# used by dovecot-lda, doveadm, possibly imap process, etc. Its default
# permissions make it readable only by root, but you may need to relax these
# permissions. Users that have access to this socket are able to get a list
# of all usernames and get results of everyone's userdb lookups.
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}
unix_listener auth-userdb {
mode = 0600
user = vmail
#group =
}
# Postfix smtp-auth
#unix_listener /var/spool/postfix/private/auth {
#? ? ? mode = 0666
#}
# Auth process is run as this user.
user = dovecot
}
找到文件中service auth-worker內容并修改如下:
service auth-worker {
# Auth worker process is run as root by default, so that it can access
# /etc/shadow. If this isn't necessary, the user should be changed to
# $default_internal_user.
user = vmail
}
修改/etc/dovecot/conf.d/10-ssl.conf文件
找到文件中ssl_cert并修改內容如下「請確保dovecot的pem文件已經存在,如果大家使用了自己的SSL文件,請將如下內容修改為相應的路徑」:
ssl_cert =