ubuntu 下postfix邮件服务器的搭建


Postfix 基本设置指南

 
[编辑] 介绍

这份指南将帮助你建立一个基本的企业级Postfix邮件服务器。主要目的是教会你如何安装和配置一个包含IMAP和POP3服务的基本Postfix邮件服务器。在此我不打算告诉你诸如病毒检测或垃圾过滤等高级应用。

这份指南较偏向于理论,适合postfix初学者。我的高级postfix指南是Postfix 虚拟邮箱和病毒过滤指南Postfix 完整虚拟邮件系统指南。如果你是初学者,可以在看完这份基本指南后,查阅他们。

[编辑]安装概述

安装配置过程中,postfix通过Internet发送和接收emial,并存储在用户邮箱里。同时客户也可以通过IMAP或者POP3恢复他们的邮件。其中的客户认证是通过Courier Authdaemon模块的。下图表示出了这个过程。attachment:SetupOverview.jpg

粗体文字=== Postfix的结构 ===

[编辑]组件

下图表示Postfix系统的主要组件以及他们间的主要信息。

attachment:PostfixComponents.gif

  • 黄色椭圆表示邮件程序。
  • 黄色方框表示邮件队列或文件。
  • 蓝色方框表示查找的表。
  • 大方框中的程序在Postfix主模块的控制下运行。
  • 大方框中的数据记录Postfix系统的属性。
[编辑]接收邮件

当一个新的消息进入Postfix系统,第一个内部停止是在新进入队列(incoming queue)。下图表示了包含一个新邮件的主要组件。

File:Attachment:PosfixRecieving.gif

  • 邮件在本地发送。 Postfix的sendmail程序调用拥有特权的postdrop程序 which deposits the message into the maildrop directory, where the message is picked up by thepickup daemon. This daemon does some sanity checks, in order to protect the rest of the Postfix system.

该程序将消息存放到 maildrop 目录,在那里消息被 pickup 守护进程取走。pickup 守护进程为了保护其他的 Postfix 系统会执行一些完整性检测。

  • Mail comes in via the network. The Postfix SMTP server receives the message and does some sanity checks, in order to protect the rest of the Postfix system.
  • 邮件从网络到来. Postfix 的SMTP server收到消息然后为了保护其他的 Postfix 系统作些完整性检测。
  • Mail is generated internally by the Postfix system itself, in order to return undeliverable mail to the sender. Thebounce or defer daemon brings the bad news.

  • Mail is forwarded by the local delivery agent, either via an entry in the system-wide alias database, or via an entry in a per-user .forward file. This is indicated with the unlabeled arrow.
  • Mail is generated internally by the Postfix system itself, in order to notify the postmaster of a problem (this path is also indicated with the unlabeled arrow).The Postfix system can be configured to notify the postmaster of SMTP protocol problems,UCE policy violations, and so on.
  • The cleanup daemon implements the final processing stage for new mail. It adds missingFrom: and other message headers, arranges for address rewriting to the standard user@fully.qualified.domain form, and optionally extracts recipient addresses from message headers. Thecleanup daemon inserts the result as a single queue file into theincoming queue, and notifies thequeue manager of the arrival of new mail. The cleanup daemon can be configured to transform addresses on the basis ofcanonical and virtua table lookups.
  • On request by the cleanup daemon, the trivial-rewrite daemon rewrites addresses to the standard user@fully.qualified.domain form.

[编辑]安装Postfix

在安装过程中我假设你的域名是 yourdomain.com 它有一个有效的邮件交换记录 mail.yourdomain.com. 记得用你自己的真实域yourdomain.com 来替换这份指南中的示例. 我假设你也同样清楚什么是邮件交换记录. 你可以在终端键入下述代码用以查看Mail Exchanger:

dig mx yourdomain.com

安装postfix

   
sudo apt-get install postfix

安装mailx软件包mailx软件包是一个命令行的邮件属性程序,mail命令包含在mailx软件包里面


sudo apt-get install mailutils  // mailx 不存在

[编辑]测试你的默认设置

首先添加一个用户. (这里以fmaster为例)

sudo useradd -m -s /bin/bash fmaster
sudo passwd fmaster

用下面的命令测试,其实就是测试25端口是否打开

telnet localhost 25

Postfix将在终端中显示如下提示,这样你就可以用来键入SMTP命令.

Trying 127.0.0.1...
Connected to mail.fossedu.org.
Escape character is '^]'.
220 localhost.localdomain ESMTP Postfix (Ubuntu)


用下面的命令测试postfix

ehlo localhost
mail from: root@localhost
rcpt to: fmaster@localhost
data
Subject: My first mail on Postfix
Hi,
Are you there?
regards,
Admin
. (Type the .[dot] in a new Line and press Enter )
quit 

检查刚才创建的fmaster用户的收件箱

