March 2, 2013, midnight

Setup a mail server with Ubuntu and Postfix

This article is about the minimum steps required to have a functional mail server using Ubuntu, Postfix, Dovecot and Postgrey. It does not cover spamassassin or webmail install.



Install the packages

The list of packages required are not so extensive:

 apt-get install libsasl2-2 libsasl2-modules sasl2-bin postfix dovecot-postfix postgray openssl

During the installation process, the package manager will ask for some input about the initial setup of postfix:

Edit the file /etc/hosts, and add the FQDN of your machine:

Setup the SASL authentication mechanism

Edit the file /etc/default/saslauthd and set it to start as a regular daemon:


Start the sasl daemon:

service saslauthd start

Create the file /etc/postfix/sasl/smtdp.conf with the following content:

pwcheck_method: saslauthd

Create a SSL/TLS certificate

Type the following to generate the certificate and fill the blanks like the screenshot:

openssl req -new -x509 -days 365 -nodes -out /etc/ssl/certs/ssl-mail.pem -keyout /etc/ssl/private/ssl-mail.key

365 is the number of days before you need to renew your certificate. If you want that your certificate last longer, fell free to change this value.

To setup dovecot edit the file /etc/dovecot/conf.d/01-mail-stack-delivery.conf

Disable the protocol pop3 and sieve:

- protocols = imap pop3 sieve
+ protocols = imap

Enable the maildir format with all the email centralized in /var/mail instead of being in the user home:

- mail_location = maildir:~/Maildir
+ mail_location = maildir:/var/mail/%u

Last you add the following content at the bottom of the file:

  service imap-login {
      inet_listener imap {

The result will look like this:

To setup postfix edit some lines in /etc/postfix/

Comment or remove the keyword home_mailbox

- home_mailbox = Maildir/

Set myhostname with the FQDN

+ myhostname =

Tell postfix where to save messages, it will be override by the dovecot configuration.

+ mail_spool_directory = /var/mail

You have to add the line checkpolicyservice inet: to the smtpdrecipientrestrictions keyword. This will make postgrey work with postfix

smtpd_recipient_restrictions = ...,
      check_policy_service inet:

For clarity I removed all commented text from my file. As you can see most of the config was untouched, the final result looks like this:

DNS Requirements

comments powered by Disqus