A definitive guide to setting up your own Mail Server – Part 1

Setting up a mail server, in itself, is not that difficult a task, but there are integrations that can really mess up with your time and mind. Integrations such as SMTP authentication, setting up virtual users and virtual mailboxes, setting up spam-filtering and antivirus filtering can really be a pain if not configured properly. Hence, I decided why not share my own experiences in the form of a tutorial. May be this will help out someone trying to setup a mail server.

I have divided this tutorial into several parts, so that I can explain as we go about setting up the server, and of course going one step at a time, ensures that we are understanding what we are doing.

About the first part

This is first part of a series of tutorials on “setting up a mail server” and it will deal with setting up the most basic form of Postfix, while the later parts will deal with adding more functionality, like SMTP authentication, POP3 and IMAP access, spam and virus filtering, etc.

I am assuming that you have Ubuntu installed on your machine and its up and running and connected to the internet.

Where ever I refer to yourdomain.com, replace it with your actual domain name (in my case it is ovaistariq.net).

Now lets get started!

Getting started

Setting up DNS record

Before you go on and follow the steps required to set up the DNS record, make sure you are familiar with DNS configuration. If you are not then please first read a guide on setting up DNS. The following DNS related entries need to go into the DNS zone file. You may also ask your hosting support guys as to how to setup the following DNS entries.

Repeat the following steps:

  • Crete a dns entry for the MX record:

    @ MX 10 mail.yourdomain.com
  • Create a A/AAAA record for the hostname mail that points to the ip address of your machine on which mail server will be setup.

    mail A 145.45.241.154

    Here replace 145.45.241.154 with the IP address of the machine on which mail server will be setup.

Setting up the hostname

Edit the file /etc/hostname, it should only have a single line that reads as follows:

yourdomain.com

Make sure that you have following entires in /etc/hosts file:

127.0.0.1 localhost
127.0.0.1 localhost.localdomain
127.0.0.1 local

Setting up the mail server name

Edit the file /etc/mailname, it should only have a single line that reads as follows:

yourdomain.com

Setting up the mail aliases

Edit the file /etc/aliases and the following aliases:

postmaster:root
abuse:root
security:root
root:root,ovaistariq@gmail.com

Replace ovaistariq@gmail.com with your email address where you want emails to be delivered to whenever emails are sent to root@localhost.

The above aliases ensure that local email addresses postmaster, abuse, security, root are actually aliases of the local email address root.

Installing the required package

Now lets start installing the required packages.
The only package that we need right now is the Postfix package.

Postfix for sending and receiving emails

We would be using Postfix as our MTA, and Postfix would be sending and receiving emails.

$ sudo apt-get install postfix

Configuring Postfix to send and receive emails

Configuring Postfix isn’t much difficult either if you now the configuration settings. We won’t be using the dpkg-configure utility. Instead we will be editing the configuration files directly because that is much simpler and less error-prone.

Main configuration files

Postfix has the following main configuration files:

  • /etc/postfix/main.cf
    This file is the main configuration file that holds various configuration settings like the mail server name, the networks on which mail is allowed to be transmitted, setting that allows Postfix to relay emails to be sent to other servers, information about how it should authenticate the mail users, etc.
  • /etc/postfix/master.cf
    This file is the Postfix process configuration file that hold information such as which services are offered (say smtp service is offered as well as smtps), how mails are to be relayed, how Postfix will interface with other processes (say the amavis process for filtering spam and virus), etc.

The above mentioned files are very important for Postfix to work properly, so be extra careful when editing the above two files. Next we will be editing the above two files.

Editing configuration files

Let’s start by first editing the /etc/postfix/main.cf configuration file.
This file should only have the following content:

# See /usr/share/postfix/main.cf.dist for a commented, more complete version

# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

readme_directory = no

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

myhostname = mail.ovaistariq.net
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = localhost.localdomain, localhost
#mydestination =
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_command = procmail -a "$EXTENSION"
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all
home_mailbox = Maildir/

# Extra params

# how long if undelivered before sending warning update to sender
delay_warning_time = 4h
# will it be a permanent error or temporary
unknown_local_recipient_reject_code = 450
# how long to keep message on queue before return as failed.
#maximal_queue_lifetime = 3d
# max and min time in seconds between retries if connection failed
minimal_backoff_time = 1000s
maximal_backoff_time = 8000s
# how long to wait when servers connect before receiving rest of data
smtp_helo_timeout = 60s
# how many error before back off.
smtpd_soft_error_limit = 3
# how many max errors before blocking it.
smtpd_hard_error_limit = 12

