Professional Documents
Culture Documents
( postfix-dovecot )
This article describes how to setup a mail server in centos 6 . Here i have used three popular
packages which is very easy to configure .
» Postfix ( for sending )
» Dovecot ( for receiving )
» Squirrelmail ( for webmail access )
This guide is strictly for learning purpose , not for real time servers .
Let’s start
Step 1 »Before installation assign a static ip and add a host entry for your domain to that
IP in the /etc/hosts file like below.
10.1.125.5 mail.ditfrek.postel.go.id
Step 3 » Now issue the below command to install SMTP AUTH packages .
Step 4 » Issue the below commands one by one for creating SSL Cert.
[root@mail ~]# mkdir /etc/postfix/ssl
[root@mail ~]# cd /etc/postfix/ssl/
[root@mail ssl]# openssl genrsa -des3 -rand /etc/hosts -out smtpd.key 1024
[root@mail ssl]# chmod 600 smtpd.key
[root@mail ssl]# openssl req -new -key smtpd.key -out smtpd.csr
[root@mail ssl]# openssl x509 -req -days 365 -in smtpd.csr -signkey smtpd.key -out smtpd.crt
[root@mail ssl]# openssl rsa -in smtpd.key -out smtpd.key.unencrypted
[root@mail ssl]# mv -f smtpd.key.unencrypted smtpd.key
[root@mail ssl]# openssl req -new -x509 -extensions v3_ca -keyout cakey.pem -out cacert.pem
-days 365
# KONFIGURASI TAMBAHAN
# TAUFIK.RAHMAN@SIGMA.CO.ID
#################################
myhostname = mail.ditfrek.postel.go.id
#mydomain = mail.ditfrek.postel.go.id
mydestination =
inet_interfaces = all
message_size_limit = 50720000
virtual_alias_domains =
virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains.cf
virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf
virtual_mailbox_base = /home/vmail
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
broken_sasl_auth_clients = yes
smtpd_sasl_authenticated_header = yes
smtpd_use_tls = yes
smtpd_tls_key_file = /etc/postfix/ssl/server.key
smtpd_tls_cert_file = /etc/postfix/ssl/server.crt
#smtpd_tls_CAfile = /etc/postfix/ssl/cacert.pem
#smtpd_tls_cert_file = </etc/pki/dovecot/certs/dovecot.pem
#smtpd_tls_key_file = </etc/pki/dovecot/private/dovecot.pem
virtual_create_maildirsize = yes
virtual_maildir_extended = yes
virtual_transport = dovecot
dovecot_destination_recipient_limit = 1
debug_peer_level = 5
debug_peer_list = 127.0.0.1
Step 5 » Now open /etc/postfix/master.cf file and add the below line after smtp
» Issue the below commands to start the postfix and saslauthd at startup
[root@mail ~]# chkconfig --level 235 postfix on
Step 7 » Now check your smtp connectivity . just telnet localhost on port 25 and type this
command ehlo localhost
Step 9 » After installation open /etc/dovecot/dovecot.conf file and add the below line at
the end of the file. please make sure mail_location and home_mailbox in postfix
configuration are using the same name.
protocols = imap pop3
mail_location = maildir:~/mail
pop3_uidl_format = %08Xu%08Xv
Step 12 » Test your configuration in thunderbird . Refer the below image for
configuration details.
Mail server centos 6 testing
Step 13 » Now you can send and receive mails using this server. In case of any issues
please check the log file /var/log/maillog )
This command will install squirrelmail along with apache and php.
Step 19 » Now open http://serverip/webmail path in your browser . you could able to see
the below page .
setup mail server postfix-dovecot-squirrelmail
and after login
setup mail server postfix-dovecot-squirrelmail
yum update
1
2. The version of Postfix included in the main CentOS repository does not include
support for MySQL; therefore, you will need install Postfix from the CentOS Plus
repository. Before doing so, add exclusions to
the [base] and [updates] repositories for the Postfix package to prevent it from
being overwritten with updates that do not have MySQL support:
/etc/yum.repos.d/CentOS-Base.repo
[base]
1 name=CentOS-$releasever - Base
2 exclude=postfix
3
4 #released updates
5 [updates]
6 name=CentOS-$releasever - Updates
7 exclude=postfix
8
4. This installs the Postfix mail server, the MySQL database server, the Dovecot
IMAP and POP daemons, and several supporting packages that provide services
related to authentication.
Next, set up a MySQL database to handle virtual domains and users.
chkconfig mysqld on
1 service mysqld start
2
2. Run mysql_secure_installation . You will be presented with the opportunity to change the
MySQL root password, remove anonymous user accounts, disable root logins outside of
localhost, remove test databases, and reload privilege tables. It is recommended that you
answer yes to these options:
mysql_secure_installation
1
mysql -u root -p
1
CREATE TABLE users (email varchar(80) NOT NULL, password varchar(20) NOT
1 NULL, PRIMARY KEY (email) );
quit
1
11. Bind MySQL to localhost (127.0.0.1) by editing /etc/my.cnf , and adding the
following to the [mysqld] section of the file:
/etc/my.cnf
bind-address=127.0.0.1
1
This is required for Postfix to be able to communicate with the database server. If
you have MySQL set up to listen on another IP address (such as an internal IP),
you will need to substitute this IP address in place of 127.0.0.1 during the Postfix
configuration steps. It is notadvisable to run MySQL on a publicly-accessible IP
address.
12. Restart the database server:
user = mail_admin
1 password = mail_admin_password
2 dbname = mail
3 query = SELECT domain AS virtual FROM domains WHERE domain='%s'
4 hosts = 127.0.0.1
5
user = mail_admin
1 password = mail_admin_password
2 dbname = mail
3 query = SELECT destination FROM forwardings WHERE source='%s'
4 hosts = 127.0.0.1
5
1 user = mail_admin
2 password = mail_admin_password
3 dbname = mail
4 query = SELECT CONCAT(SUBSTRING_INDEX(email,<'@'>,-
5 1),'/',SUBSTRING_INDEX(email,<'@'>,1),'/') FROM users WHERE email='%s'
hosts = 127.0.0.1
4. Create a virtual email mapping file for Postfix called /etc/postfix/mysql-
virtual_email2email.cf :
/etc/postfix/mysql-virtual_email2email.cf
user = mail_admin
1 password = mail_admin_password
2 dbname = mail
3 query = SELECT email FROM users WHERE email='%s'
4 hosts = 127.0.0.1
5
chmod o= /etc/postfix/mysql-virtual_*.cf
1 chgrp postfix /etc/postfix/mysql-virtual_*.cf
2
6. Create a user and group for mail handling. All virtual mailboxes will be stored under this
user’s home directory:
7. Complete the remaining steps required for Postfix configuration. Please be sure
to replace server.example.com with the Linode’s fully qualified domain name. If you
are planning on using your own SSL certificate and key,
replace /etc/pki/dovecot/private/dovecot.pem with the appropriate path:
1 postconf -e 'myhostname = server.example.com'
2 postconf -e 'mydestination = $myhostname, localhost,
3 localhost.localdomain'
4 postconf -e 'mynetworks = 127.0.0.0/8'
5 postconf -e 'inet_interfaces = all'
6 postconf -e 'message_size_limit = 30720000'
7 postconf -e 'virtual_alias_domains ='
8 postconf -e 'virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-
9 virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf'
10 postconf -e 'virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-
11 virtual_domains.cf'
12 postconf -e 'virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-
13 virtual_mailboxes.cf'
14 postconf -e 'virtual_mailbox_base = /home/vmail'
15 postconf -e 'virtual_uid_maps = static:5000'
16 postconf -e 'virtual_gid_maps = static:5000'
postconf -e 'smtpd_sasl_type = dovecot'
postconf -e 'smtpd_sasl_path = private/auth'
postconf -e 'smtpd_sasl_auth_enable = yes'
postconf -e 'broken_sasl_auth_clients = yes'
postconf -e 'smtpd_sasl_authenticated_header = yes'
17 postconf -e 'smtpd_recipient_restrictions = permit_mynetworks,
18 permit_sasl_authenticated, reject_unauth_destination'
19 postconf -e 'smtpd_use_tls = yes'
20 postconf -e 'smtpd_tls_cert_file = </etc/pki/dovecot/certs/dovecot.pem'
21 postconf -e 'smtpd_tls_key_file = </etc/pki/dovecot/private/dovecot.pem'
22 postconf -e 'virtual_create_maildirsize = yes'
23 postconf -e 'virtual_maildir_extended = yes'
24 postconf -e 'proxy_read_maps = $local_recipient_maps $mydestination
25 $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps
26 $virtual_mailbox_domains $relay_recipient_maps $relay_domains
$canonical_maps $sender_canonical_maps $recipient_canonical_maps
$relocated_maps $transport_maps $mynetworks $virtual_mailbox_limit_maps'
postconf -e 'virtual_transport = dovecot'
postconf -e 'dovecot_destination_recipient_limit = 1'
8. Edit the file /etc/postfix/master.cf and add the Dovecot service to the bottom
of the file:
/etc/postfix/master.cf
9. Configure Postfix to start on boot and start the service for the first time:
chkconfig postfix on
1 service postfix start
2
Configure Dovecot
1. Move /etc/dovecot/dovecot.conf to a backup file:
mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf-backup
1
2. Copy the following into the now-empty dovecot.conf file, substituting your
system’s domain name for example.com in line 37:
/etc/dovecot/dovecot.conf
driver = mysql
connect = host=127.0.0.1 dbname=mail user=mail_admin
1 password=mail_admin_password
2 default_pass_scheme = CRYPT
3 password_query = SELECT email as user, password FROM users WHERE
4 email='%u';
5. Configure Dovecot to start on boot, and start it for the first time:
chkconfig dovecot on
1 service dovecot start
2
6. Check /var/log/maillog to make sure Dovecot started without errors. Your log
should have lines similar to the following:
/var/log/maillog
Trying 127.0.0.1...
1 Connected to localhost.localdomain.
2 Escape character is '^]'.
3 +OK Dovecot ready.
4
9. Enter the command quit to return to your shell. This completes the Dovecot
configuration. Next, you’ll make sure aliases are configured properly.
Configure Mail Aliases
1. Edit the file /etc/aliases , making sure the postmaster and root directives are
set properly for your organization:
/etc/aliases
postmaster: root
1 root: postmaster@example.com
2
newaliases
1 service postfix restart
2
This completes alias configuration. Next, test Postfix to make sure it’s operating
properly.
Testing Postfix
1. Test Postfix for SMTP-AUTH and TLS:
telnet localhost 25
1
1ehlo localhost
3. You should see output similar to the following:
250-hostname.example.com
1 250-PIPELINING
2 250-SIZE 30720000
3 250-VRFY
4 250-ETRN
5 250-STARTTLS
6 250-AUTH PLAIN
7 250-AUTH=PLAIN
8 250-ENHANCEDSTATUSCODES
9 250-8BITMIME
10 250 DSN
11
mysql -u root -p
1
2. Switch to the mail database, add support for your domain, and create an email
account. Be sure to replace example.com with your domain
name, sales@example.com with your chosen email, and password with a strong
password:
1 USE mail;
2 INSERT INTO domains (domain) VALUES ('example.com');
3 INSERT INTO users (email, password) VALUES ('sales@example.com',
4 ENCRYPT('password'));
quit
3. Prior to accessing any newly-created email account, a test message needs to be sent to
create that user’s mailbox:
4. Press Ctrl+D to complete the message. You can safely leave the field
for Cc: blank. This completes the configuration for a new domain and email user.
Given the possibility for virtual hosting a large number of domains on a single mail
system, the username portion of an email address (i.e. before the @ sign) is not
sufficient for authentication. When email users authenticate to the server, they must
supply the entire email address created above as their username.
1. Check the maillog located in /var/log/maillog . You should see something
similar to the following:
/var/log/maillog
Now you can test to see what the users of your email server would see with their email
clients.
Test the Mailbox
1. To test the sales@example.com mailbox, navigate to the mailbox
directory /home/vmail/example.com/sales/Maildir and issue the following command:
cd /home/vmail/example.com/sales/Maildir
1 find
2
.
1 ./dovecot-uidlist
2 ./cur
3 ./new
4 ./new/1285609582.P6115Q0M368794.li172-137
5 ./dovecot.index
6 ./dovecot.index.log
7 ./tmp
8
3. Test the maillbox by using a mail client. For this test, using mutt is
recommended. If it is not installed by default, install it with yum install mutt , then
run:
mutt -f .
1
4. You may be prompted to create the root mailbox. This is not required.
5. If there is an email in the inbox, Postfix, Dovecot, and MySQL have been
successfully configured! To quit mutt press q .