How to Configure Postfix, Dovecot, Virtual Domains, Virtual Users in CentOS

Step 1: Install Postfix

Install postfix using yum command:

# yum install postfix

Step 2: Run Postfix on Multi-Levels

Make sure run the postfix on multilevels:

# chkconfig postfix on

Step 3: Modify /etc/postfix/main.cf File

Make a copy of both these files main.cf & master.cf:

# cd /etc/postfix
# cp main.cf main.cf.bk
# cp master.cf master.cf.bk

Edit main.cf and change the following values:

# vi /etc/postfix/main.cf
myhostname = www.tecdistro.com
mydomain = tecdistro.com
myorigin = $mydomain
inet_interfaces = all
home_mailbox = Maildir/

Step 4: Create Postfix User

Create postfix user “userpostfix” on server with login shell as /sbin/nologin

# useradd -s /sbin/nologin userpostfix

Check the UID:GUID for the userpostfix user in /etc/passwd file

# tail /etc/passwd
userpostfix:x:1001:1001::/home/userpostfix:/sbin/nologin

Step 5: Configure Virtual domain

Add the following lines at the end of main.cf and replace the UID:GUID under virtual_minimum_uid, virtual_maximum_uid, virtual_uid_maps and virtual_gid_maps

# vi /etc/postfix/main.cf
virtual_mailbox_domains = /etc/postfix/virtual_domains
virtual_mailbox_base = /var/mail/vhosts
virtual_mailbox_maps = hash:/etc/postfix/vmailbox
virtual_minimum_uid = 1001
virtual_maximum_uid = 1001
virtual_uid_maps = static:1001
virtual_gid_maps = static:1001
virtual_alias_maps = hash:/etc/postfix/virtual

Create /etc/postfix/virtual_domains. It contains all the domains that are pointed on server.

# vi /etc/postfix/virtual_domains
tecdistro1.com
tecdistro2.net
tecdistro3.org

Step 6: Create Mail Directories

Create the mail directory, sub directory for the domains and assign the proper permissions.

# mkdir /var/mail/vhosts
# chgrp -R userpostfix /var/mail
# cd /var/mail/vhosts
# mkdir tecdistro1.com
# mkdir tecdistro2.net
# mkdir tecdistro3.org
# cd ..
# chown -R userpostfix:userpostfix vhosts

Note: Postfix will create the “Maildir” directories automatically and assign the proper permissions.

Step 7: Create /etc/postfix/vmailbox File

Create a file /etc/postfix/vmailbox and add all of the users that will receive e-mails.

# touch /etc/postfix/vmailbox
# vi /etc/postfix/vmailbox
@tecdistro1.com          tecdistro1.com/catch-all/
user1@tecdistro1.com        tecdistro1.com/user1/
user2@tecdistro1.com       tecdistro1.com/user2/
user1@tecdistro2.net        tecdistro2.net/user1/
user1@tecdistro3.org        tecdistro3.org/user1/

Note: Make sure you end up each line with “/”.
Postfix will automatically create Maildir structure (cur, new, tmp).

Step 7: Execute postmap Command

Create the hashed file (.db) using postmap command.

	
# postmap /etc/postfix/virtual
# postmap /etc/postfix/vmailbox

Step 8: Check postfix Status

Make sure postfix service is running fine and port 25 is opened for postfix.

# ps -eaf | grep postfix
postfix   1219  1590  0 12:05 ?        00:00:00 pickup -l -t fifo -u
root      1353 30585  0 12:06 pts/0    00:00:00 grep postfix
root      1590     1  0 Mar05 ?        00:01:03 /usr/libexec/postfix/master
postfix  23181  1590  0 Apr13 ?        00:00:07 qmgr -l -t fifo -u
# netstat -an | grep :25
tcp        0      0 0.0.0.0:25                  0.0.0.0:*                   LISTEN
tcp        0      0 :::25                       :::*                        LISTEN
tcp        0      0 ::ffff:159.8.18.154:80      ::ffff:157.55.39.57:25485   TIME_WAIT

Step 9: Reload postfix

Now reload the postfix service:

# service postfix reload

Step 10: Test Postfix

Test the mail using telnet command:

# telnet localhost smtp
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 server.tecdistro.com ESMTP Postfix
ehlo localhost
250-server.tecdistro.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
mail from:support@tecdistro.com
250 2.1.0 Ok
rcpt to:sales@tecdistro.com
250 2.1.5 Ok
data
354 End data with .
Test Mail
.
250 2.0.0 Ok: queued as E7E1F5A1F6
quit
221 2.0.0 Bye
Connection closed by foreign host.

How to Configure Dovecot

Dovecot is an open-source POP and IMAP client.

Step 1: Install Dovecot

Install dovecot using yum command:

# yum install dovecot

Step 2: Configure Dovecot on Multi-Levels

To Configure dovecot on multilevels:

# chkconfig dovecot on

Step 3: Configure Dovecot Files

Then, go to conf.d folder and change the following lines in the following files.

# cd /etc/dovecot/
# vi dovecot.conf
protocols = imap pop3
# cd /etc/dovecot/conf.d/
# vi 10-auth.conf
disable_plaintext_auth = no
#!include auth-system.conf.ext
!include auth-passwdfile.conf.ext
# vi 10-logging.conf
log_path = /var/log/dovecot.log
auth_verbose = no
auth_debug = no
verbose_ssl = no
# vi 10-mail.conf
mail_location = maildir:/var/mail/vhosts/%d/%n
mail_uid = 1001
mail_gid = 1001
mail_privileged_group = userpostfix
# vi 10-master.conf
unix_listener auth-userdb {
  mode = 0600
  user = postfix
  group =  postfix
}
# Postfix smtp-auth
unix_listener /var/spool/postfix/private/auth {
  mode = 0666
  user = postfix
  group = postfix
}

Step 4: Reload Dovecot

Now reload the dovecot service:

 # service dovecot reload

Step 5: Test Dovecot

# telnet localhost pop3
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
+OK Dovecot ready.
user sales@tecdistro.com
+OK
pass Test123
+OK Logged in.
list
+OK 1 messages:
1 3845
.
retr 1
+OK 3845 octets
Return-Path: <support@tecdistro.com>
X-Original-To: user1
Delivered-To: support@tecdistro.com
Received: from localhost (localhost [IPv6:::1])
by server.tecdistro.com (Postfix) with ESMTP id 117113FF18
for ; Thu, 7 Feb 2013 17:05:32 +0530 (IST)
Message-Id: <20130207113547.117113FF18@server.tecdistro.com>
Date: Thu, 7 Feb 2013 17:05:32 +0530 (IST)
From: support@tecdistro.com
To: undisclosed-recipients:;

.
test
quit
+OK Logging out.
Connection closed by foreign host.

How to Configure Squirrelmail

Step 1: Prerequisites

Step 2: Install Squirrelmail

Install EPEL repository and install SquirrelMail package from EPEL repository.

# wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
# rpm -ivh epel-release-6-8.noarch.rpm 
# yum install squirrelmail
# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

Step 3: Configure Squirrelmail

Configure conf.pl according to the server as following:

# cd /usr/share/squirrelmail/config/
# ./conf.pl 
SquirrelMail Configuration : Read: config.php (1.4.0)
---------------------------------------------------------
Main Menu --
1.  Organization Preferences
2.  Server Settings
3.  Folder Defaults
4.  General Options
5.  Themes
6.  Address Books
7.  Message of the Day (MOTD)
8.  Plugins
9.  Database
10. Languages
D.  Set pre-defined settings for specific IMAP servers
C   Turn color off
S   Save data
Q   Quit
Command >>2

SquirrelMail Configuration : Read: config.php (1.4.0)
---------------------------------------------------------
Server Settings
General
-------
1.  Domain                 : tecdistro.com
2.  Invert Time            : false
3.  Sendmail or SMTP       : SMTP
A.  Update IMAP Settings   : localhost:143 (uw)
B.  Update SMTP Settings   : localhost:25
R   Return to Main Menu
C   Turn color off
S   Save data
Q   Quit
Command >> S

Press S to save datas and press Q to exit.

Step 4: Configure Squirrelmail with httpd.conf

Add the following lines in the httpd.conf file at the end.

# vi /etc/httpd/conf/httpd.conf
Alias /squirrelmail /usr/share/squirrelmail
<Directory /usr/share/squirrelmail>
    Options Indexes FollowSymLinks
    RewriteEngine On
    AllowOverride All
    DirectoryIndex index.php
    Order allow,deny
    Allow from all
</Directory>

Restart the httpd service.

# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
6 replies
  1. Avatar
    Francis Carayol says:

    This is a great write-up. I followed your tutorial and installed Postfix and Dovecot, Thank you. I just have one question, how do you create passwords for the email accounts? I noticed you have a password on the telnet test but I seem to have missed how you created the password.

    Reply
  2. Avatar
    Elijah says:

    You can definitely see your enthusiasm in the article
    you write. The world hopes for even more
    passionate writers like you who aren’t afraid to say how they believe.
    All the time follow your heart.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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

nine − 1 =