Mailserver with virtual users and domains using Postfix and Dovecot on a CentOS 6 VPS

Leave reply

mailserver-with-virtual-users-and-domains-using-postfix-and-dovecot-in-centos-6The following article will show you how to install and run simple POP3/IMAP/SMTP mail server in your CentOS VPS using virtual users and domains with Postfix and Dovecot

What is Postfix? It is a drop in replacement for the old and mature Sendmail. Postfix also attempts to be very fast, easy to administer, and secure.

What is Dovecot? It is an open source IMAP and POP3 server for *NIX-like systems, written primarily with security in mind.

PRE-REQ

You may want to check if your hostname/domainname is a valid FQDN (fully qualified domain name) and it has a valid MX DNS record.

## if ! type -path "dig" > /dev/null 2>&1; then yum install bind-utils -y; fi
## DOMAIN=mydomain.com
## NSHOSTS=( "$(dig @4.2.2.2 +short MX ${DOMAIN}|sort -n|cut -d' ' -f2)" )
## for NS in ${NSHOSTS[@]}; do printf "%-15s => %-s\n" "$(dig @4.2.2.2 +short A ${NS})" "${NS}"; done
## unset DOMAIN NSHOSTS

UPDATE THE SYSTEM

## screen -U -S mailserver-screen
## yum update

SET-UP SYSTEM USER

create group used for virtual mailboxes

## groupadd vmail -g 2222

create user used for virtual mailboxes

## useradd vmail -r -g 2222 -u 2222 -d /var/vmail -m -c "mail user"

INSTALL POSTFIX

## yum remove exim sendmail
## yum install postfix cronie

edit postfix main.cf configuration file

## cp /etc/postfix/main.cf{,.orig}
## vim /etc/postfix/main.cf
queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
mail_owner = postfix
unknown_local_recipient_reject_code = 550
alias_maps = hash:/etc/postfix/aliases
alias_database = $alias_maps

inet_interfaces = all
inet_protocols = ipv4
mydestination = $myhostname, localhost.$mydomain, localhost

debug_peer_level = 2
debugger_command =
         PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
         ddd $daemon_directory/$process_name $process_id & sleep 5

sendmail_path = /usr/sbin/sendmail.postfix
newaliases_path = /usr/bin/newaliases.postfix
mailq_path = /usr/bin/mailq.postfix
setgid_group = postdrop
html_directory = no
manpage_directory = /usr/share/man
sample_directory = /usr/share/doc/postfix-2.6.6/samples
readme_directory = /usr/share/doc/postfix-2.6.6/README_FILES

relay_domains = *
virtual_alias_maps=hash:/etc/postfix/vmail_aliases
virtual_mailbox_domains=hash:/etc/postfix/vmail_domains
virtual_mailbox_maps=hash:/etc/postfix/vmail_mailbox

virtual_mailbox_base = /var/vmail
virtual_minimum_uid = 2222
virtual_transport = virtual
virtual_uid_maps = static:2222
virtual_gid_maps = static:2222

smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = /var/run/dovecot/auth-client
smtpd_sasl_security_options = noanonymous
smtpd_sasl_tls_security_options = $smtpd_sasl_security_options
smtpd_sasl_local_domain = $mydomain
broken_sasl_auth_clients = yes

smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination

create vmail_domains configuration file. this is where you add your virtual domains.

## vim /etc/postfix/vmail_domains
mydomain.com            OK
my-otherdomain.com      OK

create vmail_mailbox configuration file. this is where you define your mailboxes.

## vim /etc/postfix/vmail_mailbox
info@mydomain.com           mydomain.com/info/
info@my-otherdomain.com     my-otherdomain.com/info/

create vmail_aliases configuration file. this is where you define your virtual aliases.

## vim /etc/postfix/vmail_aliases
info@mydomain.com           info@mydomain.com
info@my-otherdomain.com     foo@bar.tld

hash the configuration files

