Skip to content
Jon's View

Jon's View

a technology blog by Jon Stacey

Menu
  • Contact
Menu

How To: Setup Email Services on Ubuntu Using Postfix (TLS+SASL) and Dovecot

Posted on March 26, 2009July 5, 2016 by Jon Stacey

Here is a guide on getting Email services running on Ubuntu Intrepid. I used Postfix for core services (SMTP wtih TLS and SASL) and Dovecot for fast IMAP and POP3. This tutorial has been tested on a bare bones Ubuntu 8.10 slice from Slicehost.

Preamble

Estimated Time Required: 10-15 minutes

This guide makes several assumptions. For example, it assumes that you will use Maildir. If you decide to make any changes, just keep an eye out for any subsequent changes that might be needed down the line. If you are upgrading from another system, such as Courier, please look at Appendix A.

If you prefer Postfix and Courier, refer to my older tutorial which is reported to work with Ubuntu Intrepid.

Postfix

Let’s get core email functionality going with Postfix:

aptitude install postfix sasl2-bin

You will be asked a few questions with a nice graphical interface. Here are the answers for some of them. Replace all occurrences of example.com with your root FQDN (e.g. jonsview.com), and server1.example.com with your server’s FQDN (e.g. swift.jonsview.com).

* General type of mail configuration? Internet Site
* System mail name? server1.example.com

Unfortunately, the graphical configuration interface that was automatically launched was a condensed version. You will need to run the full graphical configuration utility.

dpkg-reconfigure postfix

Again, you will be asked some questions:

* General type of mail configuration? Internet Site
* System mail name? server1.example.com
* Root and postmaster mail recipient? Leave blank
* Other destinations to accept mail for? server1.example.com, example.com, localhost.example.com, localhost
* Force synchronous updates on mail queue? No
* Local networks? Leave default (127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128)
* Mailbox size limit (bytes)? 0
* Local address extension character? Leave default (+)
* Internet protocols to use? ipv4 (most likely)

Next, let’s take care of certificates for TLS. You will be asked several questions during this process. Fill them in as you see fit.

mkdir /etc/postfix/ssl
cd /etc/postfix/ssl/
openssl genrsa -des3 -rand /etc/hosts -out smtpd.key 1024
chmod 600 smtpd.key
openssl req -new -key smtpd.key -out smtpd.csr
openssl x509 -req -days 3650 -in smtpd.csr -signkey smtpd.key -out smtpd.crt
openssl rsa -in smtpd.key -out smtpd.key.unencrypted
mv -f smtpd.key.unencrypted smtpd.key
openssl req -new -x509 -extensions v3_ca -keyout cakey.pem -out cacert.pem -days 3650

Finish configuring Postfix for TLS and SASL.

postconf -e 'smtpd_sasl_local_domain ='
postconf -e 'smtpd_sasl_auth_enable = yes'
postconf -e 'smtpd_sasl_security_options = noanonymous'
postconf -e 'broken_sasl_auth_clients = yes'
postconf -e 'smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination'
postconf -e 'inet_interfaces = all'
echo 'pwcheck_method: saslauthd' >> /etc/postfix/sasl/smtpd.conf
echo 'mech_list: plain login' >> /etc/postfix/sasl/smtpd.conf
postconf -e 'smtpd_tls_auth_only = no'
postconf -e 'smtp_use_tls = yes'
postconf -e 'smtpd_use_tls = yes'
postconf -e 'smtp_tls_note_starttls_offer = yes'
postconf -e 'smtpd_tls_key_file = /etc/postfix/ssl/smtpd.key'
postconf -e 'smtpd_tls_cert_file = /etc/postfix/ssl/smtpd.crt'
postconf -e 'smtpd_tls_CAfile = /etc/postfix/ssl/cacert.pem'
postconf -e 'smtpd_tls_loglevel = 1'
postconf -e 'smtpd_tls_received_header = yes'
postconf -e 'smtpd_tls_session_cache_timeout = 3600s'
postconf -e 'tls_random_source = dev:/dev/urandom'
postconf -e 'myhostname = server1.example.com'
postconf -e 'home_mailbox = Maildir/'
postconf -e 'mailbox_command ='

Finally, restart Postfix

/etc/init.d/postfix restart

SASL

Authentication will be done by saslauthd which will need to be configured to support a chrooted Postfix setup. Edit /etc/default/saslauthd and add or change the following settings so that they match:

START=yes
OPTIONS="-c -m /var/spool/postfix/var/run/saslauthd"

