Setup mail server on ubuntu 14.04 ( Postfix – dovecot )

This tutorial explains how to setup mail server on ubuntu 14.04 using postfix,dovecot and squirrelmail.
» Postfix ( for sending )
» Dovecot ( for receiving )
» Squirrelmail ( for webmail access ).
Here i have used mail.krizna.com for hostname and krizna.com for Domain . please replace with your domain .

Setup mail server on ubuntu 14.04

» Installing and configuring  postfix
» Installing and configuring  dovecot
» Installing and configuring squirrelmail

» Installing and configuring  postfix

Step 1 » Assign static IP and hostname and add a host entry for the host name .
Assign hostname in /etc/hostname
mail.krizna.comAdd a hostentry in /etc/hosts
192.168.1.10 mail.krizna.com
Step 2 » Update the repositories.
krizna@mail:~$ sudo apt-get update
Step 3 » Install postfix and dependencies . Press enter for all prompted questions during installation. we will do that in the next step.
krizna@mail:~$ sudo apt-get install postfix
Step 4 » After installation issue the below command to configure postfix.krizna@mail:~$ sudo dpkg-reconfigure postfixNow you will be prompted for set of details . choose the following values and replace krizna.com with your domain name.
1. Internet Site
2. krizna.com
3. krizna
4. krizna.com, localhost.localdomain, localhost
5. No
6. 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 192.168.0.0/24
7. 0
8. +
9. all

Step 5 » Now configure Postfix for SMTP-AUTH using Dovecot SASL by adding the below lines to postfix config file /etc/postfix/main.cf .
home_mailbox = Maildir/
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_local_domain =
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination
smtp_tls_security_level = may
smtpd_tls_security_level = may
smtp_tls_note_starttls_offer = yes
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes

Step 6 » Now generate a digital certificate for tls. Issue the commands one by one and provide details as per your domain.
krizna@mail:~$ openssl genrsa -des3 -out server.key 2048
krizna@mail:~$ openssl rsa -in server.key -out server.key.insecure
krizna@mail:~$ mv server.key server.key.secure
krizna@mail:~$ mv server.key.insecure server.key
krizna@mail:~$ openssl req -new -key server.key -out server.csr
krizna@mail:~$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
krizna@mail:~$ sudo cp server.crt /etc/ssl/certs
krizna@mail:~$ sudo cp server.key /etc/ssl/private

Step 7 » Now configure certificate path
krizna@mail:~$ sudo postconf -e 'smtpd_tls_key_file = /etc/ssl/private/server.key'
krizna@mail:~$ sudo postconf -e 'smtpd_tls_cert_file = /etc/ssl/certs/server.crt'

Step 8 » Open /etc/postfix/master.cf file and uncomment below lines to enable smtps ( 465 ) and submission ( 587 ) .

submission inet n       -       -       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING
smtps     inet  n       -       n       -       -       smtpd
  -o syslog_name=postfix/smtps
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING

Step 9 » Now install Dovecot SASL by typing the below command.
krizna@mail:~$ sudo apt-get install dovecot-commonIssue the following values for the prompts during installation.
1. yes
2. mail.krizna.com

Step 10 » Make changes to the files as follows.
Open /etc/dovecot/conf.d/10-master.conf file and find # Postfix smtp-auth line ( line no:95 ) and add the below lines .
# Postfix smtp-auth
unix_listener /var/spool/postfix/private/auth {
mode = 0660
user = postfix
group = postfix
}
Open /etc/dovecot/conf.d/10-auth.conf file and find (line no:100)
auth_mechanisms = plain and replace auth_mechanisms = plain login
Step 11 » Restart postfix and dovecot services
krizna@mail:~$ sudo service postfix restart
krizna@mail:~$ sudo service dovecot restart

Step 12 » Now test SMTP-AUTH and smtp/pop3 port access .
Type the below command and should get below response.
krizna@mail:~$ telnet mail.krizna.com smtp
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mail.kriznaa.com ESMTP Postfix (Ubuntu)
now type ehlo mail.krizna.com and should get below response , please make sure you get those bolded lines .
ehlo mail.krizna.com
250-mail.krizna.com
--------
250-STARTTLS
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN

---------
250 DSN
and try the same with port 587 (telnet mail.krizna.com 587).
Postfix configuration is over, continue for dovecot installation.

» Installing and configuring dovecot

Step 12 » Install dovecot using the below command
krizna@mail:~$ sudo apt-get install dovecot-imapd dovecot-pop3d
Step 13 » Now configure mailbox. Open /etc/dovecot/conf.d/10-mail.conf file and find (Line no:30 )
mail_location = mbox:~/mail:INBOX=/var/mail/%uReplace with
mail_location = maildir:~/Maildir
Step 14 » Now change pop3_uidl_format . Open /etc/dovecot/conf.d/20-pop3.conf file and find and uncomment the below line ( Line no : 50 )
pop3_uidl_format = %08Xu%08Xv
Step 15 » Now enable SSL . Open /etc/dovecot/conf.d/10-ssl.conf file and find and uncomment the below line ( Line no : 6 )
ssl = yes
Step 16 »Restart dovecot service.
krizna@mail:~$ sudo service dovecot restart
Step 17 » Now test pop3 and imap port access using the telnet command.
krizna@mail:~$ telnet mail.krizna.com 110
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
+OK Dovecot (Ubuntu) ready.
Repeat the same for 995,993,143 ports.
OR check for listening ports using netstat command .
krizna@mail:~$ netstat -nl4 you should get the result like below.
Setup mail server on ubuntu 14.04
Step 18 » Create some users and check using mail clients like thunderbird or outlook
krizna@mail:~$ sudo useradd -m bobby -s /sbin/nologin
krizna@mail:~$ sudo passwd bobby

Setup mail server on ubuntu 14.04
Now mail server is ready, you can send and receive mail using the server. Continue for squirrelmail ..

» Installing and configuring squirrelmail

Step 19 » Install squirrelmail using the below command. This will install apache and PHP packages.
krizna@mail:~$ sudo apt-get install squirrelmail
Step 20 » Configure squirrelmail
krizna@mail:~$ sudo squirrelmail-configureEverything is pre-configured , we just need to change Organization name .
» Press 1 (Organization Preferences) » again press 1 (Organization Name) » Organization Name » Press S » Press Q to quit
Step 19 » Now configure apache to enable squirrelmail .
krizna@mail:~$ sudo cp /etc/squirrelmail/apache.conf /etc/apache2/sites-available/squirrelmail.conf
krizna@mail:~$ sudo a2ensite squirrelmail

Step 20 » Restart Apache service
krizna@mail:~$ sudo service apache2 restart
Step 21 » Now open http://serverIP/squirrelmail in your browser and login using username (bobby) . you can send and receive mail using squirrelmail.
Okay .. its over now ..
Please note when you decide to move server to the production, it is very important to implement Spam filter and antivirus . please check this post for more detail Postfix spamfilter and antivirus implementation
Also see :
» Setup mail server on centos 7
» Setup mail server on centos 6

