Skip to content

April 30, 2005

1

Simple mailserver HOWTO

This document describes how to set up a mail gateway using free and open Linux software. After completing this guide, you will have a fully functional mail server capable of SMTP connections.

Note!

This document has been replaced by the new Postfix HOWTO but it is still available if you only want a standard mail server without an SQL database or other more complicated things.

Preparation

This guide assumes that you have a recent version of Debian installed and running. How to install and setup Debian is out this documents scope, but as there are lots of on line documentation available you will hardly have any trouble installing it. The guide requires that a quite new version of the Postfix daemon is installed and if you use the sources.list-file provided there should not be any problems.

/etc/apt/sources.list:
deb security.debian.org stable/updates main contrib non-free
deb ftp.se.debian.org/debian/ stable main non-free contrib
deb www.backports.org/debian stable amavisd-new
deb www.backports.org/debian stable clamav
deb www.backports.org/debian stable spamassassin
deb www.backports.org/debian stable postfix

Postfix

Do you feel ready to install your future mail server? Well, I hope you do, because here we go! I am assuming that your Debian installation is up to date and that you have issued an apt-get dist-upgrade after adding the new entries to your sources.list file. You should also take a minute or two to skim through this document before attempting to install and configure Postfix as things that may seem confusing in the beginning clear up in the end. Installation and basic configuration To install Postfix in Debian just issue apt-get install postfix-tls and all dependencies should be resolved automatically.

debian:~# apt-get install postfix-tls
Reading Package Lists… Done
Building Dependency Tree… Done
The following extra packages will be installed:
postfix
The following packages will be REMOVED:
exim
The following NEW packages will be installed:
postfix postfix-tls
0 packages upgraded, 2 newly installed, 1 to remove and 0 not upgraded.
Need to get 905kB of archives. After unpacking 807kB will be used.
Do you want to continue? [Y/n]

Debian always comes with a default mail transfer agent, so Apt will automatically remove Exim which is the default MTA before installing Postfix. When the downloading is complete and the packages are unpacked, debconf will launch and ask you some questions about Postfix. The question “General type of configuration” should be answered with “No configuration”. When the questioning is over, you will find the Postfix configuration files in /etc/postfix/. Right now you have no main configuration in the configuration directory. To remedy that we simply create one! The file is called /etc/postfix/main.cf so open it in your favorite configuration editor, like vim if you are like me. Now let us set some paths so Postfix know where to find itself:

command_directory = /usr/sbin
daemon_directory = /usr/lib/postfix
program_directory = /usr/lib/postfix

Easy as pie, huh? Well those lines tell Postfix where all binaries are located and the above is the default for a Debian installation. Next we want to set a cool banner which people see when they try to login to your mail server:

smtpd_banner = $myhostname ESMTP ready

Cool enough? Well, feel free to customize but try not to, as this is the way it should be. You may leave your hostname out of the picture if you really want to. It is now time to setup a default behavior for the mailserver. Make sure you set myhostname to a fully qualified domain name(FQDN) like mail.example.com, and if you need to relay outgoing mail via your internet service providers MTA, please specify the server under relayhost. If you do not need to relay the mail just remove the line altogether and Postfix will deliver the mail directly:

setgid_group = postdrop
biff = no
append_dot_mydomain = no
myhostname = mail.example.org
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = localhost.localdomain, localhost
relayhost = smtprelay.example.com
mynetworks = 127.0.0.0/8
mailbox_command =
mailbox_size_limit = 0
recipient_delimiter = +
home_mailbox = Maildir/
mailbox_command = /usr/bin/maildrop

For a full specification of above configuration items, please refer to the Postfix reference manual or perhaps a computer literate person who happens to be a friend of yours. Now, to finish of the basic configuration we need to define access to the server. We obviously want to allow access from our permitted network, which should be defined as localhost(127.0.0.1/8) if you applied the configuration above. We also want to reject access to strangely formatted mails and other properties:

smtpd_recipient_restrictions =
permit_mynetworks,
reject_invalid_hostname,
reject_unknown_sender_domain,
reject_unknown_recipient_domain,
reject_unauth_pipelining,
reject_unauth_destination,
permit

