9Linux網路服務之Postfix郵件系統的搭建

Postfix郵件系統的搭建

一 電子郵件系統概述

·郵件系統角色

MUA(郵件使用者代理)、MTA(郵件傳輸代理)、MDA(郵件分發代理 )

·郵件應用協議

SMTP(發信),簡單郵件傳輸協議,TCP 25埠

POP3(收信),第3版郵局協議,TCP 110埠

IMAP4,第4版網際網路訊息訪問協議,TCP 143埠

·商業郵件系統

Exchange:

微軟公司的重量級產品,與Windows系列軟體產品相整合,協作性較 好

Notes/Domino:

IBM公司的商業電子郵件和辦公協作軟體產品,功能豐富、強大,提供跨平臺支援

·開源郵件系統

Sendmail:

資格最古老,執行穩定,但安全性欠佳

Qmail:

有更好的執行效率,配置、管理也很方便

Postfix:

相容Sendmail,採用模組化設計,在投遞效率、穩定性、服務效能及安 全性方面表現優秀,官網:www。postfix。org

Dovecot:

配合postfix使用,官方站點:http://www。dovecot。org

二 postfix郵件伺服器的基本構建

·郵件伺服器的基本結構

Postfix:

提供郵件傳送服務(SMTP)

Dovecot:

提供郵件收取服務(POP3)

Office Outlook:

用於收發信的客戶端工具

·Postfix安裝與介紹

1準備工作

/etc/init。d/sendmail stop

停用sendmail服務,

chkconfig ——level 2345 sendmail off

設定預設啟動狀態為關閉

yum install cyrus-sasl-*

認證相關

yum install db*-devel

資料庫相關

yum install pam-devel

資料庫認證相關

2建立執行使用者和組

groupadd -g 1200 postdrop 建立專門的執行組名固定

groupadd -g 1000 postfix 同上

useradd -M -u 1000 -g postfix -G postdrop -s /sbin/nologin postfix 使用者

3原始碼編譯及安裝

A解包

tar zxf postfix-2。10。0。tar。gz -C /usr/src/

cd /usr/src/postfix-2。10。tar。gz

B配置

make makefiles ‘CCARGS=-DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl’ ‘AUXLIBS=-L/usr/lib/sasl2 -lsasl2’

CCARGS:

為編譯器提供額外的擴充套件引數,如啟用SASL認證

AUXLIBS:

為編譯器指出位於標準位置之外的額外函式庫

C編譯安裝

make && make install

D檢查是否支援認證(無顯示需重新配置)

postconf -a

cyrus 支援cyrus認證

dovecot 支援dovecot認證

4 postfix服務控制

postfix start

開啟服務

postfix status

檢視執行狀態

netstat -antp | grep “:25”

檢視埠號,注意程序名為master

postfix check

配置完後檢查語法,無錯誤則無輸出

postfix reload

重新載入

5 Postfix的主要目錄

配置檔案目錄:

/etc/postfix/

服務程式目錄:

/usr/libexec/postfix/

郵件佇列目錄:

/var/spool/postfix/

Incoming(存放剛收到的郵件)、active(正在投遞的郵件)、deferred(傳遞失敗的郵件)、hold(被阻止傳送的郵件)、corrupt(不可讀或不可分析的郵件)

郵件管理程式目錄:

/usr/sbin/