## postmap /etc/postfix/vmail_domains
## postmap /etc/postfix/vmail_mailbox
## postmap /etc/postfix/vmail_aliases
## touch /etc/postfix/aliases
## vim +/submission /etc/postfix/master.cf
submission inet n       -       n       -       -       smtpd

INSTALL DOVECOT

## yum install dovecot

edit dovecot dovecot.conf configuration file

## cp /etc/dovecot/dovecot.conf{,.orig}
## vim /etc/dovecot/dovecot.conf
listen = *
ssl = no
protocols = imap lmtp
disable_plaintext_auth = no
auth_mechanisms = plain login
mail_access_groups = vmail
default_login_user = vmail
first_valid_uid = 2222
first_valid_gid = 2222
#mail_location = maildir:~/Maildir
mail_location = maildir:/var/vmail/%d/%n

passdb {
    driver = passwd-file
    args = scheme=SHA1 /etc/dovecot/passwd
}
userdb {
    driver = static
    args = uid=2222 gid=2222 home=/var/vmail/%d/%n allow_all_users=yes
}
service auth {
    unix_listener auth-client {
        group = postfix
        mode = 0660
        user = postfix
    }
    user = root
}
service imap-login {
  process_min_avail = 1
  user = vmail
}

create virtual user’s configuration file passwd. this is where usernames and password hashes will be stored.

## touch /etc/dovecot/passwd
## doveadm pw -s sha1 | cut -d '}' -f2

## vim /etc/dovecot/passwd
info@mydomain.com:DOzcsKI8HY0bg8LAuz0DPKwS3WA=
## chown root: /etc/dovecot/passwd
## chmod 600 /etc/dovecot/passwd

START SERVICES

## chkconfig postfix on
## chkconfig dovecot on
## service postfix restart
## service dovecot restart

TEST THE SET-UP

Open your favorite e-mail client and configure it to use the newly created info@mydomain.com account. Try to send/receive an email. If you experience any issues, check if there’s something logged in /var/log/maillog

you can also use swaks to test your smtp server, for example:

swaks --to support@mydomain.com --from email@address.net

more information about swaks you can find at man swaks

ADD ANOTHER ACCOUNT

set-up account’s mailbox

## vim /etc/postfix/vmail_mailbox
...
support@mydomain.com           mydomain.com/support/

set-up account’s alias(es)

## vim /etc/postfix/vmail_aliases
...
support@mydomain.com           support@mydomain.com

postmap configuration files and restart postfix

## postmap /etc/postfix/vmail_mailbox
## postmap /etc/postfix/vmail_aliases
## service postfix restart

generate password hash and add username:password-hash to passwd file.

## doveadm pw -s sha1 | cut -d '}' -f2
## vim /etc/dovecot/passwd
...
support@mydomain.com:DOzcsKI8HY0bg8LAuz0DPKwS3WA=

This is a simple, but yet very robust mail server set-up on a CentOS 6 VPS which supports SMTP and IMAP without SSL, webmail, anti-spam, anti-virus, filter rules, opendkim etc. However, in the next few related articles, we will be adding additional features to the set-up to make it even yet more powerful, so stay tuned.


Of course, if you are one of our Linux VPS Hosting customers, you don’t have to do any of this, simply ask our admins, sit back and relax. Our admins will set this up for you immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

