郵件服務器

郵件服務器

 

郵件服務器是大型公司必備的一項,因爲公司內部都需要依靠郵件來傳輸信息,郵件一般都採用POP3和SMTP服務,搭建服務的程序有很多,比較出名的微軟,IMB都有自己的郵件服務軟件,我們這裏是在LINUX下建立郵件服務器,使用的是開源的postfix和dovecot分別來做爲發郵件和收郵件的服務。

郵件服務器需要域名的解析所以我們需要DNS服務,然後配置兩個服務就可以通過郵件管理程序(MUT)來進行收發郵件了,如果需要也可以安裝webmail系統,實現網頁的mail訪問,下面以squirrelmail爲例,

實驗拓撲:

                        DNS Server             主機A

                    Mail Server             主機B

                        Win7 Client              主機W

 

實驗要求:

    在DNS Master上搭建DNS,能夠解析mail.tarena.com

    在Mail Server上部署郵件服務器

    在Win7上安裝Foxmail測試

 

 主機A的配置(配置DNS)

前面有文件專門說明DNS,這裏只寫入了需要添加的,來指定mail服務器的域名解析

 

[root@server1 named]# cat tarena.com.zone

$TTL    86400

@               IN SOA  tarena.com.      root.tarena.com. (

                                        2013122401  ; serial (d. adams)

                                        3H          ; refresh

                                        15M         ; retry

                                        1W          ; expiry

                                        1D )        ; minimum

        IN      NS        dns1.tarena.com.        

        IN      MX   5   mail.tarena.com.                //郵件服務器的域名

dns1    IN      A       192.168.10.253

mail     IN      A       192.168.10.252                                          //解析郵件服務器的域名

 

Mail Server配置(配置SMTP服務器)

1、前提條件(測試DNS)

[root@mail ~]# host -t mx tarena.com

tarena.com mail is handled by 5 mail.tarena.com.

[root@mail ~]# host mail.tarena.com

mail.tarena.com has address 192.168.10.252

2、設置郵件服務器的主機名

[root@mail ~]# grep HOSTNAME /etc/sysconfig/network

HOSTNAME=mail.tarena.com

[root@mail ~]# hostname mail.tarena.com

3、安裝Postfix

postfix的端口是25,有可能會被sendmail佔用,這時我們需要先關閉sendmail服務,可以先查看一下,

如果沒有可跳過

 

[root@mail ~]# netstat -tulnp | grep :25                                               //查看25端口

tcp    0   0 127.0.0.1:25   0.0.0.0:*   LISTEN    4079/sendmail 

[root@mail ~]# service sendmail stop                                                 //關閉服務

[root@mail ~]# chkconfig sendmail off

[root@mail ~]# yum -y install postfix

[root@mail ~]# chkconfig --add postfix

[root@mail ~]# chkconfig --list postfix

postfix    0:關閉  1:關閉  2:啓用  3:啓用  4:啓用  5:啓用  6:關閉

4、修改主配置文件

postfix的主配置文件內容很多,我們可以使用postconf -n命令導出非默認的設置,用此文件來替換原配置文件,這樣配置文件就從600多行變成30多行了,方便我們查看更改。保留原文件做爲備份

[root@mail ~]# cd /etc/postfix/

[root@mail postfix]# postconf -n >tmp.txt

[root@mail postfix]# mv main.cf main.cf.bak

[root@mail postfix]# mv tmp.txt main.cf

 

[root@mail postfix]# vim main.cf

...

  8 #inet_interfaces = localhost            //監聽端口

 20 myhostnasme = mail.tarena.com     //郵件服務器主機名

 21 mydomain = tarena.com                //郵件服務器所在區域

 22 myorigin = $mydomain                 //發件人DNS後綴           

 23 mydestination = $mydomain         //指定Postfix允許處理的郵件

 24 home_mailbox = Maildir/          //郵箱類型

 25 mynetworks = 192.168.10.0/24      //設置允許哪些客戶端直接將需要轉發到外部區域的郵件提交給Postfix

4、檢查語法啓動服務

 

[root@mail postfix]# postfix check

[root@mail postfix]# postfix reload

[root@mail postfix]# netstat -tulnp | grep :25

tcp   0   0 0.0.0.0:25   0.0.0.0:*       LISTEN     6015/master   

測試:

先建立兩個賬號,用來郵件收發的測試

[root@mail ~]# useradd yg

[root@mail ~]# useradd xln

[root@mail ~]# echo 123456 | passwd --stdin yg

[root@mail ~]# echo 123456 | passwd --stdin xln

進入測試

[root@mail ~]# telnet mail.tarena.com 25                                        //連接服務器

Trying 192.168.10.10...