Finish up SASL by creating the chroot directory, adding the postfix user to the sasl group, and then starting saslauthd.

mkdir -p /var/spool/postfix/var/run/saslauthd
dpkg-statoverride --add root sasl 710 /var/spool/postfix/var/run/saslauthd
adduser postfix sasl
/etc/init.d/saslauthd start

Testing

At this point, core email services should be up and running. Let’s make sure that you’re in good shape before moving on. First, establish a connection with the mail server.

telnet localhost 25

After establsihing a connection with the Postfix service, run:

ehlo localhost

You should see a few lines of output. Make sure that the two most important lines are there:

. . .
250-STARTTLS
250-AUTH PLAIN LOGIN
. . .

Type quit to get out.

Dovecot

Note: If you followed my last guide and are migrating from Courier, please see Appendix A before continuing.

Install and configure Dovecot.

aptitude install dovecot-imapd dovecot-pop3d
perl -pi -e 's/#mail_location =/mail_location = maildir:\/home\/\%u\/Maildir/' /etc/dovecot/dovecot.conf
/etc/init.d/dovecot restart

If everything went smoothly you should now be in email nirvana. Each user has their own email account and you can move on to virtual accounts if you desire.

Appendix A: Courier to Dovecot Conversion

Please refer to this Dovecot wiki article for detailed information, but in a nutshell:

wget http://www.dovecot.org/tools/courier-dovecot-migrate.pl
chmod 755 courier-dovecot-migrate.pl
./courier-dovecot-migrate.pl --to-dovecot --recursive /home

If everything looks good, then perform the actual conversion.

Note: Even if 0 mailbox changes are shown, the script may still be working. If there aren’t any explicit errors, run the conversion and then check the Maildirs for dovecot indexes.

./courier-dovecot-migrate.pl --to-dovecot --convert --recursive /home

For a transparent conversion you will need to setup Dovecot to use INBOX as the namespace for private mailboxes. Edit /etc/dovecot/dovecot.conf and uncomment the namespace private { block (and corresponding } ). Uncomment #prefix = and change to prefix = INBOX. (include the period). Finally, change #inbox = no to inbox = yes. In essence, it should look like the following, which has been stripped of comments for brevity.

namespace private {
prefix = INBOX.
inbox = yes
}

Appendix B: SMTP Troubleshooting

If core email services and IMAP are working, but not SMTP, then it’s most likely that sasl is not set up properly. Log entries like the following in /var/log/mail.warn will confirm this:

Mar 27 00:36:56 swift postfix/smtpd[12537]: warning: SASL authentication failure: cannot connect to saslauthd server: No such file or directory

Last words

I’ve always found setting up email services on Linux a pain, so hopefully this has helped you get up and running. Please let me know if you find any errors, or have suggestions that would make this guide easier to understand.

81 thoughts on “How To: Setup Email Services on Ubuntu Using Postfix (TLS+SASL) and Dovecot”

Comments navigation