Is should not be too hard to figure out what all above mean, but check out the manual if you are unsure. Just one more thing, as the above configuration use the maildrop binary from the Courier packages you need to install the package courier-maildrop by issuing apt-get install couriermaildrop on the command line. No questions should be asked, so please remain calm! Now please edit the file /etc/mailname and set your default sender domain as it would appear in an email address, for example example.org. You will also need to create an alias map for handling special email addresses such as postmaster and abuse. These two addresses should always be read by a real person! The file /etc/aliases is probably not empty but make it look like the following:

MAILER-DAEMON: postmaster
postmaster: root
abuse: root
root: yourSystemUsername

You should obviously replace yourSystemUsername with the user name you use in the Linux system. To make a database file of the alias file you are required to issue newaliases which will create the /etc/aliases.db file for you. To create a maildir in your home directory use apt-get to install the package called courier-base and login as your regular system user. In your home directory issue the command maildirmake Maildir. This will obviously create a directory in your home directory called Maildir, and if you recall, this directory was configured to be used in the main.cf file of Postfix. Now start Postfix with /etc/init.d/postfix start and do the following. The bold parts are user input, and replace yourSystemUsername with the appropriate username:

debian:~# telnet localhost 25
Trying 127.0.0.1…
Connected to debian.
Escape character is ‘^]’.
220 mail.example.org ESMTP Postfix
helo localhost
250 mail.example.org
mail from: root@localhost
250 Ok
rcpt to: yourSystemUsername@localhost
250 Ok
data
354 End data with .
This is a test
.

250 Ok: queued as 1774118097
quit
221 Bye
Connection closed by foreign host.

If you have a look at your system log file /var/log/syslog you should see something similar to the following. (Unnecessary text removed):

smtpd[474]: connect from debian[127.0.0.1]
smtpd[474]: 57A15180AC: client=debian[127.0.0.1]
cleanup[485]: 57A15180AC: messageid=<20040710223313.57A15180AC@mail.example.org>
qmgr[310]: 57A15180AC: from=[root @localhost], size=344, nrcpt=1 (queue active)
local[486]: 57A15180AC: to=[zeth @localhost], relay=local, delay=13, status=sent (delivered to command: /usr/bin/maildrop)
qmgr[310]: 57A15180AC: removed
smtpd[474]: disconnect from debian[127.0.0.1]

The key part is the status flag, and in this case it is sent which is very good. To verify that the mail actually has been delivered, login with your system username and check the directory ~/Maildir/new/. It should contain a file containing the email message. To view it, use cat file. If the file is there, then you have succeeded with the basic configuration of Postfix!

Configuring virtual hosts

Your mailserver is at this point able to send email from localhost and receive mail destined to localhost. You most certainly want to connect this server to a domain so that people located somewhere other than localhost can send you email. The approach to this is using virtual hosts, but if you have only one domain this could be unnecessary and then it should suffice putting your domain name in the mydestination configuration item in the main.cf file. If you do however, want to receive email for different domains and perhaps map them to different usernames, virtual hosts is for you! There really is not much to it, so we will begin right away by once again editing the main.cf file and adding the following to the bottom of the file:

virtual_maps =
hash:/etc/postfix/vhosts/domain1.com.virtual,
hash:/etc/postfix/vhosts/domain2.net.virtual,
hash:/etc/postfix/vhosts/domain3.org.virtual

Save and close the configuration file, and create the /etc/postfix/vhosts/ directory. In that file, create the files mentioned in the virtual_maps segment of the configuration file. The file format is as follows:

domain1.com virtual
root@domain1.com systemUser1
abuse@domain1.com systemUser1
postmaster@domain1.com systemUser1
public@domain1.com systemUser1
john@domain1.com john

It look somewhat similar to the /etc/aliases file created earlier, but do not mix them, as they are not exactly the same! Repeat this for all domains you want to define. When you are done, the files must be indexed and this is done with the postmap command. Just issue postmap domain1.com.virtual in the shell and repeat for all your domains. Now restart Postfix with postfix reload and try to send mail to one of the defined aliases. If the mail did not got through, check the syslog for a possible reason. Note that you may need a MX record in the domain for this to perform successfully.

Read more from Articles

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments