Rent my time and knowledge so I can help with your project!


Ricardo Pascal

Usually a Python programmer, sometimes an Angular programmer, a physics dropou planning a triumphal come back, currently working as bioinformatician and team leader in a biotech company in south Brazil.

Enjoy expose/consume data from rest API's and teach/evangelize python newcomers.

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.


  • The ip address used is
  • The machine hostname is hercules
  • The domain is
  • You have to change those values to reflect your setup


  • Ubuntu 12.10, but any recent debian based distro should work without much modification.
  • A VPS or a physical server
  • You must be able to setup the dns using the tools provided by your host provider

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 check_policy_service inet: to the smtpd_recipient_restrictions 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

  • mx record
  • a record for
  • spf record
  • reverse entry

by Ricardo Pascal on March 2, 2013

comments powered by Disqus