42 Responses to “Mailserver with virtual users and domains using Postfix and Dovecot on a CentOS 6 VPS”

  1. Install and set-up Roundcube webmail interface | RoseHosting.com Linux VPS Hosting Blog

    […] is actually part 2 of our mailserver with virtual users and domains using Postfix and Dovecot on a CentOS 6 VPS […]

    Reply
  2. Set-up SSL encrypted connection in Postfix, Dovecot and Apache | RoseHosting.com Linux VPS Hosting Blog

    […] article is part 3 of our part 1 mailserver set-up with virtual users and domains using Postfix and Dovecot on a CentOS 6 VPS and part 2 which explains how to install and set-up the Roundcube webmail […]

    Reply
  3. How to install and integrate SpamAssassin with Postfix on a CentOS 6 VPS | RoseHosting.com Linux VPS Hosting Blog

    […] following article is part 4 of our mailserver set-up with virtual users and domains using Postfix and Dovecot on a CentOS 6 VPS, followed by part 2 which explains how to install and set-up the Roundcube […]

    Reply
  4. How to set-up server-side email filtering with Dovecot Sieve and Roundcube on a CentOS 6 VPS | RoseHosting.com Linux VPS Hosting Blog

    […] following article is part 6 of the mailserver set-up with virtual users and domains using Postfix and Dovecot series. It covers the steps used to set-up server-side email filtering with Dovecot Sieve and […]

    Reply
  5. How to install and integrate OpenDKIM with Postfix on a CentOS 6 VPS | RoseHosting.com Linux VPS Hosting Blog

    […] tutorial is part 5 of the mailserver set-up with virtual users and domains using Postfix and Dovecot series. It goes through the steps of installing and integrating OpenDKIM in Postfix on a CentOS 6 […]

    Reply
  6. matt

    Please add extra details to install postfix,dovecot,roundcube on ningx+wordpress+w3total cache article
    Thanks!

    Reply
  7. Rodrigo Gliksberg

    Thanks it works!!!

    Reply
  8. mayasl

    My configuration details

    /etc/postfix/vmail_domains
    indianrockers.net OK

    /etc/postfix/vmail_mailbox
    webmaster@indianrockers.net indianrockers.net/webmaster/

    /etc/postfix/vmail_aliases
    webmaster@indianrockers.net webmaster@indianrockers.net

    When I executed “doveadm pw -s sha1 | cut -d ‘}’ -f2″ I was asked for a password. I entered a password and confirmed it. Then I got a hash value.
    I opened /etc/dovecot/passwd and entered as follows.
    webmaster@indianrockers.net:THEHASHVALUEIGOT

    In Thunderbird,
    Your name : IndianRockers
    Email address : webmaster@indianrockers.net
    Password : Which I provided before getting the hash value.

    Error : Configuration could not be verified – is the username or password wrong?

    What am I doing wrong?

    Maillog

    Mar 2 02:14:09 indianrockers dovecot: imap-login: Login: user=, method=PLAIN, rip=***.***.***.***, lip=107.150.12.23, mpid=4262
    Mar 2 02:14:09 indianrockers dovecot: imap(webmaster@indianrockers.net): Error: chdir(/var/vmail/indianrockers.net/webmaster/) failed: Permission denied (euid=2222(<u$
    Mar 2 02:14:09 indianrockers dovecot: imap(webmaster@indianrockers.net): Error: chdir(/var/vmail/indianrockers.net/webmaster) failed: Permission denied
    Mar 2 02:14:09 indianrockers dovecot: imap(webmaster@indianrockers.net): Error: user webmaster@indianrockers.net: Initialization failed: Initializing mail storage fro$
    Mar 2 02:14:09 indianrockers dovecot: imap(webmaster@indianrockers.net): Error: Invalid user settings. Refer to server log for more information.

    Reply
    • admin

      Could you please post the output of below commands:

      # id vmail
      # ls -dl /var/vmail
      # ls -dl /var/vmail/vmail1
      # dovecot -n

      Reply
      • simon

        Hi, I am getting the same error as @mayasl mentioned. Here is what you asked for.

        #id vmail
        uid=2222(vmail) gid=2222(vmail) groups=2222(vmail)

        #ls -dl /var/vmail
        drwx——. 2 vmail vmail 4096 Apr 20 22:01 /var/vmail

        #ls -dl /var/vmail/vmail1
        ls: cannot access /var/vmail/vmail1: No such file or directory

        #dovecot -n
        # 2.0.9: /etc/dovecot/dovecot.conf
        # OS: Linux 2.6.32-431.11.2.el6.i686 i686 CentOS release 6.5 (Final) ext4
        auth_mechanisms = plain login
        default_login_user = vmail
        disable_plaintext_auth = no
        first_valid_gid = 2222
        first_valid_uid = 2222
        listen = *
        mail_access_groups = vmail
        mail_location = maildir:/var/vmail/%d/%n
        passdb {
        args = scheme=SHA1 /etc/dovecot/passwd
        driver = passwd-file
        }
        protocols = imap lmtp
        service auth {
        unix_listener auth-client {
        group = postfix
        mode = 0660
        user = postfix
        }
        user = root
        }
        service imap-login {
        process_min_avail = 1
        user = vmail
        }
        ssl = no
        userdb {
        args = uid=2222 gid=2222 home=/var/vmail/%d/%n allow_all_users=yes
        driver = static
        }

        Can you please help me. I have no idea where It went wrong

        Reply
        • Matthew

          Using Centos 6.5, postfix 2.6.6, dovecot 2.0.9 I need to add the following into dovecot.conf

          service auth {

          unix_listener auth-client {
          group = postfix
          mode = 0660
          user = postfix
          }


          unix_listener auth-master {
          group = vmail
          mode = 0660
          user = vmail
          }

          }

          Reply
  9. Jeff

    Followed Part 1 to 3 of this blog. Can’t seem to login via roundcube. Always get ‘Login failed’ error. Complete Linux newbie, so please advise how to troubleshoot/investigate further. Thank you.

    Reply
    • admin

      Make sure you complete all the steps correctly.

      Also, you could enable imap_debug in the main.inc.php and post the log of an attempted login.

      Reply
  10. Jeff

    Found out what was wrong. I deviated a little from the steps above. I put my custom config of dovecot in local.conf instead of dovecot.conf but forgot to uncomment the last line of dovecot.conf which includes configuration in local.conf.

    I have another question regarding roundcube webmail that I hope you can assist with. I deviated the configuration of postfix/dovecot a little (based on https://library.linode.com/email/postfix/postfix2.9.6-dovecot2.0.19-mysql) to store virtual domains, virtual accounts, passwords and virtual aliases in MySQL database. I understand roundcube webmail comes with password plugin that can allow user to change their passwords via the roundcube webmail interface and that it supports accounts/passwords stored in MySQL. Just not sure how to configure the plugin to fit with the MySQL schema as listed in the linode article.

    Reply
  11. Steen

    This guide is very sweet and it makes it very easy to get a server going.
    I have both an issue and a concern though.
    First of all: “relay_domains = *” sounds like a terribly bad idea, but I’m not really sure, I guess it won’t matter when login is required as specified by “auth_mechanisms = plain login”

    Now my issue which has nothing to do with the above:
    postfix expects /var/vmail/%d/%u to be a file and dovecot sets it up as a directory. What do I do here? Postfix can’t deliver e-mail because %d/%u is a directory and not a file. If I delete it and create it the mail is delivered but I cannot log in to the IMAP server.

    Reply
    • admin

      About “relay_domains = *”; You need to add the list of the domains your server will relay mail to, e.g:

      relay_domains = your-domain1.com, your-domain2.com

      As for the ‘mail_location’ problem, can you please provide us with your ‘/etc/dovecot/dovecot.conf’ Dovecot configuration file?

      Reply
  12. kavitama

    Did part 1
    Went fine but cannot check mail server connection since i get the following:
    “Log onto incoming mail server (POP3): Outlook cannot connect to your incoming (POP3) e-mail server. If you continue to receive this message, contact your server administrator or Internet service provider (ISP).”

    cat /var/log/maillog gives this:
    warning: maildir access problem for UID/GID=2222/2222: create maildir file /var/vmail/prosyde.net/info/tmp/1394210401.P2283.prosyde-mail: Permission denied

    checked /var/vmail and it is ok
    drwxrwxrwx. 2 vmail vmail 4096 Mar 7 11:47 /var/vmail

    What did i do wrong?

    Regards

    Reply
    • admin

      Something is not configured OK. Re-check your dovecot and postfix configuration files and make sure they use UID/GID 2222 to manage the mailboxes in /var/vmail

      Reply
  13. Simon

    I think my previous comment didn’t go through. I have done all you have asked but when I try to log in using thunderbird, I get an error saying: “error : Configuration could not be verified – is the username or password wrong?”

    What could be the problem? Are you sure the password hashing is correct?

    Reply
    • admin

      Do you see any error messages in your mail logs?

      Reply
      • Simon

        Yes. Here is the error I get.
        http://pastebin.com/Uc9MhqAD

        Can you please help me? I am trying your tutorial on a fresh install VPS, running CentOS 6.5, 32but, Minimal, running postfix, devcot. Simply, server and A record and MX record wise everything is going smoothly.

        Reply
        • admin

          Please check your ‘/var/vmail’ directory permissions, or provide us with the output of the following command:
          ls -al /var/vmail

          Reply
          • Simon

            Ok, I reinstalled my VPS for the 4th time, and followed again your tutorials, still the same error for one week. Here is the output for `ls -al /var/vmail`

            [root@simon /]# ls -al /var/vmail
            total 20
            drwxrwxrwx. 2 vmail vmail 4096 Apr 23 16:28 .
            drwxr-xr-x. 18 root root 4096 Apr 23 16:28 ..
            -rwxrwxrwx. 1 vmail vmail 18 Jul 18 2013 .bash_logout
            -rwxrwxrwx. 1 vmail vmail 176 Jul 18 2013 .bash_profile
            -rwxrwxrwx. 1 vmail vmail 124 Jul 18 2013 .bashrc
            [root@simon /]#

            I’m convinced this method does NOT work for CentOs 6.5. I wish I had known that a week ago. Please include that in your tutorial to save some people time, and yourself same questions. I will check back one more time.
            Thanks for the reply.

          • admin

            We double-checked the tutorial and can confirm that it is working on CentOS 6.5. These are the correct permissions:

            # ls -al /var/vmail/
            total 24
            drwx—— 3 vmail vmail 4096 Apr 24 04:24 .
            drwxr-xr-x 22 root root 4096 Apr 24 04:09 ..
            -rw-r–r– 1 vmail vmail 18 Jul 18 2013 .bash_logout
            -rw-r–r– 1 vmail vmail 176 Jul 18 2013 .bash_profile
            -rw-r–r– 1 vmail vmail 124 Jul 18 2013 .bashrc
            drwx—— 3 vmail vmail 4096 Apr 24 04:24 mydomain.com

            Please read the tutorial very carefully and make sure you follow the simple instructions. Of course, you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to install this for you.

            Thanks.

  14. Muhammed Thaha

    Apr 27 19:11:37 abc postfix/virtual[478563]: 330822209AE: to=, relay=virtual, delay=0.92, delays=0.81/0.01/0/0.1, dsn=2.0.0, status=sent (delivered to mailbox)

    postfix delivering but not showing while connected via pop3

    Reply
  15. Simon

    It finally worked. Here is the trick. Don’t create e-mail account with the same name as one described in MYDOMAIN=

    If your MYDOMAIN=foobartar.com
    then creating any-email@foobartar.com will cause some problems.

    Reply
  16. simon

    Why is it that If I use DOMAIN=web101.com then I won’t be able to receive any email created for that domain? Do you have a solution for this? With other domains everything works, but not for the domain mentioned. I would love to know a work-around for this.

    Thanks.

    Reply
    • admin

      Hi, simon.

      Unfortunately, we are not sure what exactly are you talking about? How did you mean if you use web101.com you are not able to receive emails? What error message did you get? Did you check your mailserver log file? Are you sure you have your MX records set-up correctly?

      You need to provide more information and relevant sections from your log files so we can help.

      Reply
      • Simon

        In your tutorial above, you have wrote a line where it says

        # DOMAIN=mydomain.com

        I substituted the above with my domain name. Let just say my domain is web101.com. So, according to your instructions, my I set

        # DOMAIN=web101.com

        So, now the problem is that I created an email for that domain. ex: info@web101.com, contact@web101.com … in the same way I created for other domains. But, all those emails associated for the domain web101.com do not work. Including those I mentioned.

        Reply
        • admin

          Everything is OK with the tutorial and you may be doing something wrong. We do not know what exactly you’ve done and how you configured your services. Please feel free to sign-up for one of our VPS hosting plans at http://www.rosehosting.com/linux-vps-hosting.html and some of our admins will install and set-up a mailserver for you.

          Reply
  17. Victor

    First thank you for writing this awesome tutorial.
    I have followed the steps, and most of it seems to work fine. I have sent an email to my address and it appears on my maillog without error. My only problem is nothing is generated in the actual virtual_mailbox_base folder, it stays empty. my postfix config
    below is what appeared in my log
    May 22 05:54:45 styluxinc postfix/smtpd[20782]: A9E2274B81: client=mail-oa0-f54.google.com[209.85.219.54]
    May 22 05:54:45 styluxinc postfix/cleanup[20786]: A9E2274B81: message-id=
    May 22 05:54:45 styluxinc postfix/qmgr[20777]: A9E2274B81: from=, size=2379, nrcpt=1 (queue active)
    May 22 05:54:45 styluxinc postfix/local[20787]: A9E2274B81: to=, relay=local, delay=0.14, delays=0.13/0.01/0/0, dsn=2.0.0, status=sent (delivered to mailbox)
    May 22 05:54:45 styluxinc postfix/qmgr[20777]: A9E2274B81: removed

    Reply
  18. Cesar

    Hey!

    Can I pls get some help regarding MX records?

    I don´t know how to configure it, should I put the MX records in both zone files or just one of them?
    And what should I type?

    I know it should be something like:

    name IN MX [Preference number] host

    But what should be in the name column and what should be in the host column, PLS HELP!! Soooo confused :(

    Reply
    • admin

      You need to edit the forward zone containing authoritative information for your domain.

      name – usually it is your domain name (this name appears after the ‘@’ in an email address).
      host – a hostname or FQDN (not IP address) of a mail server that will accept mail.

      Please note, the DNS specification (RFC 2181) does not allow for an MX record to be a CNAME.

      Reply
  19. Shivanand

    Hi ..!!
    i have installed/configured postfix mail server and now i want to send mass mails, is there option to break those bulk mails into single mails and queue 1000 recipients to one after the other ?

    Reply
  20. oleg

    Hi!

    First of all – this is really great tutorial. I’ve been able to install everything without any problems.
    I can login via imap to my email account, but I can’t receive any email. I think i have the same problem as Steen had. In my error log it says:
    delivery failed to mailbox /var/vmail/testdomain.pl/test: cannot open file: Is a directory

    How to fix this error?

    Reply
    • admin

      Please execute the following commands on your server and provide us with the output:
      cat /etc/postfix/vmail_mailbox
      ls -al /var/vmail/testdomain.pl/

      Reply
      • LateNiter

        This problem will occur if you miss the trailing slash for a directory name in
        /etc/postfix/vmail_mailbox

        Reply
  21. JimmY_BL

    I`m using CentOS 6.4 x64 and I just finished with tutorial – from one with installing LAMP, thru this one.
    All worked fine except one little change in config file of postfix (maybe new version or something).
    I sent test mail and received it in inbox on yahoo (not going in SPAM folder – YAY, my MX record is working great), but when I did reply – mail delivery system returned mail to yahoo saying: unknow user: xxxx …
    Then I just changed one line in /etc/postfix/main.cf :
    mydestination = localhost
    and it`s working great xD

    Thx for making this wonderfull tutorial, I will bookmark it for life! Cheers!!!

    Reply
  22. simon

    All works. Thanks. But why does every message I send to any domain other than my own, gets sent to spam folder?

    Reply

Leave a Reply