46 Comments

  1. I did this config but I could not see any messages. Basically the mail wasn’t being delivered/handled either to the right location or by the right mail handling program. I assume procmail was handling delivery but I don’t now. HERE is how I fixed it:

    Comment out the line in>> /etc/postfix/main.cf
    mail_command = procmail -a $”EXSTENSION”

    and append below:
    mail_command =

    Not sure why this is but I can now see all the messages sent since this change was made in Thunderbird, Windows Mail and SquirrelMail.

    • If you had installed mail server using tasksel , you will be prompted for an extra value during 4th step ..
      “Use procmail for local delivery ?”
      Choose No and continue

      Thanks for your comment.

  2. I did this config but I could not see any messages. Basically the mail wasn’t being delivered/handled either to the right location or by the right mail handling program. I assume procmail was handling delivery but I don’t now. HERE is how I fixed it:

    Comment out the line in>> /etc/postfix/main.cf
    mail_command = procmail -a $”EXSTENSION”

    and append below:
    mail_command =

    Not sure why this is but I can now see all the messages sent since this change was made in Thunderbird, Windows Mail and SquirrelMail.

    • If you had installed mail server using tasksel , you will be prompted for an extra value during 4th step ..
      “Use procmail for local delivery ?”
      Choose No and continue

      Thanks for your comment.

    • MAN YOU ARE A SAVIOUR!!! I have been struggling with emails not coming, not receiving, no error except of SASL login authentification failed in log files.. Spent last day just solving this. Now it works like a charm again! Thanks a lot.

  3. THANKS! I’ve been trying to set mail stuff up for ages and never got it working, this is working flawlessly exactly like I want, thanks again.

  4. THANKS! I’ve been trying to set mail stuff up for ages and never got it working, this is working flawlessly exactly like I want, thanks again.

  5. Dear Krizna,

    Thank you for your guide

    I got a question, when I try to telnet the postfix, it’s connected but the result is 220 UNKNOWN ESMTP Postfix (Ubuntu)

    Can you please help me with this issue?
    Also I already try to login to squirrelmail via browser and it works perfectly
    My mail can receive message, but unfortunately it can’t send email out
    Did I missconfigure or something?

  6. Dear Krizna,

    Thank you for your guide

    I got a question, when I try to telnet the postfix, it’s connected but the result is 220 UNKNOWN ESMTP Postfix (Ubuntu)

    Can you please help me with this issue?
    Also I already try to login to squirrelmail via browser and it works perfectly
    My mail can receive message, but unfortunately it can’t send email out
    Did I missconfigure or something?

  7. Hello krizna, squirrelmail worked super fine. but as you know it does not have any html composing system. Also if you receive email from 3rd party, it shows garbage attachments. Cann’t use squirrelmail even for general email purposes. outdated.

    Could you please add tutorials for installing horde and roundcube as well ?

  8. Hello krizna, squirrelmail worked super fine. but as you know it does not have any html composing system. Also if you receive email from 3rd party, it shows garbage attachments. Cann’t use squirrelmail even for general email purposes. outdated.

    Could you please add tutorials for installing horde and roundcube as well ?

  9. I’ve followed a few guides on trying to set postfix and dovecot up together (ignoring the squirrelmail) and none of them worked, this one did first time. Not sure it that’s just luck because we’re both on a very similar set up but either way thank you!

  10. I’ve followed a few guides on trying to set postfix and dovecot up together (ignoring the squirrelmail) and none of them worked, this one did first time. Not sure it that’s just luck because we’re both on a very similar set up but either way thank you!

  11. Doesn’t work for me. I did everything exactly as you described it. Dovecot connects and disconnects after half a second again. No chance to get or to send mails.

  12. Doesn’t work for me. I did everything exactly as you described it. Dovecot connects and disconnects after half a second again. No chance to get or to send mails.

  13. We get an error if we install squirrelmail in the last stap and we can fix this with google:

    ERROR: Could not complete request.

    Query: SELECT “INBOX”

    Reason Given: [SERVERBUG] Internal error occurred. Refer to server log for more information. [2014-09-17 14:09:59]

        • sudo nano /etc/dovecot/conf.d/10-mail.conf
          And do not just uncomment the (mail_location)
          but change the value of uncommented mail_location to the given below you will not receive any of such error

          # mail_location = maildir:~/Maildir
          # mail_location = mbox:~/mail:INBOX=/var/mail/%u
          # mail_location = mbox:/var/mail/%d/%1n/%n:INDEX=/var/indexes/%d/%1n/%n
          #
          #
          #
          mail_location = maildir:~/Maildir

          • Hi Krizna,

            Firstly thanks for the documentation. I followed every steps of your documentation & my mail server is running fine while sending other domain but can’t receive mail from other domain. An error message is showing while sending mails from other domain i.e. Recipient address rejected: User unknown in local recipient table. But internal mail is working fine. I can send and receive mails within the domain.

            Requesting you to help me to fix the issue.

            Thanks in advance.

            Regards,

            ankualliance

  14. We get an error if we install squirrelmail in the last stap and we can fix this with google:

    ERROR: Could not complete request.

    Query: SELECT “INBOX”

    Reason Given: [SERVERBUG] Internal error occurred. Refer to server log for more information. [2014-09-17 14:09:59]

        • sudo nano /etc/dovecot/conf.d/10-mail.conf
          And do not just uncomment the (mail_location)
          but change the value of uncommented mail_location to the given below you will not receive any of such error

          # mail_location = maildir:~/Maildir
          # mail_location = mbox:~/mail:INBOX=/var/mail/%u
          # mail_location = mbox:/var/mail/%d/%1n/%n:INDEX=/var/indexes/%d/%1n/%n
          #
          #
          #
          mail_location = maildir:~/Maildir

          • Hi Krizna,

            Firstly thanks for the documentation. I followed every steps of your documentation & my mail server is running fine while sending other domain but can’t receive mail from other domain. An error message is showing while sending mails from other domain i.e. Recipient address rejected: User unknown in local recipient table. But internal mail is working fine. I can send and receive mails within the domain.

            Requesting you to help me to fix the issue.

            Thanks in advance.

            Regards,

            ankualliance

  15. Hi Krizna,

    I’ve followed several tutorials (and a combination of) but your one really nailed it. You’ve covered items that others missed and I really like step 12 in your postfix setup. I had a problem with the authentication which I didn’t know about, but this step told me that something was missing and I fixed it before moving on.

    If anyone wants to check that their configuration files for dovecot are causing problems you can try this:

    sudo service dovecot restart

    cat /var/log/syslog

    Oct 8 09:51:56 ubuntu dovecot: master: Dovecot v2.2.9 starting up (core dumps disabled)

    Hope this helps, and thanks Krizna

  16. Hi Krizna,

    I’ve followed several tutorials (and a combination of) but your one really nailed it. You’ve covered items that others missed and I really like step 12 in your postfix setup. I had a problem with the authentication which I didn’t know about, but this step told me that something was missing and I fixed it before moving on.

    If anyone wants to check that their configuration files for dovecot are causing problems you can try this:

    sudo service dovecot restart

    cat /var/log/syslog

    Oct 8 09:51:56 ubuntu dovecot: master: Dovecot v2.2.9 starting up (core dumps disabled)

    Hope this helps, and thanks Krizna

  17. Everything works except squirrelmail. At step 19 “sudo a2ensite squirrelmail” it says “ERROR: Site squirrelmail does not exist!”. Can you guide what is the problem ?

  18. Everything works except squirrelmail. At step 19 “sudo a2ensite squirrelmail” it says “ERROR: Site squirrelmail does not exist!”. Can you guide what is the problem ?

  19. I have installed as per the instructions, but…:

    when testing the smtp-auth settings I get my computer’s IP and not local:

    user@mail ~ $ telnet mail.user.org smtp
    Trying 172.16.1.183…
    Connected to mail.user.org.
    Escape character is ‘^]’.
    220 unkl ESMTP Postfix (Ubuntu)

    and running ehlo I do not get the important highlighted auth lines:
    ehlo mail.user.org
    250-user
    250-PIPELINING
    250-SIZE 10240000ehlo mail.user.org
    250-user
    250-PIPELINING
    250-SIZE 10240000
    250-VRFY
    250-ETRN
    250-STARTTLS
    250-ENHANCEDSTATUSCODES
    250-8BITMIME
    250 DSN

    when trying to send email to the test account bobby, I get error authenticating, how to fix this this? Apache2 answers on the web, but mail is not working.

    Your input will be appreciated.

  20. Hey, i’ve a question. … The following thing is interesting for me. When i configure postfix and dovecot with your how to. Is my server an open relay, or isn’t it because i do step 10. Sorry for my bad english and thanks for answer.

  21. Hey, i’ve a question. … The following thing is interesting for me. When i configure postfix and dovecot with your how to. Is my server an open relay, or isn’t it because i do step 10. Sorry for my bad english and thanks for answer.

  22. I posted this question before, but I no longer see it. Port 25 is blocked by my ISP. In your postfix configuration, what would need to be configured for SMTP relay though Gmail or Comcast? Thanks!

  23. I posted this question before, but I no longer see it. Port 25 is blocked by my ISP. In your postfix configuration, what would need to be configured for SMTP relay though Gmail or Comcast? Thanks!

1 Trackback / Pingback

  1. Setup a mail server on ubuntu 14.04 ( Postfix – dovecot ) | Lost Packets – WordPress.com | Ubuntu VPS Hosting

Leave a Reply

Your email address will not be published.


*