su - fmaster
mail

当你键入 mail 命令时,你的终端中将显示如下信息.

Mail version 8.1.2 01/15/2001.  Type ? for help.
"/var/mail/fmaster": 2 messages 2 new
>N  1 root@localhost     Mon Mar  6 12:49   13/479   Just a test
N  2 root@localhost     Mon Mar  6 12:51   15/487   My first mail
&

你会发现邮件将用数字进行索引,你可以通过键入邮件相对应的数字来打开你想阅读的邮件。 比如键入 "2" 就可以打开阅读第二封邮件. 键入"q"可以退出 邮件将会以mbox 文件保存在用户的个人目录. 根据我们的示例,这个目录为/home/fmaster/mbox.

All messages in an mbox type of mailbox are concatenated and stored in a single file. The beginning of each message is indicated by a line whose first five characters are "From " and a blank line is appended to the end of each message

[编辑]设置Postfix以支持 Maildir风格的邮箱

Maildir is a format for an e-mail spool that does not require file locking to maintain message integrity because the messages are kept in separate files with unique names. A Maildir is a directory (often named Maildir) with three subdirectories named tmp, new, and cur. The subdirectories should all reside on the same filesystem.

Another reason to use Maildir format is that Courier IMAP/POP3 servers only work with Maildir format of mailboxes.

Please find out more about Maildir here


sudo  vi /etc/postfix/main.cf

Add the following code segment:

home_mailbox = Maildir/

Comment the Line mailbox_command = procmail -a "$EXTENSION" adding a "#" at the beginning

Restart Postfix to make changes effect.

sudo  /etc/init.d/postfix restart

Test your setup again

[编辑]Installing courier IMAP and POP3

sudo apt-get install courier-pop
sudo apt-get install courier-imap

[编辑]Adding local domains to postfix

sudo   vi /etc/postfix/main.cf

Add your domain to:

sudo vi /etc/postfix/main.cf

Add your domain to mydestination. Once added it should be like the following code segment.

...
mydestination = mail.fossedu.org, localhost.localdomain, localhost, yourdoamin.com
...

Add your local network to:

sudo vi /etc/postfix/main.cf

I assume that your local network is 192.168.1.0/24 and add your local network tomynetworks. Once added it should be like the following code segment.

mynetworks = 127.0.0.0/8, 192.168.1.0/24

Make Postfix to receive mail from the Internet

Set inet_interfaces = all in /etc/postfix/main.cf using:

sudo vi /etc/postfix/main.cf

Finally Restart Postfix;

sudo  /etc/init.d/postfix restart

Test your setup again using following code:

telnet mail.yourdomain.com 25
ehlo yourdmain.com
mail from: root@yourdomain.com
rcpt to: fmaster@yourdomain.com
data
Subject: My first mail for my domain
Hi,
Are you there?
regards,
Admin
. (and Enter In a new Line)
quit 

Check the mailbox of fmaster

su - fmaster
cd Maildir/new
ls

Now you will see mail has a separate file

[编辑]Testing Courier POP3

在终端输入:

telnet mail.yourdomain.com 110

用下面例子的代码做测试. 您在测试前请修改下面的代码以合适你的运行环境. 之后将在终端输出如下内容

Connected to mail.yourdomain.com (69.60.109.217).
Escape character is '^]'.
+OK Hello there.

紧接着为pop3服务器输入如下内容.输入的内容为 +OK上面的部分(即:user 用户名;pass 密码)

user fmaster
+OK Password required.
pass password
+OK logged in.
quit

[编辑]Testing Courier IMAP

Type in a terminal:

telnet mail.yourdomain.com 143

Use the following example code segment for your test. Be intelligent to tweak the changes appropriately to your environment. An output like follows will display in your terminal.

* OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA IDLE ACL ACL2=UNION STARTTLS XCOURIEROUTBOX=INBOX.Outbox] Courier-IMAP ready. Copyright 1998-2005 Double Precision, Inc.  See COPYING for distribution information.

Type the following code segment in the prompt provided by the Courier IMAP server.

a login fmaster password
a OK LOGIN Ok.
a logout

[编辑]Local Alias database

When mail is to be delivered locally, the local delivery agent runs each local recipient name through the aliases database. The mapping does not affect addresses in message headers. Local aliases are typically used to implement distribution lists, or to direct mail for standard aliases such as postmaster to real people. The table can also be used to map Firstname.Lastname addresses to login names.

Alias lookups are enabled by default and you will see following code segment inmain.cf file.

...
alias_maps = hash:/etc/aliases 
...
[编辑]Creating an alias for an account

The following codes illustrate how you can setup an alias. This step is optional since we are going to configure virtual mail domains later in this howto. I have added this step to make understand how you can do this in case of a requirement.

Create a user

sudo useradd -m -s /bin/bash sysadmin
sudo passwd sysadmin

Edit the alias table

Open the alias file with:

sudo vi /etc/aliases

Add the following code:

fmaster: sysadmin

To make your changes effect type:

sudo newaliases

To test your changes send a mail to fmaster and check the mail in/home/sysadmin/Maildir/new folder.


[编辑]Per User .forward Files

Users can control their own mail delivery by specifying destinations in a file called .forward in their home directories. The syntax of these files is the same as with system aliases, except that the lookup key and colon are not present.

I will illustrate an example here:

Assume that you need to for all the mails which comes sysadmin account to an another account do like this

su - sysadmin
touch .forward

Then open the .forward file

vi .forward

Add the following code:

fossedu@gmail.com

Remember to use email address which exists in this exercise.

Now send a mail to sysadmin and mail should come to fossedu@gmail.com

[编辑]Postfix virtual Aliases for separate domains and Linux system accounts

With this approach, every hosted domain can have its own info etc. email address. However, it still uses LINUX system accounts for local mailbox deliveries.

With virtual alias domains, each hosted address is aliased to a local UNIX system account or to a remote address. The example below shows how to use this mechanism for the fossedu.org and linuxelabs.com domains.

Open the main.cf file:

sudo vi /etc/postfix/main.cf

Add the following code segment:

virtual_alias_domains = fossedu.org linuxelabs.com
virtual_alias_maps = hash:/etc/postfix/virtual

Edit the /etc/postfix/virtual file:


Add two Linux system accounts

sudo useradd -m -s /bin/bash sigiri
sudo useradd -m -s /bin/bash kala

Set Password for the above users.

sudo passwd sigiri
sudo passwd kala


sudo vi /etc/postfix/virtual

Add the following code segment:

info@fossedu.org       sigiri
info@linuxelabs.com    kala

To create a Map Database type :

sudo postmap /etc/postfix/virtual

The postmap is utility program that will convert /etc/postfix/virtual to/etc/postfix/virtual.db Berkley DB format, so that Postfix can access the data faster.

Restart Postfix to make changes effect:

sudo /etc/init.d/postfix restart

Send mails to both info@fossedu.org and info@linuxelabs.com and those mails should come to mailboxes ofsigiri andkala respectively.


Yet another howto by: ChinthakaDeshapriya.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答关于UbuntuPostfix +Dovecot邮件服务器搭建的问题。以下是大致的步骤: 1. 安装Postfix和Dovecot 在Ubuntu终端输入以下命令安装Postfix和Dovecot: ``` sudo apt-get update sudo apt-get install postfix dovecot-core dovecot-imapd dovecot-pop3d ``` 2. 配置Postfix 在终端中输入以下命令打开Postfix的主配置文件: ``` sudo nano /etc/postfix/main.cf ``` 在文件中搜索以下两行内容并进行修改: ``` myhostname = your_domain_name mydestination = your_domain_name, localhost.localdomain, localhost ``` 将“your_domain_name”替换为您的域名。 接下来,找到以下行并更改以允许邮件的发送和接: ``` inet_interfaces = all ``` 最后,添加以下内容使Postfix能够与Dovecot一起工作: ``` home_mailbox = Maildir/ mailbox_command = ``` 保存并关闭文件。 3. 配置Dovecot 打开Dovecot的主配置文件: ``` sudo nano /etc/dovecot/dovecot.conf ``` 找到以下行并进行修改: ``` protocols = imap pop3 ``` 将其更改为: ``` protocols = imap pop3 lmtp ``` 然后,找到以下行并进行修改: ``` mail_location = mbox:~/mail:INBOX=/var/mail/%u ``` 将其更改为: ``` mail_location = maildir:~/Maildir ``` 保存并关闭文件。 4. 配置MySQL 如果您想要使用MySQL存储邮件,可以按照以下步骤进行配置: - 安装MySQL: ``` sudo apt-get install mysql-server ``` - 创建数据库和用户: ``` sudo mysql -u root -p ``` ``` CREATE DATABASE mailserver; CREATE USER 'mailuser'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON mailserver.* TO 'mailuser'@'localhost'; FLUSH PRIVILEGES; ``` - 创建表格: ``` sudo mysql -u mailuser -p mailserver < /etc/postfix/mysql_virtual_mailbox_domains.sql sudo mysql -u mailuser -p mailserver < /etc/postfix/mysql_virtual_mailbox_maps.sql sudo mysql -u mailuser -p mailserver < /etc/postfix/mysql_virtual_alias_maps.sql ``` 5. 重启服务 在终端中输入以下命令以重启Postfix和Dovecot: ``` sudo systemctl restart postfix sudo systemctl restart dovecot ``` 完成以上步骤后,您的Ubuntu邮件服务器将已经搭建完成。您可以测试是否正常工作,例如发送和接一封电子邮件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值