Connected to mail.tarena.com (192.168.10.10).

Escape character is '^]'.

220 mail.tarena.com ESMTP Postfix

helo localhost                                             //宣告客戶端

250 mail.tarena.com

mail from:[email protected]                                     //郵件來自

250 2.1.0 Ok

rcpt to:[email protected]                                                         //郵件發往

250 2.1.5 Ok

data                                                       //郵件正文

354 End data with <CR><LF>.<CR><LF>

subject Test mail!                                         //郵件主題

hello,byebye           

.                                                    //郵件結束

250 2.0.0 Ok: queued as A967D324DE8

quit                                                         //退出

221 2.0.0 Bye

Connection closed by foreign host.

查看Mail的日誌,

[root@mail ~]# tail /var/log/maillog 

...

Dec 24 11:49:27 ser1 postfix/smtpd[14064]: connect from ser1.tarena.com[192.168.10.10]

Dec 24 11:50:20 ser1 postfix/smtpd[14064]: A967D324DE8: client=ser1.tarena.com[192.168.10.10]

Dec 24 11:51:09 ser1 postfix/cleanup[14083]: A967D324DE8: message-id=<[email protected]>

Dec 24 11:51:09 ser1 postfix/qmgr[13913]: A967D324DE8: from=<[email protected]>, size=367, nrcpt=1 (queue active)                                     

                                                   //上面是發件人,下面是收件人

Dec 24 11:51:09 ser1 postfix/local[14099]: A967D324DE8: to=<[email protected]>, relay=local, delay=63, delays=63/0.01/0/0.05, dsn=2.0.0, status=sent(delivered to maildir)

// status=sent表示發送成功

Dec 24 11:51:09 ser1 postfix/qmgr[13913]: A967D324DE8: removed

Dec 24 11:51:14 ser1 postfix/smtpd[14064]: disconnect from ser1.tarena.com[192.168.10.10]

[root@mail ~]# ls ~xln/Maildir/new/

1387857069.V802I3ec114M561364.mail.tarena.com

//下面是查看剛剛發送的郵件

[root@mail ~]# cat ~xln/Maildir/new/1387857069.V802I3ec114M561364.mail.tarena.com

Return-Path: <[email protected]>

X-Original-To: [email protected]

Delivered-To: [email protected]

Received: from localhost (ser1.tarena.com [192.168.10.10])

        by mail.tarena.com (Postfix) with SMTP id A967D324DE8

        for <[email protected]>; Tue, 24 Dec 2013 11:50:06 +0800 (CST)

Message-Id: <[email protected]>

Date: Tue, 24 Dec 2013 11:50:06 +0800 (CST)

From: [email protected]

To: undisclosed-recipients:;

 

subject Test mail!

hello,byebye

 

 

Mail Server配置(配置POP服務器)

1、安裝dovecot

dovecot默認配置就可以使用,安裝完後啓動服務,就可以使用了

[root@mail ~]# yum -y install dovecot

2、配置主配置文件

[root@mail ~]# vim /etc/dovecot.conf

...

205    mail_location = maildir:~/Maildir                     //設置郵箱路徑

3、啓動服務

[root@mail ~]# service dovecot restart

[root@mail ~]# chkconfig dovecot on

[root@mail ~]# netstat -tulnp | grep dovecot

tcp   0      0 :::110      :::*        LISTEN      16835/dovecot  

tcp   0      0 :::143      :::*        LISTEN      16835/dovecot 

 

 

 

測試:

 

[root@mail ~]# telnet mail.tarena.com 110               //連接服務器

Trying 192.168.10.10...

Connected to mail.tarena.com (192.168.10.10).

Escape character is '^]'.

+OK Dovecot ready.

user xln                                                   //輸入賬戶

+OK

pass 123456                                               //輸入密碼

+OK Logged in.

list                                                     //列出郵件

+OK 1 messages:

1 458

.

retr 1                                                 //查看郵件1

+OK 458 octets

Return-Path: <[email protected]>

X-Original-To: [email protected]

Delivered-To: [email protected]

Received: from localhost (ser1.tarena.com [192.168.10.10])

        by mail.tarena.com (Postfix) with SMTP id A967D324DE8

      for <[email protected]>; Tue, 24 Dec 2013 11:50:06 +0800 (CST)

Message-Id: <[email protected]>

Date: Tue, 24 Dec 2013 11:50:06 +0800 (CST)

From: [email protected]

To: undisclosed-recipients:;

 

subject Test mail!

hello,bybye

.

quit

+OK Logging out.

Connection closed by foreign host.

 

SMTP認證控制

1、安裝,啓動saslauthd服務

[root@mail ~]# rpm -q cyrus-sasl