Now let’s edit the file /etc/postfix/master.cf.
This file should only have the following content:

# Postfix master process configuration file.  For details on the format
# of the file, see the master(5) manual page (command: "man 5 master").
#
# Do not forget to execute "postfix reload" after editing this file.
#
# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (yes)   (never) (100)
# ==========================================================================
smtp      inet  n       -       -       -       -       smtpd
#628       inet  n       -       -       -       -       qmqpd
pickup    fifo  n       -       -       60      1       pickup
        -o content_filter=
        -o receive_override_options=no_header_body_checks
cleanup   unix  n       -       -       -       0       cleanup
qmgr      fifo  n       -       n       300     1       qmgr
#qmgr     fifo  n       -       -       300     1       oqmgr
tlsmgr    unix  -       -       -       1000?   1       tlsmgr
rewrite   unix  -       -       -       -       -       trivial-rewrite
bounce    unix  -       -       -       -       0       bounce
defer     unix  -       -       -       -       0       bounce
trace     unix  -       -       -       -       0       bounce
verify    unix  -       -       -       -       1       verify
flush     unix  n       -       -       1000?   0       flush
proxymap  unix  -       -       n       -       -       proxymap
proxywrite unix -       -       n       -       1       proxymap
smtp      unix  -       -       -       -       -       smtp
# When relaying mail as backup MX, disable fallback_relay to avoid MX loops
relay     unix  -       -       -       -       -       smtp
        -o smtp_fallback_relay=
#       -o smtp_helo_timeout=5 -o smtp_connect_timeout=5
showq     unix  n       -       -       -       -       showq
error     unix  -       -       -       -       -       error
retry     unix  -       -       -       -       -       error
discard   unix  -       -       -       -       -       discard
local     unix  -       n       n       -       -       local
virtual   unix  -       n       n       -       -       virtual
lmtp      unix  -       -       -       -       -       lmtp
anvil     unix  -       -       -       -       1       anvil
scache    unix  -       -       -       -       1       scache
#
# ====================================================================
# Interfaces to non-Postfix software. Be sure to examine the manual
# pages of the non-Postfix software to find out what options it wants.

# Many of the following services use the Postfix pipe(8) delivery
# agent.  See the pipe(8) man page for information about ${recipient}
# and other message envelope options.
# ====================================================================
#
# maildrop. See the Postfix MAILDROP_README file for details.
# Also specify in main.cf: maildrop_destination_recipient_limit=1
#
maildrop  unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}
#
# ====================================================================
#
# Recent Cyrus versions can use the existing "lmtp" master.cf entry.
#
# Specify in cyrus.conf:
#   lmtp    cmd="lmtpd -a" listen="localhost:lmtp" proto=tcp4
#
# Specify in main.cf one or more of the following:
#  mailbox_transport = lmtp:inet:localhost
#  virtual_transport = lmtp:inet:localhost
#
# ====================================================================
#
# Cyrus 2.1.5 (Amos Gouaux)
# Also specify in main.cf: cyrus_destination_recipient_limit=1
#
#cyrus     unix  -       n       n       -       -       pipe
#  user=cyrus argv=/cyrus/bin/deliver -e -r ${sender} -m ${extension} ${user}
#
# ====================================================================
# Old example of delivery via Cyrus.
#
#old-cyrus unix  -       n       n       -       -       pipe
#  flags=R user=cyrus argv=/cyrus/bin/deliver -e -m ${extension} ${user}
#
# ====================================================================
#
# See the Postfix UUCP_README file for configuration details.
#
uucp      unix  -       n       n       -       -       pipe
  flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)
#
# Other external delivery methods.
#
ifmail    unix  -       n       n       -       -       pipe
  flags=F user=ftn argv=/usr/lib/ifmail/ifmail -r $nexthop ($recipient)
bsmtp     unix  -       n       n       -       -       pipe
  flags=Fq. user=bsmtp argv=/usr/lib/bsmtp/bsmtp -t$nexthop -f$sender $recipient
scalemail-backend unix  -       n       n       -       2       pipe
  flags=R user=scalemail argv=/usr/lib/scalemail/bin/scalemail-store ${nexthop} ${user} ${extension}
mailman   unix  -       n       n       -       -       pipe
  flags=FR user=list argv=/usr/lib/mailman/bin/postfix-to-mailman.py
  ${nexthop} ${user}

That’s all of the initial configuration that we need for Postfix to start running. There will be more configuration settings to do when we setup anti-virus, anti-spam, authentication, user management, etc.