Postalias(構造、修改、查詢別名表)、postmap(構造、修改、查詢表文件、postconf(顯示和編輯main。cf配置檔案)、postfix(啟動、停止服務)、postqueue(檢視郵件列隊)、postsuper(管理郵件列隊)

6 Postfix的配置檔案

/etc/postfix/master.cf

master主程式的配置檔案

/etc/postfix/main.cf

postfix服務的配置檔案

輔助配置工具 postconf

postconf

檢視當前的有效配置

postconf -n

檢視非預設配置

postconf -d

檢視預設配置

7 Postfix的日誌檔案

/var/log/maillog

記錄了postfix服務的郵件傳遞等過程資訊

常用分析方法

動態跟蹤日誌變化

tail -f /var/log/maillog

關鍵詞搜尋

egrep ‘(reject|error|fatal|panic):’ /var/log/maillog

8配置SMTP發信服務

a伺服器所使用的相關配置案例環境

IP地址

:173。16。16。2/24

主機名:

mazi。benet。com

郵件域:

@benet。com

郵件帳號:

使用本地系統使用者;使用dovecot提供收信服務

域名設定

:需要在 benet。com 域的DNS伺服器中註冊設定相應的A記錄和MX記錄

b設定main.cf並啟動服務

vim /etc/postfix/main。cf //新增以下引數,並且每行開頭不能有空格

inet_interfaces = 192。168。1。100, 127。0。0。1 //伺服器地址

myhostname = mazi。benet。com 主機名

mydomain = benet。com 郵件域

myorigin = $mydomain 外發郵件時發件人地址中的郵件域

mydestination = $mydomain, $myhostname 允許投遞到本地的目標郵件域

home_mailbox = Maildir/ 設定郵件儲存位置和格式(Maildir是一種郵箱儲存方式)

postfix check 檢測配置檔案的語法是否有錯

postfix reload 過載

9新增郵箱賬號

Postfix伺服器預設使用系統使用者作為郵件賬號

groupadd mailusers

useradd -g mailusers -s /sbin/nologin lisi

useradd -g mailusers -s /sbin/nologin zhangsan

passwd lisi

passwd zhangsan

10 SMTP傳送測試

telnet mail。benet。com 25 連線伺服器

Trying 192。168。1。100。。。

Connected to mail。benet。com (192。168。1。100)。

Escape character is ‘^]’。

220 mazi。benet。com ESMTP Postfix

HELO mail。benet。com 宣告客戶機地址

250 mazi。benet。com

MAIL FROM:lisi@benet。com 告知發件人地址

250 2。1。0 Ok

RCPT TO:zhangsan@benet。com 告知收件人地址

250 2。1。5 Ok

DATA 告知要寫正文了

354 End data with

Subject:A Test Mail 正文

Dear zhangsan:

This is a test mail by lisi.

. 以點表示正文結束

250 2。0。0 Ok: queued as D4B7DA809A

Quit 退出

221 2。0。0 Bye

Connection closed by foreign host。

ls -lh /home/zhangsan/Maildir/new/ 驗證檢視新接收的郵件

·dovecot的安裝與介紹

1預先建立執行使用者

useradd -M -s /sbin/nologin dovecot

useradd -M -s /sbin/nologin dovenull

2原始碼編譯及安裝

解包

tar zxf dovecot-2。2。2。tar。gz -C /usr/src/

cd /usr/src/dovecot-2。2。2/

配置

。/configure ——sysconfdir=/etc

指定配置文件的父目錄

編譯及安裝

make && make install

3新增為系統服務

cp doc/dovecot-initd。sh /etc/init。d/dovecot

chmod +x /etc/init。d/dovecot

chkconfig ——add dovecot

4建立主配置檔案

