Set up a mail server on Linux

linux mail ubuntu server pop3 imap mailserver

A simple guide how to set up a basic Postfix mail server with IMAP and POP3 services. It does not included advanced topics such as integrating virus-checking and spam-filtering.

Setup Overview

In our setup, Postfix sends and receives mail from Internet and stores them in the user mailboxes while clients in the Internet can retrieve their mails via Courier IMAP or POP3. The user authentication is done by Courier Authdaemon. The following diagram shows this process.

Install Postfix

In this setup I assume that your domain is and it has a valid MX record call Remember to replace with your actual domain in the example codes in this howto. Also I assume that you know what an MX record is. To find out MX your type in a terminal:

dig mx

To install postfix

sudo apt-get install postfix

Intall mailx package for use as command mail utility program. Mail command is installed with this package.

sudo apt-get install mailx

Test your default setup

Add a user before you start this.

sudo useradd -m -s /bin/bash fmaster
sudo passwd fmaster

Test your default installation using the following code segment.

telnet localhost 25

Postfix will prompt like following in the terminal so that you can use to type SMTP commands.

Connected to
Escape character is '^]'.
220 localhost.localdomain ESMTP Postfix (Ubuntu)

Type the following code segment in Postfix’s prompt.

ehlo localhost
mail from: root@localhost
rcpt to: fmaster@localhost
Subject: My first mail on Postfix

Are you there?
. (Type the .[dot] in a new Line and press Enter )

Check the mailbox of fmaster

su - fmaster

When you type mail command an output like follows display in your terminal.

Mail version 8.1.2 01/15/2001. Type ? for help.
"/var/mail/fmaster": 2 messages 2 new
>N 1 root@localhost Mon Mar 6 12:49 13/479 Just a test
N 2 root@localhost Mon Mar 6 12:51 15/487 My first mail

You will observe that mails are indexed by numbers and you can type the number of which the mail that you want to read. For example type no “2” to read the 2nd mail. The type “q” to quit. The mail will be written to a file called mbox in user’s home directory. According to our example it will be /home/fmaster/mbox.

All messages in an mbox type of mailbox are concatenated and stored in a single file. The beginning of each message is indicated by a line whose first five characters are “From ” and a blank line is appended to the end of each message

Setting Postfix Support for Maildir-style Mailboxes

Maildir is a format for an e-mail spool that does not require file locking to maintain message integrity because the messages are kept in separate files with unique names. A Maildir is a directory (often named Maildir) with three subdirectories named tmp, new, and cur. The subdirectories should all reside on the same filesystem.

Another reason to use Maildir format is that Courier IMAP/POP3 servers only work with Maildir format of mailboxes.

Instruct Postfix to use Maildirs instead of Mboxes:

sudo postconf -e "home_mailbox = Maildir/"

Ensure Procmail isn’t used: (if the step was taken during dpkg-reconfigure, by mistake)

sudo postconf -e "mailbox_command = "

Restart Postfix to make changes effect.

sudo /etc/init.d/postfix restart

Test your setup again

Installing courier IMAP and POP3

sudo apt-get install courier-pop
sudo apt-get install courier-imap

Adding your local domains to postfix

Add your domains to mydestination:

sudo postconf -e "mydestination =, localhost.localdomain, localhost,"

Add your local networks, too:

Postfix comes with the localhost ( entry; you may have others, here we assume your LAN is on Make changes to suit your situation.

sudo postconf -e "mynetworks =,"

Make Postfix to receive mail from the Internet

Instruct Postfix to receive on all interfaces:

sudo postconf -e "inet_interfaces = all"

(optional) Make Postfix accept IPv4, IPv6 protocols

If you’re not using IPv6 yet, and you’re paranoid, use “ipv4” instead of “all”. Again, this is to suit your own network sensibilities.

sudo postconf -e "inet_protocols = all"

Finally, restart Postfix;

sudo /etc/init.d/postfix restart

Test your setup again using following code:

telnet 25
mail from: [email protected]
rcpt to: [email protected]
Subject: My first mail for my domain

Are you there?
. (and Enter In a new Line)

Check the mailbox of fmaster

su - fmaster
cd Maildir/new

Now you will see mail has a separate file.

Testing Courier POP3

Type in a terminal:

telnet 110

Use the following example code segment for your test. Be intelligent to tweak the changes appropriately to your environment. An output like follows will display in your terminal.

Connected to (
Escape character is '^]'.
+OK Hello there.

Type the following code segment in the prompt provided by the Courier POP3 server. I assume that you are intelligent enough not to type the lines which starts from +OK

user fmaster
+OK Password required.
pass password
+OK logged in.

For a advanced mode, please visit where you can find the complete guide Step by step guide to install Postfix

Ubuntu + Postfix + Courier IMAP + MySQL + Amavisd-new + SpamAssassin + ClamAV + SASL + TLS + SquirrelMail + Postgrey

Share your love

Leave a Reply

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