Make sure that connections to port 25 are allowed by the firewall on your server, if you have any. If you are using Ubuntu’s default Firewall UFW, then its as simple as $ ufw allow 25. You can check which ports are allowed by doing this, $ ufw status

Testing the configuration

Now let’s test out our mail server, by sending out an email to an email address on another domain, say ovaistariq@gmail.com. The tool that we will be using to do our testing is telnet.
If you don’t already have telnet installed, then you can do so as follows:

$ sudo apt-get install telnet

Sending a test email to an external email address

Start off by issuing the following command

$ telnet localhost 25

You should a response as follows:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mail.yourdomain.com ESMTP Postfix

Of course instead of mail.yourdomain.com you would see the name of your domain, in my case its mail.ovaistariq.net

Now lets initiate the hand shake with the mail server by issuing the following command

EHLO localhost

The response from the server should be something like follows:

250-mail.yourdomain.com
250-PIPELINING
250-SIZE 10240000
250-8BITMIME
250 DSN

Next step is to tell the mail server the from email address (replace yourdomain.com in the following line with the name of your domain)

MAIL FROM: 

The response should be:

250 2.1.0 Ok

Next we tell the mail server who the recipient of the mail will be (replace ovaistariq@gmail.com with your own external email address)

RCPT TO: 

The response should be:

250 2.1.0 Ok

Next we tell the mail server that we are ready to write down the email body

data

The response will be something like the following:

354 End data with .

Now enter the message body and end with a line with only a full stop

This is a test message and i am testing to see if i can send out email to external email address.
.

If you get a message the email has been queued as follows then all is well

250 2.0.0 Ok: queued as 5B708A841

Now lets issue the quit command to end our session with the mail server

quit

But do we know that the email has been sent. Yes we can find out by looking at the logs.
Postfix logs all the activity in a file /var/log/mail.log. So a quick look and if you see a log entry similar to the following one, then it means the email has been sent successfully:

postfix/smtp[7194]: 0ACE6A84B: to=, relay=gmail-smtp-in.l.google.com[209.85.229.27]:25, delay=0.62, delays=0/0.01/0.29/0.33, dsn=2.0.0, status=sent (250 2.0.0 OK 1285503898 n30si6563972weq.139)

Well this isn’t just the end of setting up your Mail Server, there is more to follow.

What’s coming?

This is just the first part of a series of tutorials, there are more interesting things to come, so keep tuned. Let me list down whats coming:

  • Setting up (Virtual) users and mailbox management, using MySQL as the backend
  • Setting up POP3 and IMAP access for email clients
  • Setting up Spamassassin, Pyzor and Razor for anti-spam related functionality
  • Setting up ClamAV to be used for mail anti-virus filtering
  • Setting up SMTP authentication using Cyrus SASL
  • Setting up mail white-listing and black-listing using Postgrey
  • Setting up RoundCube as the web email client
  • Setting up Postfix Admin as email and user management front end