cp -r /usr/local/share/doc/dovecot/example-config/* /etc/dovecot/

將所有樣例配置檔案複製出來(注意一定要加-r引數)

5建立PAM系統使用者認證檔案

vim /etc/pam。d/dovecot 寫入以下內容

#%PAM-1。0

auth required pam_nologin。so

auth include system-auth

account include system-auth

session include system-auth

6配置POP3收信服務

vim etc/dovecot/dovecot。conf 配置引數只允許系統使用者認證的方式

!include conf。d/10-auth。conf 只是用系統使用者認證(預設存在,只需修改)

ssl = no 禁用ssl機制

disable_plaintext_auth = no 允許明文驗證

mail_location = maildir:~/Maildir 設定郵箱格式及位置

7啟動服務並確認監聽狀態

/etc/init。d/dovecot start

netstat -anpt | grep dovecot 檢視110和143埠

8POP3接收測試

telnet mail。benet。com 110

USER zhangsan 指定使用者名稱

+OK

PASS 123456 指定登入密碼

+OK Logged in。

List 檢視郵件列表

+OK 1 messages:

retr 1 讀取第一封郵件內容

Quit 退出

·SMTP發信認證

前面的配置可以讓任何人使用telnet用任意的郵箱給使用者發郵件,這是相當不安全的,所以可以使用SMTP發信認證來進行身份驗證,只有透過認證的使用者才能向外域名傳送郵件,否則只能在給本域的使用者傳送

1 SMTP使用者認證的實現

結合Cyrus SASL軟體來實現

2 設定Cyrus SASL函式庫,並啟動saslauthd服務

a建立SMTP認證配置檔案smtpd。conf

vim /usr/lib/sasl2/smtpd。conf

pwcheck_method: saslauthd 使用saslauthd服務認證

b啟動服務

/etc/init。d/saslauthd start

chkconfig ——level 35 saslauthd on 設定3、5級別自啟動

3新增SMTP認證支援,並重載服務

vim /etc/postfix/main。cf

smtpd_sasl_auth_enable=yes 啟用認證

smtpd_sasl_security_options=noanonymous 禁止匿名使用SMTP服務

mynetworks=127。0。0。1 啟用接收地址過濾

smtpd_recipient_restrictions= 收件人地址過濾規則

permit_mynetworks, 允許

permit_sasl_authenticated, 允許透過驗證的使用者向外傳送

reject_unauth_destination 禁止未授權的IP

4測試SMTP發信認證

A生成密碼加密字串(telnet登入時需要)

printf “lisi” | openssl base64

bGlzaQ==

printf “123456” | openssl base64

MTIzNDU2

B登入測試

telnet mail。benet。com 25

EHLO mail。benet。com 宣告客戶機地址

250-mazi。benet。com

250-PIPELINING

250-SIZE 10240000

250 DSN 表示支援認證

………………

auth login 告知要進行認證登入

334 VXNlcm5hbWU6

bGlzaQ== 使用者字串

334 UGFzc3dvcmQ6

MTIzNDU2 密碼字串

235 2。7。0 Authentication successful

MALL FROM:wangwu@benet。com 發件人地址

250 2。1。0 Ok

rcpt to:wangwu@benet。com 收件人地址

250 2。1。5 Ok

DATA 宣告要開始寫正文

你好

。 點來告知正文結束

quit

退出

三 構建web郵件系統

·web郵件系統介紹

Postfix支援使用的web郵件系統很多,如openwebmail、extmail、squirrelmail等。Squirrelmail官網

www。squirrelmail。org

·部署並配置squirrelmail

1部署

tar zxf squirrelmail-1。4。22。tar。gz -C /usr/local/httpd/htdocs/

tar zxf zh_CN-1。4。22-20110425。tar -C /usr/local/httpd/htdocs/squirrelmail/ 中文包

Mv /usr/local/httpd/htdocs/squirrelmail/ /usr/local/httpd/htdocs/webmail

2建立資料目錄,附件目錄(用於上傳附件的目錄)

mkdir -p date/ attach/ 在webmail目錄下建立

chown -R daemon:daemon date/ attach 設定屬主給予許可權

3建立主配置檔案並配置

cp config/config_default。php config/config。php 複製樣例配置檔案

vim config/config。php

$domain = ‘benet。com’;

$smtpServerAddress = ‘localhost’;

$smtpPort = 25;

$imap_server_type = ‘dovecot’;

$imapPort = 143;

$data_dir = ‘/usr/src/httpd/htdocs//webmail/data/’;

$attachment_dir = ‘/usr/src/httpd/htdocs/webmail/attach/’;

$squirrelmail_default_language = ‘zh_CN’;

$default_charset = ‘zh_CN。UTF-8’;

·訪問web郵件系統:

使用系統使用者登入即可