My VPS is running Ubuntu, and I'd like to be able to receive email to my domain.

How do I easily set up a mail server to do this?

  • 3
    Great question, I'm waiting for a great answer. I have build a couple of mail servers over the years and it has never been easy. Lot's of decisions: what SMS server, what IMAP/POP, how to store the mails, how to store users... I'm complete unable to give an easy answer. Jul 29, 2011 at 6:41

4 Answers 4


This is how I've setup mail on our production machines. These are the criteria that we needed:

  • Email Accounts
  • Email Aliases (Forwarders)
  • IMAP, POP3, and SMTP

"Easy" (tl;dr)

First, I want to address what would appear to be the easiest solution.

sudo tasksel install mail-server

Several issues with this occurred when we tried this: First it installs Dovecot, which is fine for most, but we've deemed Courier to be the better of the two for our needs. Second, it utilizes Postfix which is great but we also need Exim as it's a more powerful MTA/SMTP server. Third, it installs MySQL - in the configuration I utilize we prefer flat files for configuration as it's one less breaking point. Think what would happen if MySQL crashed for some unknown reason. Otherwise the rest of the packages is pretty straight forward and easy to maintain for a small mail service (think 1-2 email domains total).

Our Configuration

Directory Structure

We stray slightly outside the path of normal configurations but it makes for easier management.

All of our mail is stored in /var/mail/virtual/<domain>/<user>/mail So for future examples I'll be using email@example.com, fwd@example.com, foo@example.com to represent an email address, a forwarder to go to example@gmail.com, and a bad address respectively. In the above example it would be /var/mail/virtual/example.com/email/mail.

I also maintain a list of all the domains on the server in /etc/valiases but more about that later.


This is more or less the easy part of the setup. Just install the postfix package.


Install Exim with apt-get install exim4 exim4-base exim4-config exim4-daemon-heavy Once installed you'll need to edit the exim default configuration to replace or add the following values:

domainlist local_domains = @:localhost:dsearch;/etc/valiases:dsearch;/var/mail/virtual
daemon_smtp_ports = smtp : 587 : 465

(These lines will appear in different parts of the file, replace each accordingly)

Once that's complete rebuild the exim configuration with update-exim4.conf This concludes the changes required for Exim


Install Courier with courier-base this should install courier-authdaemon, courier-authlib*, courier-imap*, courier-pop*, courieruserinfo, courier-ssl

There honestly isn't much configuration outside the standard. You'll just need to create a user database.


Exim and Courier check a few places to see if a login or an incoming email are valid. Exim checks if the domain is listed as a local hostname, or if the domain is in /var/mail/virtual or if the domain is in /etc/valiases.

Creating Email Accounts

I eventually created several tools to streamline this process - but adding a new user goes to the tune of:

mkdir -p /var/mail/virtual/example.com/email
chown -R mail.mail /var/mail/virtual/example.com/
maildirmake /var/mail/virtual/example.com/email/mail
chown -R mail.mail /var/mail/virtual/example.com/

Then add the address to courier userdb - so they can log in

userdb email@example.com set uid=8 gid=8 home=/var/mail/virtual/example.com/email mail=/var/mail/virtual/example.com/email/mail

Make sure to replace the values where appropriate. Also - uid and gid need to be the numerical user/group ids for the mail user.

userdbpw -md5 | userdb email@example.com set systempw

This will prompt you for a password, enter the one you wish to use for the account.


Finally, generate the userdb hash/shadow files. Restart Courier and test if your changes work:

authtest email@example.com

Should produce something similar to

Authentication succeeded.

     Authenticated: email@example.com  (uid 8, gid 8)
    Home Directory: /var/mail/virtual/example.com/email
           Maildir: /var/mail/virtual/example.com/email/mail
             Quota: (none)
Cleartext Password: (none)
           Options: (none)

If you see "Authentication FAILED: Operation not permitted" instead edit /etc/courier/authdaemonrc and add authuserdb to the authmodulelist line.

After all tests have been confirmed, restart the various services involved (courier-authdaemon, exim4), open the ports 143, 25, 586, 495, 110 and setup the accounts in your favorite mail client.

Creating email aliases

For each domain you should create a file in /etc/valiases (create if it doesn't exist) with at least the following line:

*: :fail: No user at this address.

What this says: If the incoming mail doesn't match any email account I have on file - then the mail should be failed and bounced with a message: "No user at this address". So all mail sent to say: foo@example.com would be bounced as a failure.

However, we have a few email address we wish to maintain elsewhere - say example@gmail.com - in order to do so we need to create /etc/valiases/example.com and the contents of the file should be as follows:

fwd: example@gmail.com
*: :fail: No user at this address.

That way, even though fwd@example.com doesn't match any email accounts on the server, it matches in the /etc/valiases file and the mail will be forwarded to example@gmail.com - However, foo@example.com will still fail with a "No user at this address" message.

  • 11
    Does this come with an ISBN number?
    – Wesley
    Aug 4, 2011 at 3:36
  • 1
    @WesleyDavid I don't think my bash history will show up as an ISBN :) Aug 4, 2011 at 3:57
  • Just following these - much appreciated - I've edited above to cover an error I got with the authtest line. Mar 24, 2012 at 17:30
  • @DarrenGreaves Thanks! I'm glad it helped and thanks for the update! Mar 24, 2012 at 19:54
  • I'm struggling to get all of this working - I only got valiases routing to work when I added the (edited for paths) 350 routing file from debian-administration.org/articles/140 - virtual domains stuff doesn't work at all - did you have to add any routing files at all? Ta. Mar 25, 2012 at 10:05

The easiest way is to run sudo tasksel install mail-server. That will give you an email server with sane defaults. All you have to do, is to answer a few questions. Obviously, you're still able to do manual configurations afterwards if that's necessary, but in most cases it won't be. Just follow the on-screen directions and you should be fine.

Reading up on email services administration is absolutely recommendable though.

Official References:

  • Is there a lazy way to see what is that going to do (packages that it will install, default settings)?. By lazy I mean without installing it myself. Jul 29, 2011 at 18:04
  • @Javier you can view what contents are installed by running sudo tasksel --task-packages mail-server Jul 30, 2011 at 14:27
  • Configuration stored in MySQL, no anti-spam, no anti-virus... it's no really useful for us :(. Jul 31, 2011 at 18:28

I don't have a "great answer" but you may find these links helpful https://help.ubuntu.com/community/PostfixBasicSetupHowto#Receiving Mail and https://help.ubuntu.com/community/Postfix


Just been doing this myself, you do indeed need postfix, and in my case I wanted an imap server as well so that I can use a a nice gui client (not mentioning any names) on another machine. I used these documents:


Dovecot (imap and pop3)

Its actually pretty simple, got it up and running in a few minutes and am receiving e-mails. This is also useful to check that everything is set up ok pingability.

Oh, and you will of course have to set up your dns entries correctly as below (based on the settings that worked for me):

name   type   content
  @      A     ???.???.??.??    
mail     A     ???.???.??.??


         MX Records
Name                 Priority
mail.mydomain.com.      1
mail2.mydomain.com.     2

Note the full stop at end of mail servers and insert your ip address and domain name where appropriate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.