So stay tuned, I won’t be taking too long to finish up this series of tutorials. I hope this first part was of help to you. See you soon, cheers till then!

  • http://alwaysgetthebest.com/?p=8121 A definitive guide to setting up your own Mail Server – Part 1 … | Domain registration always get the best

    [...] this article: A definitive guide to setting up your own Mail Server – Part 1 … Also of interest:Proxy Profit: Your definitive guide on how to make money online …Proxy [...]

  • http://internetdomainnames4u.com/?p=8606 A definitive guide to setting up your own Mail Server – Part 1 …

    [...] more here: A definitive guide to setting up your own Mail Server – Part 1 … actual, and-running, internet, machine, pipe-flags, replace-it-with, the-internet, ubuntu, [...]

  • http://dnrsimplified.com/?p=705 A definitive guide to setting up your own Mail Server – Part 1 … « DNR Simplified

    [...] original here: A definitive guide to setting up your own Mail Server – Part 1 … Related Posts:A definitive guide to setting up your own Mail Server – Part 1 … I am assuming [...]

  • http://topsy.com/www.ovaistariq.net/2010/09/a-definitive-guide-to-setting-up-your-own-mail-server-part-1/?utm_source=pingback&utm_campaign=L2 Tweets that mention A definitive guide to setting up your own Mail Server – Part 1 | ovais.tariq — Topsy.com

    [...] This post was mentioned on Twitter by Proggit Articles, Ovais Tariq. Ovais Tariq said: A definitive guide to setting up your own Mail Server – Part 1 | ovais.tariq http://t.co/G7pSBPy via @ovaistariq #postfix #ubuntu #server [...]

  • http://www.erasedmail.com/?p=22144 A definitive guide to setting up your own Mail Server – Part 1 … | www.erasedmail.com

    [...] post: A definitive guide to setting up your own Mail Server – Part 1 … really-mess, [...]

  • M Lark_88

    I tried running the command

    @ MX 10 mail.yourdomain.com

    on my ubuntu command prompt.. but nothing happened ? ?? help please…

  • http://www.ovaistariq.net/ Ovais Tariq

    This command is not supposed to be run on the command prompt, this will go inside the dns zone file. Where is your domain hosted?

  • M Lark_88

    What in the world is a dns zone file ? This is certainly not the definitive guide.. perhaps you don’t know the meaning of ‘definitive’ man. No offense, but you can rather clean the article and make it easy for poeple like me to understand. eh?

  • http://www.ovaistariq.net/ Ovais Tariq

    Well no offense but if you read the article thoroughly, you will see “Crete a dns entry for the MX record” written above the line “@ MX 10 mail.yourdomain.com”, which means you have to create a dns entry. And because this article is by no means on the topic of “DNS” so, I have omitted it, but even then if you need my help in setting up a DNS entry, I am ready to help you out.

    Of course there has to be some kind of a knowledge that you need to have before hand before you start setting up your mail server :)

  • http://www.teamsportguide.com/coaching-footballs-zone-offense/ Coaching Football’s Zone Offense | Team Sport Guide

    [...] A definitive guide to setting up your own Mail Server – Part 1 | ovais.tariq [...]

  • http://internetdomainnames4u.com/?p=9175 A definitive guide to setting up your own Mail Server – Part 1 …

    [...] more here: A definitive guide to setting up your own Mail Server – Part 1 … always-get, article, best, definitive, make-money, profit-, proxy-profit, server, the-best, [...]

  • http://dragonflypictures.info/products-accessories/the-definitive-guide-to-local-search DragonFly Pictures» Dragonfly Pictures | The Definitive Guide To Local Search

    [...] A definitive guide to setting up your own Mail Server – Part 1 … [...]

  • http://www.supercallsaver.com/2010/09/30/the-definitive-guide-to-guinea-pig-care/ Super Call Saver Telecommunications Savings Portal » Blog Archive » The Definitive Guide to Guinea Pig Care

    [...] A definitive guide to setting up your own Mail Server &#8211… [...]

  • http://www.coffee-tea-cocoa.info/italy-from-the-inside-ebookr/ Italy From The Inside EBook(R. | Coffee, Tea, and Cocoa.

    [...] A definitive guide to setting up your own Mail Server – Part 1 … [...]

  • http://www.weatherproofsecuritycameras.org/10933/i-need-to-secure-my-boat-with-security-cameras-because-some1-is-breaking-in-while-its-in-the-boat-yard/ I need to secure my boat with security cameras because some1 is breaking in while its in the boat yard.? | Weather Proof Security Cameras

    [...] A definitive guide t&#959 setting up &#1091&#959&#965r &#959wn Mail Server – P&#1072rt 1 | ova… [...]

  • http://cord-blood-banking-blog.tk/archives/1967 Spamfilter xQmail.eu for a future without spam | cord blood banking

    [...] A definitive guide to setting up your own Mail Server – Part 1 … [...]

  • Serbmanalex

    Thanks! This is the most straightforward explanation I’ve found on the web – I’m eager for future installations. Any clue when?

  • http://www.ovaistariq.net/ Ovais Tariq

    Till the time there is no future installation., you can probably have a look here:

    http://www.ovaistariq.net/510/my-recommendation-for-a-basic-mail-server-setup/

  • Anonymous

    OMG, relax man! This guide is about “setting up a mail server” and not “setting DNS”.

    As an example, if you’re using freedns:

    yourdomain.com (G)MX10:mail.yourdomain.com

  • Oakley vault

    The eyewear brand companies Oakley vault
    sunglasses
    mainly for women with a couple
    of designs in the actual mens category. Known because of its colorful and unique
    sets, Oakley
    vault
    are easily recognizable using the
    embossed logo on the actual temples. The exquisite design is from the frames
    range from Oakley
    sunglasses outlet
    to classic aviator tones,
    with pastel and earthy colored lenses. The variety of colors the brand offers
    could be compared with the mood from the Cheap
    Oakley vault
    .

  • http://bestsmtpserver.com/ BEST SMTP SERVER

    Great tip. Visit the website http://bestsmtpserver.com/.

blog comments powered by Disqus