1. 程式人生 > >使用postfix+dovecot搭建一臺郵件伺服器

使用postfix+dovecot搭建一臺郵件伺服器

一,前期準備

1.各種賬號

*注意:如果是真實環境,使用者名稱密碼等會有複雜度要求,請酌情建立。
不用統一建立這些賬號,後面教程會一一提到

賬號 使用者名稱 密碼 說明
centos root *****隱去 安裝OS時自帶的使用者
centos jw *****隱去 安裝OS時自帶的使用者
centos vmail vmail 專門用於虛擬使用者郵件收發的使用者
centos jw1 jwmail cyrus-sasl的smtp使用者,可以收發郵件,虛擬使用者建立後,失效
mysql root 123456 mysql根使用者
mysql postfix postfix 專門為 postfix準備的使用者,資料庫也叫 postfix
mysql webmail webmail 專門為 webmail 準備的使用者,資料庫也叫 webmail
postfixadmin setup使用者 postfix2018 ---
postfixadmin [email protected] postfix2018 不一定非得是[email protected], 只要是一個郵箱就行
虛擬使用者 [email protected] user2018 postfixadmin建立的
虛擬使用者 [email protected]
user2018 postfixadmin建立的

HOSTNAME:
mail.test.net
注意:雖然dns對字尾沒要求,但是postfix會自動校驗字尾合法性。請使用主流字尾.net .com 等等

DNS:
在本機上構建了一套DNS解析的系統,否則會在投遞 [email protected] 的過程中出現無法解析domain的情況
當然,如果自己已經購買了DNS伺服器,直接在把域名掛載DNS上即可,注意要一條A記錄,一條MX記錄。

2.系統安裝

CentOS 6.5 安裝過程略。

網路

實驗環境-192.168.2.234

防火牆

# 永久關閉 
chkconfig iptables off

在生產環境中,基於安全策略可能並不允許關閉防火牆。但是要保證下列埠暢通。

25, 465, 587, 110, 995, 143, 993

SELINUX

# 永久關閉selinux 
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

3.JDK

JDK環境用於後面測試 傳送郵件的jar是否可用。不安裝也OK。

yum search java|grep jdk
yum install java-1.7.0-openjdk.x86_64
java -v

image.png

4.LAMP環境

用於postfixadmin 和 web端的郵件收發。
PHP 和 APACHE的安全類設定本文件未提及,但是在真實環境裡,需要考慮安全策略。

LAMP的準備工作

CentOS 6.5 有時候會找不到一些源,造成安裝失敗。

# 安裝下載工具
yum install wget
# 下載
wget http://www.atomicorp.com/installers/atomic
# 安裝
sh ./atomic
# 更新yum源
yum check-update

安裝apache

yum install httpd
# 安裝完成後,啟動apache
/etc/init.d/httpd start
# 設定為開機啟動
chkconfig httpd on

安裝mysql

yum install mysql mysql-server
#安裝完成後,啟動mysql
/etc/init.d/mysqld start
#設定為開機啟動
chkconfig mysqld on

安裝php

#檢查當前安裝的PHP包
yum list installed | grep php
#如果有安裝的PHP包,先刪除他們, 如:
yum remove php.x86_64 php-cli.x86_64 php-common.x86_64
#配置安裝包源:
rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
#執行安裝,分下面幾部
yum -y install php56w.x86_64
yum -y --enablerepo=webtatic install php56w-devel
yum -y install php56w-gd.x86_64 php56w-ldap.x86_64 php56w-mbstring.x86_64 php56w-mcrypt.x86_64 php56w-mysql.x86_64 php56w-pdo.x86_64 php56w-opcache.x86_64
yum -y install php56w-imap
yum install php-dom

製作一個首頁

cd /var/www/html    
vi index.php    
<?php   
phpinfo();  
?>  

安裝telnet 並激活服務

yum install xinetd
yum install telnet
yum install telnet -server

cd /etc/xinetd.d  
cp telnet telnet.20180327
vi telnet
#修改 disable = yes 為 disable = no
#需要啟用xinetd服務
service xinetd restart

重啟mysql和apache

#重啟MySql
/etc/init.d/mysqld restart
#重啟Apache 
/etc/init.d/httpd restart

測試PHP首頁

telnet 192.168.2.234 80
GET /index. php HTTP/1.1
HOST: 127.0.0.1 (連續兩個回車)

或者
瀏覽器內位址列輸入 :http://192.168.2.234

 

image.png

mysql建表建使用者

強制指定mysql的ROOT使用者的密碼為123456

/etc/init.d/mysqld stop 
# 執行如下命令
mysqld_safe --user=mysql --skip-grant-tables --skip-networking
mysql -u root mysql
mysql>UPDATE user SET Password=PASSWORD('123456') where USER='root';
mysql>FLUSH PRIVILEGES;
mysql>quit
/etc/init.d/mysqld restart 
#如果失敗,可以整機重啟

建庫,建使用者

 mysql -uroot -p
123456  
mysql>create database postfix default character set utf8 collate utf8_bin;
mysql>grant all on postfix.* to 'postfix'@'%' identified by 'postfix';
mysql>flush privileges;
mysql>quit

測試新使用者

mysql -upostfix -ppostfix

5.hostname

vim /etc/sysconfig/network

image.png

 

根據實際情況 酌情填寫。
重啟完成之後,

hostname

image.png

6.建立郵件專用使用者

為了後續的管理方便,我們使用系統的一個使用者對映為對郵件伺服器的使用者,該使用者對於postfix來說是一個虛擬使用者。
所在在此之前,我們需要新增一個不能登入到系統的,並且指定使用者組和使用者ID的特殊使用者vmail,該使用者也可以自行定義。

groupadd -g 5000 vmail
useradd -g vmail -u 5000 -s /sbin/nologin vmail
cat /etc/passwd |grep vmail

image.png

7.構建本機DNS