cyrus-sasl-2.1.22-7.el5_8.1

[root@mail ~]# cat /etc/sasl2/smtpd.conf

pwcheck_method: saslauthd

[root@mail ~]# service saslauthd start

[root@mail ~]# chkconfig saslauthd on

[root@mail ~]# testsaslauthd -u yg -p 123456 -s smtp    //檢查saslauthd服務

0: OK "Success."

 

2、調整postfix配置,啓用認證

[root@localhost ~]# vim /etc/postfix/main.cf

...

 25 mynetworks = 127.0.0.1                       //設置本地網絡

 26 smtpd_sasl_auth_enable = yes                  //啓用SASL認證

 27 smtpd_sasl_security_options = noanonymous   //阻止匿名發信

 28 smtpd_recipient_restrictions =              //設置收件人過濾

 29  permit_mynetworks,                         //允許來自mynetworks的客戶

 30  permit_sasl_authenticated,             //允許已通過sasl認證的用戶

 31  reject_unauth_destination                 //拒絕向未授權的目標域發信

[root@localhost ~]# service postfix restart

 

3、測試

通過下面的命令算認證,下面測試會用到

[root@mail ~]# printf "yg"| openssl base64

eWc=

[root@mail ~]# printf "123456"| openssl base64

MTIzNDU2

 

[root@mail ~]# telnet mail.tarena.com 25              //連接服務器

 

Trying 192.168.10.10...

Connected to mail.tarena.com (192.168.10.10).

Escape character is '^]'.

220 mail.tarena.com ESMTP Postfix

mail from:[email protected]

250 2.1.0 Ok

rcpt to:[email protected]

554 5.7.1 <[email protected]>: Relay access denied

quit

221 2.0.0 Bye

Connection closed by foreign host.

 

 

[root@mail ~]# telnet mail.tarena.com 25             //連接服務器

telnet mail.tarena.com 25

Trying 192.168.10.10...

Connected to mail.tarena.com (192.168.10.10).

Escape character is '^]'.

220 mail.tarena.com ESMTP Postfix

helo localhost

250 mail.tarena.com

auth login

334 VXNlcm5hbWU6

eWc=

334 UGFzc3dvcmQ6

MTIzNDU2

235 2.0.0 Authentication successful

mail from:[email protected] 

250 2.1.0 Ok

rcpt to:[email protected]

250 2.1.5 Ok

quit

221 2.0.0 Bye

Connection closed by foreign host.

 

 

 

郵件過濾

1、根據客戶端地址過濾

 

[root@mail ~]# tail -n 2 /etc/postfix/access

192.168.10.53   REJECT

192.168.10.50   OK

[root@mail ~]# postmap /etc/postfix/access

[root@mail ~]# vim /etc/postfix/main.cf

...

 32 smtpd_client_restrictions = check_client_access hash:/etc/postfix/access

[root@localhost ~]# postfix reload

做完這個實驗請將main.cf 32行註釋

 

2、根據發信人地址過濾

34到38行前面是有一個空格的,表是接上一行的內容,可以把他們當成是一行,因爲一行寫不下才這樣寫的。

[root@mail ~]# cat /etc/postfix/sender_access

[email protected]   REJECT

[root@mail ~]# postmap /etc/postfix/sender_access

[root@mail ~]# vim /etc/postfix/main.cf

...

 33 smtpd_sender_restrictions =

 34  permit_mynetworks,                      //若從mynetworks網絡訪問則允許

 35  reject_sender_login_mismatch,            //發件人與登錄信息不符時拒絕

 36  reject_non_fqdn_sender,                    //拒絕不完整的發件域

 37  reject_unknown_sender_domain,               //拒絕未知的收件域

 38  check_sender_access hash:/etc/postfix/sender_access   //指定策略庫

[root@localhost ~]# service postfix restart

做完這個實驗請將main.cf 33-38行註釋

 

 

 

搭建Webmail系統

需要HTTPD服務,只要啓動就可以。

1、安裝squirrelmail

[root@mail ~]# yum -y install squirrelmail

2、配置squirrelmail

[root@mail ~]# vim /etc/squirrelmail/config.php

...

 26 $squirrelmail_default_language = 'zh_CN';          //默認語言改成中文

 28 $domain                 = 'tarena.com';         //域名

 29 $imapServerAddress      = '192.168.10.10';       //收郵件地址

 32 $smtpServerAddress      = '192.168.10.10';       //發郵件地址

3、啓動httpd服務

[root@mail ~]# service httpd restart

[root@mail ~]# chkconfig httpd on

測試:

在流覽器上輸入下面的地址,試試自已搭建的網頁郵箱吧

http://mail.tarena.com/webmail

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章