Older comments
Newer comments
  1. kaaoua says:
    November 2, 2013 at 4:13 am

    this work fine, Thank you so much

    Reply
  2. Bogdan says:
    October 16, 2013 at 1:35 pm

    Thank you so much for this tutorial!

    I’ve got both an an MTA and a MDA running perfectly, using secure ports on my LAN and later on WAN. 🙂

    I was able to use port 993 (secure IMAP) right from the start.

    For secure smtp (port 465), I’ve only commented out a few lines in /etc/postfix/master.cf

    “netsmtps inet n – – – – smtpd” and the next five lines which begin with “-o”

    I also noticed in my logs this message:

    “dovecot: config: Warning: Obsolete setting in /etc/dovecot/dovecot.conf:19:
    namespace private {} has been replaced by namespace { type=private },

    so I changed

    namespace private {
    prefix = INBOX.
    inbox = yes
    }

    to

    namespace {
    type = private
    prefix = INBOX.
    inbox = yes
    }

    I am not an expert in code syntax, but after this change the errors stopped, so I assumed that everything is fine now.

    Thank you again!

    Reply
  3. Ed says:
    August 14, 2013 at 6:39 am

    Hi,

    Thank you for your page.
    Recently I setup a homemade e-mail server and wrote a full detailed tutorial that you can find in

    http://cosmolinux.no-ip.org/raconetlinux2/mail.html

    using Debian Squeeze, Postfix, Dovecot, SASL, Spamassassin and Squirrel (and a Google account for SMTP relay).

    I wish it is helpful to someone.

    Reply
  4. Ryan says:
    April 13, 2013 at 10:39 am

    Dear Mr. Stacy,

    Thanks you ever so much for the priceless tutorial. I have followed about 6 to my count up to now, and yours was the only one that gave me a fully functional internal and external email server.

    A few messages between my gmail and my email server confirmed it.
    Thank you again, you really make the days of people like us.

    Reply
  5. Paul says:
    March 29, 2013 at 1:50 pm

    Good tutorial. I also wrote my own tutorial on configuring Ubuntu 12.04 with postfix and dovecot. Check it out http://www.dev-smart.com/archives/492

    Reply
  6. Jon Stacey says:
    March 10, 2013 at 6:50 pm

    Just a note for everyone, I just upgraded to Ubuntu 12.10 and this same setup continues to work just fine for me. The only change that I noticed was an additional question asked while running the dpkg-reconfigure postfix command.

    If you’re asked “Use procmail for local delivery?” answer “Yes”.

    If you have a spam problem you might also check out: http://townx.org/blog/elliot/simple_spamassassin_setup_with_postfix_and_dovecot_on_ubuntu_breezy

    Reply
  7. Cosmin says:
    February 23, 2013 at 12:27 pm

    Thanks so much for this, it works (Ubuntu 12.04)

    Do you have any good tutorial for setting up virtual users (non-linux accounts) with Dovecot, that works with your tutorial above?

    I’ve been trying for a week now, nothing I found works (sending to a virtual user fails with “no such user here, all the time).

    Thanks again!

    Reply
    1. Jon Stacey says:
      February 23, 2013 at 5:23 pm

      I just use system accounts and disable shell acces, but I only have a handful of virtual users. You could setup using MySQL. Check out the Linode library: http://library.linode.com

      Reply
  8. Toby1 says:
    December 18, 2012 at 4:23 am

    God bless you man. I could hardly believe my eyes when it worked.
    I’ve been trying to do this for ages.
    Thank you.

    Reply
  9. Ehsan says:
    October 14, 2012 at 3:54 am

    Please note that i am using ubuntu 12.04 64bit

    Reply
  10. Ehsan says:
    October 14, 2012 at 3:52 am

    Dear Sir,

    Thank you for your tutorial . I am getting some problem with “Edit /etc/default/saslauthd”
    I cannot find any file called “saslauthd”. I am very new to linux it would be very helpful if you kindly explain.

    Thanking you,
    Ehsan

    Reply
    1. Jon Stacey says:
      October 14, 2012 at 1:13 pm

      The guide was written for an older version of Ubuntu, so it’s possible that some things have shifted around. First, make sure that saslauthd (sasl2-bin) is actually installed. If the config file still isn’t there, then it’s possible that it’s moved to a new location, although I haven’t seen any references on the web for this to be the case.

      Reply
  11. Grateful User says:
    September 24, 2012 at 9:31 pm

    Great tutorial; very straightforward (it really only took 10 minutes!).

    I can confirm that this works “as is” for Ubuntu 8.04.

    Thanks for posting!

    Reply
  12. Pingback: Belajar Setting Postfix di Ubuntu Server | theredfan85
  13. wawan says:
    July 25, 2012 at 11:35 pm

    thanks jon, finally my mail server is working, try to find the answer on how should we make configuration in postfix main.cf but not find the right answer to me, until I come to your pages. thanks.
    wawan

    Reply
  14. Hanz says:
    July 25, 2012 at 7:44 pm

    This is awefully good. I have been reading all those instructions and none of them are as easy and as good as this one.

    Thanks buddy.
    !!

    Reply
  15. Sven says:
    May 16, 2012 at 7:41 am

    Thanks a whole lot, that worked out of the box! ☺

    Reply
  16. Aaron Jones says:
    March 23, 2012 at 9:32 pm

    So not that I’ve completed this tutorial, how do I proceed from here? How do I add usernames and passwords for individuals who want to use my email services? How do I know that the email works? Just a few questions I have other than that thanks for the great tutorial I found that the only errors I made were my fault not the tutorials 😀

    Reply
    1. Jon Stacey says:
      March 25, 2012 at 8:37 am

      That depends on the complexity of your needs. For example, my needs are pretty simple so every email user has a corresponding unix account, so dovecot references the passwd file. There are more sophisticated options though such as using MySQL or static database files:

      http://wiki.dovecot.org/VirtualUsers
      http://wiki.dovecot.org/HowTo/DovecotLDAPostfixAdminMySQL

      Reply
  17. bugin70 says:
    March 21, 2012 at 5:15 pm

    I too am having trouble with the line, “/etc/init.d/saslauthd start”
    I get the following – /etc/default/saslauthd: 55: Syntax error: Unterminated quoted string

    I’ve made sure sasl2-bin is installed. I’ve checked for any updates. and I’ve restarted.

    Does anyone have any suggestions. I’d really love to get this working. Many thanks

    Reply
    1. Jon Stacey says:
      March 25, 2012 at 8:41 am

      Sounds like the config fille is corrupt. Perhaps a copy/paste error, the configuration format has changed since I wrote the tutorial, or any other number of reasons. Try to see what’s going on in line 55 of the file to correct the problem by adding the missing quote terminator. Another option is to purge the sasl2-bin and configuration with the package manager and try again.

      Reply
  18. Anuj says:
    March 17, 2012 at 2:18 am

    /etc/init.d/saslauthd start — This line is failing for me.. saying saslauthd not found.

    Also, i tried sending email to my gmail id after this and it did not work. I did not receive any email. Any suggestions.

    Reply
    1. Jon Stacey says:
      March 18, 2012 at 8:46 pm

      Did you install sasl2-bin ?

      Reply
  19. hanuman_bkk says:
    February 25, 2012 at 10:35 am

    This is a nice info. It saved my time a lot to build a mail server.
    Just to share my experience, getting “authentication” issue for pop, I found that, similar to your tip of “telnet localhost 25”, I run “telnet localhost 110” on the server to check how the authentication had been, and I could resolve it later.
    Thanks much for this great article !

    Reply
  20. Zoltan says:
    January 28, 2012 at 8:28 pm

    Thanks, great tutorial, worked out of the box, helped a lot.

    Reply
  21. Dave Nicholson says:
    January 20, 2012 at 5:52 am

    THANK YOU!!!

    I wish I’d found this 15 tutorials earlier. It’s the only one that has worked 100% without any swearing!

    Reply
  22. ice says:
    January 18, 2012 at 5:15 am

    Hey,

    I think installation went well following your steps. Just one question though. When I ran ‘telnet localhost pop3’ at the terminal, I get the return “+OK Dovecot ready,” but it doesn’t give me the login prompt. How can I check my mails then?

    ice

    Reply
    1. Jon Stacey says:
      January 19, 2012 at 4:03 pm

      The purpose of telnet in this guide was simply to test. As for actually retrieving email, you will have to install an email client such as mutt, or download the email with an imap or pop3 client to your local computer, or install a webmail application such as Squirrelmail.

      Reply
    2. hanuman_bkk says:
      February 25, 2012 at 10:39 am

      See an example below;

      root@userver1:/etc# telnet localhost 110
      Trying ::1…
      Trying 127.0.0.1…
      Connected to localhost.
      Escape character is ‘^]’.
      +OK Dovecot ready.
      user mii
      +OK
      pass mii
      +OK Logged in.
      list
      +OK 1 messages:
      1 720
      .
      retr 1
      +OK 720 octets

      Reply
  23. ice says:
    January 18, 2012 at 3:25 am

    Hey Jon,

    I messed up. For system mail name, I typed in server1.example.com. I don’t have my own website. Should I have written localhost? I want to be able to localmail myself. I’m just learning to build a contact form with php. Should I change system mail name to localhost? Or leave it blank? In either case, how can I amend system mail name? Thanks.

    Reply
    1. Jon Stacey says:
      January 19, 2012 at 3:59 pm

      ice, this guide and setup are extreme overkill if you only need to send emails. If that’s the case you only need a simple MTA such as postfix. It won’t allow you to deliver mail locally, but you can deliver to another valid email address (e.g. a gmail account) and will save yourself a lot of unnecessary headaches.

      Reply
  24. Hamda says:
    January 16, 2012 at 4:45 am

    when I type /etc/init.d/saslauthd start it tells me “no such file or directory” what should I do? please help 🙁

    Reply
    1. Jon Stacey says:
      January 19, 2012 at 3:49 pm

      Sounds like you didn’t install it. apt-get install sasl2-bin

      Reply
  25. Pingback: "SASL authentication failed" with mutt and Gmail, why? | MoVn - Linux Ubuntu Center

Comments navigation

Older comments
Newer comments

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

©2025 Jon's View | Built using WordPress and Responsive Blogily theme by Superb