注意:如果已經在第三方上購買了域名,就不需要再在本機搭建DNS伺服器了。
注意:如果是區域網內已經有DNS伺服器了,把mail.test.net繫結即可,要綁一條A記錄,一條MX記錄

yum install -y bind bind-chroot
先備份
cp /etc/named.conf /etc/named.conf.20180326
vim /etc/named.conf 

image.png

cd /var/named
cp named.localhost test.net.zone
cp named.localhost 2.168.192.in-addr.arpa.zone

接下來修改配置檔案。

vim test.net.zone

vim 2.168.192.in-addr.arpa.zone

image.png

# 賦許可權
chown :named test.net.zone
chown :named 2.168.192.in-addr.arpa.zone

#驗證配置是否正確
named-checkconf /etc/named.conf
named-checkzone test.net test.net.zone
named-checkzone 2.168.192.in-addr.arpa 2.168.192.in-addr.arpa.zone

#重啟服務
service named restart

#把本機IP的DNS指向DNS伺服器(本例中就是自己)
vim /etc/sysconfig/network-scripts/ifcfg-eth0

image.png

整機重啟。

位址列中使用 mail.test.net 也是可行的。

此處要做還原點

二,郵件伺服器構建

1.cyrus-sasl

cyrus-sasl(Simple Authentication Security Layer)簡單認證安全層, SASL主要是用於SMTP認證。而cyrus-sasl在OS裡面,saslauthd是其守護程序。

建立

yum -y install cyrus-sasl
檢視版本
/usr/sbin/saslauthd -v

image.png

# 配置
vim /etc/sysconfig/saslauthd
# 修改mech

image.png

# 如果沒有該檔案,就建立。
vim /etc/sasl2/smtpd.conf

image.png

測試

useradd jw1
echo 'jwmail' | passwd --stdin jw1
# 切換到新使用者
su - jw1
mkdir -p ~/mail/.imap/INBOX

#切換回ROOT
/etc/init.d/saslauthd start
chkconfig saslauthd on

#進行認證測試
testsaslauthd -u jw1 -p 'jwmail'

image.png

 

意味著建立成功。

2.postfix

postfix作為傳送郵件伺服器。

yum -y install postfix

#先備份
cp /etc/postfix/main.cf /etc/postfix/main.cf.20180326

#修改配置
vim /etc/postfix/main.cf
#參見 《附件1main.cf》

#測試postfix配置
/etc/init.d/postfix start
chkconfig postfix on
netstat -tunlp
ps -ef |grep postfix

檢視mail日誌,這個功能會經常使用。

tail -f /var/log/maillog

檢視postfix現在生效的配置(挑錯時候用)

postconf -n

3.dovecot

#安裝
yum -y install dovecot dovecot-devel dovecot-mysql pam-devel

#修改配置之前先備份
cp /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.20180326
cp /etc/dovecot/conf.d/10-mail.conf /etc/dovecot/conf.d/10-mail.conf.20180326
cp /etc/dovecot/conf.d/10-master.conf /etc/dovecot/conf.d/10-master.conf.20180326
cp /etc/dovecot/conf.d/10-auth.conf /etc/dovecot/conf.d/10-auth.conf.20180326
cp /etc/dovecot/conf.d/10-ssl.conf /etc/dovecot/conf.d/10-ssl.conf.20180326
cp /etc/dovecot/conf.d/10-logging.conf /etc/dovecot/conf.d/10-logging.conf.20180326

#進行配置
vim /etc/dovecot/dovecot.conf
>protocols = imap pop3
>listen = *
>!include conf.d/*.conf

vim /etc/dovecot/conf.d/10-auth.conf
>disable_plaintext_auth = no
>auth_mechanisms = plain login
>!include auth-system.conf.ext

vi /etc/dovecot/conf.d/10-mail.conf
>mail_location = maildir:~/Maildir

vi /etc/dovecot/conf.d/10-master.conf
>unix_listener /var/spool/postfix/private/auth {    
>     mode = 0666 
>   user = postfix 
>   group = postfix
>}

vim /etc/dovecot/conf.d/10-ssl.conf
>ssl = no

#只要是一個合法郵箱即可,否則無法啟動,這屬於docecot的BUG
vim /etc/dovecot/conf.d/15-lda.conf
>postmaster_address = [email protected]

vim /etc/dovecot/conf.d/10-logging.conf
# Log file to use for informational messages. Defaults to log_path.
>info_log_path = /var/log/dovecot_info.log
# Log file to use for debug messages. Defaults to info_log_path.
>debug_log_path = /var/log/dovecot.debug.log
# 注意:如果後續日誌中提示沒有寫入許可權的話,修改其許可權即可。

#檢視dovecot生效的非預設配置
doveconf -n

/etc/init.d/dovecot start
chkconfig dovecot on
/etc/init.d/portreserve stop
chkconfig portreserve off

*注意:上述命令中的portreserve服務相關的兩行,這個如果啟動的話,你會發現系統重啟後dovecot會無法啟動,
這是因為portreserve佔用了dovecot的埠,所以在此我們禁用portreserve服務。

簡單測試

使用本章開頭建立的使用者 jw1/jwmail

 

image.png

image.png

image.png

此處要建立一個還原點。

三,虛擬使用者配置

為什麼要配置虛擬使用者

如果按照電子郵件的原始定義,郵件是傳送到作業系統的使用者上的。
但是當使用者越來越多的時候,不可能在一臺作業系統上構建特別多的使用者。
於是,就產生了虛擬使用者的概念

實體使用者

image.png

虛擬使用者

image.png

1.postfix配置虛擬使用者

修改main.cf檔案

#先備份
cp /etc/postfix/main.cf /etc/postfix/main.cf.20180327

#修改配置,增加對虛擬使用者的支援
vim /etc/postfix/main.cf
#參見 SHEET 《附件2main.cf》

#其中還需要說明的是我們在此啟用的虛擬使用者是《1.前期準備》中建立的vmail使用者,該使用者的id是5000,
#所以在postfix主配置檔案會看到vmail的家目錄/home/vmail/,以及vmail的id資訊5000。

修改master.cf檔案

#接下來會修改大量的配置檔案,記得備份
#先統一進行備份
cp /etc/postfix/master.cf /etc/postfix/master.cf.20180327
vim /etc/postfix/master.cf
#新增下面兩句
dovecot   unix  –       n       n       –       –       pipe
  flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/dovecot-lda -f ${sender} -d ${recipient}
#注意 flags前面有兩個半形空格。

資料庫連線相關檔案
連線資料庫相關檔案有7個,在建立配置檔案之前,我們要在/etc/postfix/目錄下建立sql目錄用來存放這些配置如下:

mkdir /etc/postfix/sql/

vim /etc/postfix/sql/mysql_virtual_alias_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
query = SELECT goto FROM alias WHERE address='%s' AND active = '1'

vim /etc/postfix/sql/mysql_virtual_alias_domain_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('%u', '@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'

vim /etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'

vim /etc/postfix/sql/mysql_virtual_domains_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
query = SELECT domain FROM domain WHERE domain='%s' AND active = '1'

vim /etc/postfix/sql/mysql_virtual_mailbox_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
query = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1'

vim /etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
query = SELECT maildir FROM mailbox,alias_domain WHERE alias_domain.alias_domain = '%d' and mailbox.username = CONCAT('%u','@',alias_domain.target_domain) AND mailbox.active = 1 AND alias_domain.active='1'

vim /etc/postfix/sql/mysql_virtual_mailbox_limit_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
query = SELECT quota FROM mailbox WHERE username='%s' AND active = '1'

測試sasl與postfix整合

telnet mail.test.net 25
ehlo test.net

#該命令需要手工輸入,而如果出現250-AUTH PLAIN LOGIN和250-AUTH=PLAIN LOGIN兩行,則說明postfix已經正確啟用smtp認證。

image.png

2.dovecot配置虛擬使用者

因為要修改的檔案很多,所以先統一備份

cp /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.20180327
cp /etc/dovecot/conf.d/10-auth.conf /etc/dovecot/conf.d/10-auth.conf.20180327
cp /etc/dovecot/conf.d/10-ssl.conf /etc/dovecot/conf.d/10-ssl.conf.20180327
cp /etc/dovecot/conf.d/10-mail.conf  /etc/dovecot/conf.d/10-mail.conf.20180327
cp /etc/dovecot/conf.d/10-logging.conf /etc/dovecot/conf.d/10-logging.conf.20180327
cp /etc/dovecot/conf.d/10-master.conf /etc/dovecot/conf.d/10-master.conf.20180327
cp /etc/dovecot/conf.d/15-lda.conf /etc/dovecot/conf.d/15-lda.conf.20180327

修改dovecot.conf檔案

vim /etc/dovecot/dovecot.conf
#修改
protocols = imap pop3
listen = *
!include conf.d/*.conf

#追加
passdb {
  driver = sql
  args = /etc/dovecot/dovecot-sql.conf.ext
}
userdb {
  driver = static
  args = uid=5000 gid=5000 home=/home/vmail/%d/%n
}
#log
auth_debug_passwords=yes
mail_debug=yes
auth_verbose=yes
auth_verbose_passwords=plain

修改10-auth.conf檔案

vim /etc/dovecot/conf.d/10-auth.conf
#修改紅色
disable_plaintext_auth = no
auth_mechanisms = plain login cram-md5
!include auth-system.conf.ext

修改10-mail.conf檔案

vim /etc/dovecot/conf.d/10-mail.conf
#修改
mail_location = maildir:/home/vmail/%d/%n
mbox_write_locks = fcntl

修改10-master.conf檔案
10-master.conf檔案定義了dovecot的pop3和imap埠,以及其他的一些資訊。

vim /etc/dovecot/conf.d/10-master.conf

  inet_listener imap {
    port = 143
  }

  inet_listener pop3 {
    port = 110
  }

service auth {
   unix_listener auth-userdb {
mode = 0600
    user = vmail
    group = vmail
  }
  # Postfix smtp-auth
unix_listener /var/spool/postfix/private/auth {
    mode = 0666
    user = postfix
    #group = postfix (註釋掉)
  }
}

新增dovecot-sql.conf.ext檔案

vim /etc/dovecot/dovecot-sql.conf.ext                                               
driver = mysql
connect = host=localhost dbname=postfix user=postfix password=postfix
default_pass_scheme = MD5-CRYPT
password_query = SELECT username AS user,password FROM mailbox WHERE username = '%u' AND active='1'
user_query = SELECT maildir, 5000 AS uid, 5000 AS gid, CONCAT('dict:storage=',floor(quota/1000),' proxy::quota') as quota FROM mailbox WHERE username = '%u' AND active='1'
#注意: proxy::quata 的前面有半形空格  

3.postfixadmin配置

確保LAMP環境

安裝postfixadmin

wget http://nchc.dl.sourceforge.net/project/postfixadmin/postfixadmin/postfixadmin-2.93/postfixadmin-2.93.tar.gz
tar -xf postfixadmin-2.93.tar.gz
mv postfixadmin-2.93 /var/www/html/postfixadmin
chown -R apache:apache /var/www/html/postfixadmin
chmod -R 755 /var/www/html/postfixadmin

配置postfixadmin

#先備份
cp /var/www/html/postfixadmin/config.inc.php /var/www/html/postfixadmin/config.inc.php.20180327
vim /var/www/html/postfixadmin/config.inc.php
#下列均需要修改
$CONF['configured'] = true;
$CONF['default_language'] = 'cn';
$CONF['database_type'] = 'mysql';
$CONF['database_host'] = 'localhost';
$CONF['database_user'] = 'postfix';
$CONF['database_password'] = 'postfix';
$CONF['database_name'] = 'postfix';
$CONF['encrypt'] = 'dovecot:CRAM-MD5';
$CONF['aliases'] = '1000';
$CONF['mailboxes'] = '1000';
$CONF['maxquota'] = '1000';
$CONF['fetchmail'] = 'NO';
$CONF['quota'] = 'YES';
$CONF['used_quotas'] = 'YES';
$CONF['new_quota_table'] = 'NO';

啟動postfixadmin

#啟動apache
#如果已經啟動,就restart
/etc/init.d/httpd restart

瀏覽器中開啟
http://192.168.2.234/postfixadmin/setup.php

該頁面是自檢頁面

 

image.png

 

image.png

 

image.png

 

image.png

vim /var/www/html/postfixadmin/config.inc.php
# 修改為剛才畫面上的值
$CONF['setup_password'] = 'changeme';

image.png

回到web頁面

 

image.png

 

image.png

登入: http://192.168.2.234/postfixadmin/login.php

登入

 

image.png

 

image.png

 

image.png

 

image.png


新建兩個使用者,密碼都是user2018

image.png

 

image.png

 

image.png


可以在此為該伺服器設定一個還原點

 

四,郵件連線測試

注意是 IMAP,虛擬使用者只支援IMAP,即143埠

 

image.png

 

image.png

嘗試建立一個不存在的賬戶的連線,說明伺服器的驗證生效了。

 

image.png

image.png

 

image.png

 

image.png

 

使用user2 給 user1 回信

 

image.png

 

image.png


可以在此為該伺服器設定一個還原點。

五,WEB端郵件系統

1.下載roundcube webmail軟體包

wget http://jaist.dl.sourceforge.net/project/roundcubemail/roundcubemail/1.1.4/roundcubemail-1.1.4-complete.tar.gz
#解壓roundcube webmail軟體包,並移動到apache根目錄下,如下:
tar -xf roundcubemail-1.1.4-complete.tar.gz
mv roundcubemail-1.1.4 /var/www/html/webmail/
chown -R apache:apache /var/www/html/webmail/
chmod -R 755 /var/www/html/webmail/
ll /var/www/html/webmail/

image.png

2.配置webmail

#安裝pear環境,為了下面更新PHP
wget http://pear.php.net/go-pear.phar
php go-pear.phar

image.png

 

上圖中間需要敲一個回車。

pear channel-update pear.php.net
pear install Auth_SASL Net_SMTP Net_IDNA2-0.1.1 Mail_Mime

修改配置:

vim /etc/php.ini +889
date.timezone = Asia/Chongqing
#以上修改完畢後,重啟apache,使用如下命令:
/etc/init.d/httpd restart

建庫,建使用者

mysql -uroot -p 
#輸入123456
mysql>create database webmail default character set utf8 collate utf8_bin;
mysql>grant all on webmail.* to 'webmail'@'%' identified by 'webmail';
mysql>flush privileges;
mysql>quit

測試新使用者

mysql -uwebmail -pwebmail

3.安裝roundcube webmail

http://192.168.2.234/webmail/installer/index.php
預設進行環境檢驗

image.png

 

image.png


填寫各種配置

image.png

 

image.png

 

image.png

 

image.png

 

image.png

 

image.png


點選【繼續】

image.png


點選【初始化資料庫】

image.png

 

image.png

 

image.png


WEB上收發郵件的測試

image.png

 

image.png

 

image.png


WEB上的登入測試

image.png

 

image.png

 

4.測試WEB版

http://192.168.2.234/webmail/
輸入之前建立的虛擬使用者

image.png

 

image.png

 

基於安全考慮,請刪除 installer/index.php 這個檔案。防止他人誤操作。
可以在此為該伺服器設定一個還原點。

至此,整個伺服器搭建完畢。

六,維護類命令

#開啟關閉 apache        
/etc/init.d/httpd start 

#開啟關閉 mysql             
/etc/init.d/mysqld start
/etc/init.d/mysqld restart #重啟      
/etc/init.d/mysqld stop #停止     
/etc/init.d/mysqld start #啟動    
        
#開啟關閉防火牆        
chkconfig iptables off  
                
#開啟關閉postfix        
/etc/init.d/postfix start

#檢視傳送郵件的Log 
tail -f /var/log/maillog

#列出POSTFIX非預設值的設定資料。
postconf -n

#開啟關閉dovecot        
/etc/init.d/dovecot start   
chkconfig dovecot on    
cat /var/log/dovecot_info.log   
cat /var/log/dovecot.debug.log

#檢視dovecot生效的非預設配置
doveconf -n

檢視postfix對系統的影響
進入 /home/vmail/ 能看到 所有的虛擬域名
進入某一個虛擬域名,就能看到所有該域名下的使用者

#全域性搜尋檔案     
find / -name  'xxx' 
#檢視系統的整體安全LOG       
cat  /var/log/secure    

七,附件

1.附件1main.cf

# Global Postfix configuration file. This file lists only a subset
# of all parameters. For the syntax, and for a complete parameter
# list, see the postconf(5) manual page (command: "man 5 postconf").
#
# For common configuration examples, see BASIC_CONFIGURATION_README
# and STANDARD_CONFIGURATION_README. To find these documents, use
# the command "postconf html_directory readme_directory", or go to
# http://www.postfix.org/.
#
# For best results, change no more than 2-3 parameters at a time,
# and test if Postfix still works after every change.

# SOFT BOUNCE
#
# The soft_bounce parameter provides a limited safety net for
# testing.  When soft_bounce is enabled, mail will remain queued that
# would otherwise bounce. This parameter disables locally-generated
# bounces, and prevents the SMTP server from rejecting mail permanently
# (by changing 5xx replies into 4xx replies). However, soft_bounce
# is no cure for address rewriting mistakes or mail routing mistakes.
#
#soft_bounce = no

# LOCAL PATHNAME INFORMATION
#
# The queue_directory specifies the location of the Postfix queue.
# This is also the root directory of Postfix daemons that run chrooted.
# See the files in examples/chroot-setup for setting up Postfix chroot
# environments on different UNIX systems.
#
queue_directory = /var/spool/postfix

# The command_directory parameter specifies the location of all
# postXXX commands.
#
command_directory = /usr/sbin

# The daemon_directory parameter specifies the location of all Postfix
# daemon programs (i.e. programs listed in the master.cf file). This
# directory must be owned by root.
#
daemon_directory = /usr/libexec/postfix

# The data_directory parameter specifies the location of Postfix-writable
# data files (caches, random numbers). This directory must be owned
# by the mail_owner account (see below).
#
data_directory = /var/lib/postfix

# QUEUE AND PROCESS OWNERSHIP
#
# The mail_owner parameter specifies the owner of the Postfix queue
# and of most Postfix daemon processes.  Specify the name of a user
# account THAT DOES NOT SHARE ITS USER OR GROUP ID WITH OTHER ACCOUNTS
# AND THAT OWNS NO OTHER FILES OR PROCESSES ON THE SYSTEM.  In
# particular, don't specify nobody or daemon. PLEASE USE A DEDICATED
# USER.
#
mail_owner = postfix

# The default_privs parameter specifies the default rights used by
# the local delivery agent for delivery to external file or command.
# These rights are used in the absence of a recipient user context.
# DO NOT SPECIFY A PRIVILEGED USER OR THE POSTFIX OWNER.
#
#default_privs = nobody

# INTERNET HOST AND DOMAIN NAMES
# 
# The myhostname parameter specifies the internet hostname of this
# mail system. The default is to use the fully-qualified domain name
# from gethostname(). $myhostname is used as a default value for many
# other configuration parameters.
#
#myhostname = host.domain.tld
#myhostname = virtual.domain.tld
myhostname = mail.test.net

# The mydomain parameter specifies the local internet domain name.
# The default is to use $myhostname minus the first component.
# $mydomain is used as a default value for many other configuration
# parameters.
#
#mydomain = domain.tld
mydomain = test.net

# SENDING MAIL
# 
# The myorigin parameter specifies the domain that locally-posted
# mail appears to come from. The default is to append $myhostname,
# which is fine for small sites.  If you run a domain with multiple
# machines, you should (1) change this to $mydomain and (2) set up
# a domain-wide alias database that aliases each user to
# [email protected]
#
# For the sake of consistency between sender and recipient addresses,
# myorigin also specifies the default domain name that is appended
# to recipient addresses that have no @domain part.
#
#myorigin = $myhostname
myorigin = $mydomain

# RECEIVING MAIL

# The inet_interfaces parameter specifies the network interface
# addresses that this mail system receives mail on.  By default,
# the software claims all active interfaces on the machine. The
# parameter also controls delivery of mail to [email protected][ip.address].
#
# See also the proxy_interfaces parameter, for network addresses that
# are forwarded to us via a proxy or network address translator.
#
# Note: you need to stop/start Postfix when this parameter changes.
#
inet_interfaces = all
#inet_interfaces = $myhostname
#inet_interfaces = $myhostname, localhost
#inet_interfaces = localhost

# Enable IPv4, and IPv6 if supported
inet_protocols = ipv4

# The proxy_interfaces parameter specifies the network interface
# addresses that this mail system receives mail on by way of a
# proxy or network address translation unit. This setting extends
# the address list specified with the inet_interfaces parameter.
#
# You must specify your proxy/NAT addresses when your system is a
# backup MX host for other domains, otherwise mail delivery loops
# will happen when the primary MX host is down.
#
#proxy_interfaces =
#proxy_interfaces = 1.2.3.4

# The mydestination parameter specifies the list of domains that this
# machine considers itself the final destination for.
#
# These domains are routed to the delivery agent specified with the
# local_transport parameter setting. By default, that is the UNIX
# compatible delivery agent that lookups all recipients in /etc/passwd
# and /etc/aliases or their equivalent.
#
# The default is $myhostname + localhost.$mydomain.  On a mail domain
# gateway, you should also include $mydomain.
#
# Do not specify the names of virtual domains - those domains are
# specified elsewhere (see VIRTUAL_README).
#
# Do not specify the names of domains that this machine is backup MX
# host for. Specify those names via the relay_domains settings for
# the SMTP server, or use permit_mx_backup if you are lazy (see
# STANDARD_CONFIGURATION_README).
#
# The local machine is always the final destination for mail addressed
# to [email protected][the.net.work.address] of an interface that the mail system
# receives mail on (see the inet_interfaces parameter).
#
# Specify a list of host or domain names, /file/name or type:table
# patterns, separated by commas and/or whitespace. A /file/name
# pattern is replaced by its contents; a type:table is matched when
# a name matches a lookup key (the right-hand side is ignored).
# Continue long lines by starting the next line with whitespace.
#
# See also below, section "REJECTING MAIL FOR UNKNOWN LOCAL USERS".
#
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
#

# REJECTING MAIL FOR UNKNOWN LOCAL USERS
#
# The local_recipient_maps parameter specifies optional lookup tables
# with all names or addresses of users that are local with respect
# to $mydestination, $inet_interfaces or $proxy_interfaces.
#
# If this parameter is defined, then the SMTP server will reject
# mail for unknown local users. This parameter is defined by default.
#
# To turn off local recipient checking in the SMTP server, specify
# local_recipient_maps = (i.e. empty).
#
# The default setting assumes that you use the default Postfix local
# delivery agent for local delivery. You need to update the
# local_recipient_maps setting if:
#
# - You define $mydestination domain recipients in files other than
#   /etc/passwd, /etc/aliases, or the $virtual_alias_maps files.
#   For example, you define $mydestination domain recipients in    
#   the $virtual_mailbox_maps files.
#
# - You redefine the local delivery agent in master.cf.
#
# - You redefine the "local_transport" setting in main.cf.
#
# - You use the "luser_relay", "mailbox_transport", or "fallback_transport"
#   feature of the Postfix local delivery agent (see local(8)).
#
# Details are described in the LOCAL_RECIPIENT_README file.
#
# Beware: if the Postfix SMTP server runs chrooted, you probably have
# to access the passwd file via the proxymap service, in order to
# overcome chroot restrictions. The alternative, having a copy of
# the system passwd file in the chroot jail is just not practical.
#
# The right-hand side of the lookup tables is conveniently ignored.
# In the left-hand side, specify a bare username, an @domain.tld
# wild-card, or specify a [email protected] address.
# 
#local_recipient_maps = unix:passwd.byname $alias_maps
#local_recipient_maps = proxy:unix:passwd.byname $alias_maps
local_recipient_maps =

# The unknown_local_recipient_reject_code specifies the SMTP server
# response code when a recipient domain matches $mydestination or
# ${proxy,inet}_interfaces, while $local_recipient_maps is non-empty
# and the recipient address or address local-part is not found.
#
# The default setting is 550 (reject mail) but it is safer to start
# with 450 (try again later) until you are certain that your
# local_recipient_maps settings are OK.
#
unknown_local_recipient_reject_code = 550

# TRUST AND RELAY CONTROL

# The mynetworks parameter specifies the list of "trusted" SMTP
# clients that have more privileges than "strangers".
#
# In particular, "trusted" SMTP clients are allowed to relay mail
# through Postfix.  See the smtpd_recipient_restrictions parameter
# in postconf(5).
#
# You can specify the list of "trusted" network addresses by hand
# or you can let Postfix do it for you (which is the default).
#
# By default (mynetworks_style = subnet), Postfix "trusts" SMTP
# clients in the same IP subnetworks as the local machine.
# On Linux, this does works correctly only with interfaces specified
# with the "ifconfig" command.
# 
# Specify "mynetworks_style = class" when Postfix should "trust" SMTP
# clients in the same IP class A/B/C networks as the local machine.
# Don't do this with a dialup site - it would cause Postfix to "trust"
# your entire provider's network.  Instead, specify an explicit
# mynetworks list by hand, as described below.
#  
# Specify "mynetworks_style = host" when Postfix should "trust"
# only the local machine.
# 
#mynetworks_style = class
#mynetworks_style = subnet
#mynetworks_style = host

# Alternatively, you can specify the mynetworks list by hand, in
# which case Postfix ignores the mynetworks_style setting.
#
# Specify an explicit list of network/netmask patterns, where the
# mask specifies the number of bits in the network part of a host
# address.
#
# You can also specify the absolute pathname of a pattern file instead
# of listing the patterns here. Specify type:table for table-based lookups
# (the value on the table right-hand side is not used).
#
#mynetworks = 168.100.189.0/28, 127.0.0.0/8
#mynetworks = $config_directory/mynetworks
#mynetworks = hash:/etc/postfix/network_table

# The relay_domains parameter restricts what destinations this system will
# relay mail to.  See the smtpd_recipient_restrictions description in
# postconf(5) for detailed information.
#
# By default, Postfix relays mail
# - from "trusted" clients (IP address matches $mynetworks) to any destination,
# - from "untrusted" clients to destinations that match $relay_domains or
#   subdomains thereof, except addresses with sender-specified routing.
# The default relay_domains value is $mydestination.
# 
# In addition to the above, the Postfix SMTP server by default accepts mail
# that Postfix is final destination for:
# - destinations that match $inet_interfaces or $proxy_interfaces,
# - destinations that match $mydestination
# - destinations that match $virtual_alias_domains,
# - destinations that match $virtual_mailbox_domains.
# These destinations do not need to be listed in $relay_domains.
# 
# Specify a list of hosts or domains, /file/name patterns or type:name
# lookup tables, separated by commas and/or whitespace.  Continue
# long lines by starting the next line with whitespace. A file name
# is replaced by its contents; a type:name table is matched when a
# (parent) domain appears as lookup key.
#
# NOTE: Postfix will not automatically forward mail for domains that
# list this system as their primary or backup MX host. See the
# permit_mx_backup restriction description in postconf(5).
#
#relay_domains = $mydestination

# INTERNET OR INTRANET

# The relayhost parameter specifies the default host to send mail to
# when no entry is matched in the optional transport(5) table. When
# no relayhost is given, mail is routed directly to the destination.
#
# On an intranet, specify the organizational domain name. If your
# internal DNS uses no MX records, specify the name of the intranet
# gateway host instead.
#
# In the case of SMTP, specify a domain, host, host:port, [host]:port,
# [address] or [address]:port; the form [host] turns off MX lookups.
#
# If you're connected via UUCP, see also the default_transport parameter.
#
#relayhost = $mydomain
#relayhost = [gateway.my.domain]
#relayhost = [mailserver.isp.tld]
#relayhost = uucphost
#relayhost = [an.ip.add.ress]

# REJECTING UNKNOWN RELAY USERS
#
# The relay_recipient_maps parameter specifies optional lookup tables
# with all addresses in the domains that match $relay_domains.
#
# If this parameter is defined, then the SMTP server will reject
# mail for unknown relay users. This feature is off by default.
#
# The right-hand side of the lookup tables is conveniently ignored.
# In the left-hand side, specify an @domain.tld wild-card, or specify
# a [email protected] address.
# 
#relay_recipient_maps = hash:/etc/postfix/relay_recipients

# INPUT RATE CONTROL
#
# The in_flow_delay configuration parameter implements mail input
# flow control. This feature is turned on by default, although it
# still needs further development (it's disabled on SCO UNIX due
# to an SCO bug).
# 
# A Postfix process will pause for $in_flow_delay seconds before
# accepting a new message, when the message arrival rate exceeds the
# message delivery rate. With the default 100 SMTP server process
# limit, this limits the mail inflow to 100 messages a second more
# than the number of messages delivered per second.
# 
# Specify 0 to disable the feature. Valid delays are 0..10.
# 
#in_flow_delay = 1s

# ADDRESS REWRITING
#
# The ADDRESS_REWRITING_README document gives information about
# address masquerading or other forms of address rewriting including
# username->Firstname.Lastname mapping.

# ADDRESS REDIRECTION (VIRTUAL DOMAIN)
#
# The VIRTUAL_README document gives information about the many forms
# of domain hosting that Postfix supports.

# "USER HAS MOVED" BOUNCE MESSAGES
#
# See the discussion in the ADDRESS_REWRITING_README document.

# TRANSPORT MAP
#
# See the discussion in the ADDRESS_REWRITING_README document.

# ALIAS DATABASE
#
# The alias_maps parameter specifies the list of alias databases used
# by the local delivery agent. The default list is system dependent.
#
# On systems with NIS, the default is to search the local alias
# database, then the NIS alias database. See aliases(5) for syntax
# details.
# 
# If you change the alias database, run "postalias /etc/aliases" (or
# wherever your system stores the mail alias file), or simply run
# "newaliases" to build the necessary DBM or DB file.
#
# It will take a minute or so before changes become visible.  Use
# "postfix reload" to eliminate the delay.
#
#alias_maps = dbm:/etc/aliases
alias_maps = hash:/etc/aliases
#alias_maps = hash:/etc/aliases, nis:mail.aliases
#alias_maps = netinfo:/aliases

# The alias_database parameter specifies the alias database(s) that
# are built with "newaliases" or "sendmail -bi".  This is a separate
# configuration parameter, because alias_maps (see above) may specify
# tables that are not necessarily all under control by Postfix.
#
#alias_database = dbm:/etc/aliases
#alias_database = dbm:/etc/mail/aliases
alias_database = hash:/etc/aliases
#alias_database = hash:/etc/aliases, hash:/opt/majordomo/aliases

# ADDRESS EXTENSIONS (e.g., user+foo)
#
# The recipient_delimiter parameter specifies the separator between
# user names and address extensions (user+foo). See canonical(5),
# local(8), relocated(5) and virtual(5) for the effects this has on
# aliases, canonical, virtual, relocated and .forward file lookups.
# Basically, the software tries user+foo and .forward+foo before
# trying user and .forward.
#
#recipient_delimiter = +

# DELIVERY TO MAILBOX
#
# The home_mailbox parameter specifies the optional pathname of a
# mailbox file relative to a user's home directory. The default
# mailbox file is /var/spool/mail/user or /var/mail/user.  Specify
# "Maildir/" for qmail-style delivery (the / is required).
#
#home_mailbox = Mailbox
home_mailbox = Maildir/
 
# The mail_spool_directory parameter specifies the directory where
# UNIX-style mailboxes are kept. The default setting depends on the
# system type.
#
#mail_spool_directory = /var/mail
#mail_spool_directory = /var/spool/mail

# The mailbox_command parameter specifies the optional external
# command to use instead of mailbox delivery. The command is run as
# the recipient with proper HOME, SHELL and LOGNAME environment settings.
# Exception:  delivery for root is done as $default_user.
#
# Other environment variables of interest: USER (recipient username),
# EXTENSION (address extension), DOMAIN (domain part of address),
# and LOCAL (the address localpart).
#
# Unlike other Postfix configuration parameters, the mailbox_command
# parameter is not subjected to $parameter substitutions. This is to
# make it easier to specify shell syntax (see example below).
#
# Avoid shell meta characters because they will force Postfix to run
# an expensive shell process. Procmail alone is expensive enough.
#
# IF YOU USE THIS TO DELIVER MAIL SYSTEM-WIDE, YOU MUST SET UP AN
# ALIAS THAT FORWARDS MAIL FOR ROOT TO A REAL USER.
#
#mailbox_command = /some/where/procmail
#mailbox_command = /some/where/procmail -a "$EXTENSION"

# The mailbox_transport specifies the optional transport in master.cf
# to use after processing aliases and .forward files. This parameter
# has precedence over the mailbox_command, fallback_transport and
# luser_relay parameters.
#
# Specify a string of the form transport:nexthop, where transport is
# the name of a mail delivery transport defined in master.cf.  The
# :nexthop part is optional. For more details see the sample transport
# configuration file.
#
# NOTE: if you use this feature for accounts not in the UNIX password
# file, then you must update the "local_recipient_maps" setting in
# the main.cf file, otherwise the SMTP server will reject mail for    
# non-UNIX accounts with "User unknown in local recipient table".
#
#mailbox_transport = lmtp:unix:/var/lib/imap/socket/lmtp

# If using the cyrus-imapd IMAP server deliver local mail to the IMAP
# server using LMTP (Local Mail Transport Protocol), this is prefered
# over the older cyrus deliver program by setting the
# mailbox_transport as below:
#
# mailbox_transport = lmtp:unix:/var/lib/imap/socket/lmtp
#
# The efficiency of LMTP delivery for cyrus-imapd can be enhanced via
# these settings.
#
# local_destination_recipient_limit = 300
# local_destination_concurrency_limit = 5
#
# Of course you should adjust these settings as appropriate for the
# capacity of the hardware you are using. The recipient limit setting
# can be used to take advantage of the single instance message store
# capability of Cyrus. The concurrency limit can be used to control
# how many simultaneous LMTP sessions will be permitted to the Cyrus
# message store. 
#
# To use the old cyrus deliver program you have to set:
#mailbox_transport = cyrus

# The fallback_transport specifies the optional transport in master.cf
# to use for recipients that are not found in the UNIX passwd database.
# This parameter has precedence over the luser_relay parameter.
#
# Specify a string of the form transport:nexthop, where transport is
# the name of a mail delivery transport defined in master.cf.  The
# :nexthop part is optional. For more details see the sample transport
# configuration file.
#
# NOTE: if you use this feature for accounts not in the UNIX password
# file, then you must update the "local_recipient_maps" setting in
# the main.cf file, otherwise the SMTP server will reject mail for    
# non-UNIX accounts with "User unknown in local recipient table".
#
#fallback_transport = lmtp:unix:/var/lib/imap/socket/lmtp
#fallback_transport =

# The luser_relay parameter specifies an optional destination address
# for unknown recipients.  By default, mail for [email protected]$mydestination,
# [email protected][$inet_interfaces] or [email protected][$proxy_interfaces] is returned
# as undeliverable.
#
# The following expansions are done on luser_relay: $user (recipient
# username), $shell (recipient shell), $home (recipient home directory),
# $recipient (full recipient address), $extension (recipient address
# extension), $domain (recipient domain), $local (entire recipient
# localpart), $recipient_delimiter. Specify ${name?value} or
# ${name:value} to expand value only when $name does (does not) exist.
#
# luser_relay works only for the default Postfix local delivery agent.
#
# NOTE: if you use this feature for accounts not in the UNIX password
# file, then you must specify "local_recipient_maps =" (i.e. empty) in
# the main.cf file, otherwise the SMTP server will reject mail for    
# non-UNIX accounts with "User unknown in local recipient table".
#
#luser_relay = [email protected]
#luser_relay = [email protected]
#luser_relay = admin+$local
  
# JUNK MAIL CONTROLS
# 
# The controls listed here are only a very small subset. The file
# SMTPD_ACCESS_README provides an overview.

# The header_checks parameter specifies an optional table with patterns
# that each logical message header is matched against, including
# headers that span multiple physical lines.
#
# By default, these patterns also apply to MIME headers and to the
# headers of attached messages. With older Postfix versions, MIME and
# attached message headers were treated as body text.
#
# For details, see "man header_checks".
#
#header_checks = regexp:/etc/postfix/header_checks

# FAST ETRN SERVICE
#
# Postfix maintains per-destination logfiles with information about
# deferred mail, so that mail can be flushed quickly with the SMTP
# "ETRN domain.tld" command, or by executing "sendmail -qRdomain.tld".
# See the ETRN_README document for a detailed description.
# 
# The fast_flush_domains parameter controls what destinations are
# eligible for this service. By default, they are all domains that
# this server is willing to relay mail to.
# 
#fast_flush_domains = $relay_domains

# SHOW SOFTWARE VERSION OR NOT
#
# The smtpd_banner parameter specifies the text that follows the 220
# code in the SMTP server's greeting banner. Some people like to see
# the mail version advertised. By default, Postfix shows no version.
#
# You MUST specify $myhostname at the start of the text. That is an
# RFC requirement. Postfix itself does not care.
#
#smtpd_banner = $myhostname ESMTP $mail_name
#smtpd_banner = $myhostname ESMTP $mail_name ($mail_version)

# PARALLEL DELIVERY TO THE SAME DESTINATION
#
# How many parallel deliveries to the same user or domain? With local
# delivery, it does not make sense to do massively parallel delivery
# to the same user, because mailbox updates must happen sequentially,
# and expensive pipelines in .forward files can cause disasters when
# too many are run at the same time. With SMTP deliveries, 10
# simultaneous connections to the same domain could be sufficient to
# raise eyebrows.
# 
# Each message delivery transport has its XXX_destination_concurrency_limit
# parameter.  The default is $default_destination_concurrency_limit for
# most delivery transports. For the local delivery agent the default is 2.

#local_destination_concurrency_limit = 2
#default_destination_concurrency_limit = 20

# DEBUGGING CONTROL
#
# The debug_peer_level parameter specifies the increment in verbose
# logging level when an SMTP client or server host name or address
# matches a pattern in the debug_peer_list parameter.
#
debug_peer_level = 2

# The debug_peer_list parameter specifies an optional list of domain
# or network patterns, /file/name patterns or type:name tables. When
# an SMTP client or server host name or address matches a pattern,
# increase the verbose logging level by the amount specified in the
# debug_peer_level parameter.
#
#debug_peer_list = 127.0.0.1
#debug_peer_list = some.domain

# The debugger_command specifies the external command that is executed
# when a Postfix daemon program is run with the -D option.
#
# Use "command .. & sleep 5" so that the debugger can attach before
# the process marches on. If you use an X-based debugger, be sure to
# set up your XAUTHORITY environment variable before starting Postfix.
#
debugger_command =



# If you can't use X, use this to capture the call stack when a
# daemon crashes. The result is in a file in the configuration
# directory, and is named after the process name and the process ID.
#
# debugger_command =
#
#
#
#
# Another possibility is to run gdb under a detached screen session.
# To attach to the screen sesssion, su root and run "screen -r
# <id_string>" where <id_string> uniquely matches one of the detached
# sessions (from "screen -list").
#
# debugger_command =
#
#
#

# INSTALL-TIME CONFIGURATION INFORMATION
#
# The following parameters are used when installing a new Postfix version.
# 
# sendmail_path: The full pathname of the Postfix sendmail command.
# This is the Sendmail-compatible mail posting interface.
# 
sendmail_path = /usr/sbin/sendmail.postfix

# newaliases_path: The full pathname of the Postfix newaliases command.
# This is the Sendmail-compatible command to build alias databases.
#
newaliases_path = /usr/bin/newaliases.postfix

# mailq_path: The full pathname of the Postfix mailq command.  This
# is the Sendmail-compatible mail queue listing command.
# 
mailq_path = /usr/bin/mailq.postfix

# setgid_group: The group for mail submission and queue management
# commands.  This must be a group name with a numerical group ID that
# is not shared with other accounts, not even with the Postfix account.
#
setgid_group = postdrop

# html_directory: The location of the Postfix HTML documentation.
#
html_directory = no

# manpage_directory: The location of the Postfix on-line manual pages.
#
manpage_directory = /usr/share/man

# sample_directory: The location of the Postfix sample configuration files.
# This parameter is obsolete as of Postfix 2.1.
#
sample_directory = /usr/share/doc/postfix-2.6.6/samples

# readme_directory: The location of the Postfix README files.
#
readme_directory = /usr/share/doc/postfix-2.6.6/README_FILES

#啟用SMTP認證
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_application_name = smtpd
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
broken_sasl_auth_clients = yes
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination,reject_unknown_sender_domain
smtpd_sasl_security_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
smtpd_client_restrictions = permit_sasl_authenticated
smtpd_sasl_security_options = noanonymous
proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $v