Pages

Configuring Postfix and dovecot POP3 and IMAP on Red Hat or Centos using local system accounts


Postfix is one of the most widely deployed mail servers. In this tutorial, I will first show you how to configure Postfix for local system accounts and then I will describe how to configure Dovecot for POP and IMAP servers so remote users can retrieve their emails using email clients like Mozilla Thunderbird, Evolution or Microsoft Outlook etc. This howto is written for Red Hat, Centos or Fedora like distributions but can be applied to any other distributions too.

Example domain used in this how-to is linuxgravity.com, please replace it with your own domain name.
First thing we need to do is to install Postfix. On Red Hat/Centos/Fedora etc, install it like below
yum -y install postfix
After installing remove original main.cf
rm -f /etc/postfix/main.cf
Now create main.cf like below
nano /etc/postfox/main.cf
and paste (ctrl+shift+v) the following:
myhostname = mail.linuxgravity.com
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
Save it by pressing ctrl+x, Enter and then y.


Let me explain what the above means.
myhostname is the name we give to this mail server. Our DNS entry must have mail.linuxgravity.com as mx record to be able to send and receive emails. Check my earlier tutorial Configuring Bind9 Domain name server on Centos or Red Hat on how to do that. myhostname is the basis for other configuration parameters such as mydomain name which is the internet domain this mail server. Postfix will remove the first part from myhostname (mail in this case) and set to mydomain i.e. it will become mydomain=linuxgravity.com.
inet_interfaces = all will make Postfix listen on all network interfaces. Default is to listen on local interface only.
mydestination sets all the domains that this server will accept emails for. Here we are accepting emails for mail.linuxgravity.com, localhost.linuxgravity.com, localhost and linuxgravity.com. In main.cf, you can use an earlier defined parameter by putting $ before it.
That’s it. Now we can test if emails are sent and received properly. On local system, add a user ‘user1′
useradd user1
Set password for user1
passwd user1
Send an email to user1 from root
echo "This is a test email" | mail -s "test email" user1
switch to user1
su - user1
and check email with mail command
mail
Press ENTER to see the email body and ctrl+D to quit.
On another system, install Postfix, switch to Postfix using system mail switcher and then send a test email to user1
echo "This is a test email from another host" | mail -s "test email from external system" user1@linuxgravity.com
On mail server, check if user1 has received the email as described above.

Dovecot – POP3

To make remote users able to send and receive emails, we need to install and configure POP or IMAP server. Our choice here will be Dovecot.
First install it with the following command
yum -y install dovecot

Open nano dovecot.conf
nano /etc/dovecot.conf
and uncomment the following line
protocols = imap imaps pop3 pop3s
If using Outlook 2003, also uncomment this line too
pop3_uidl_format = %08Xu%08Xv
Save it (ctrl+x, y).
Start dovecot
/etc/init.d/dovecot start
I will show you how to configure Evolution. Same settings applies to other mail client. On the same system or another one open Evolution. If do not have any account already created, it will walk you through creating one. Fill in the required information at identity screen like below
evolution_identity
Fill in the Receiving Mail screen as shown
pop_receiving_mail
The Sending Mail configuration screen should look like below
pop_sending_mail
Now send a test email to root@linuxgravity.com and check the email from root account using mail program.
Configuring Evolution for IMAP is the same as POP3 except that at Receiving email screen, we select IMAP instead of POP.
From Edit Menu in evolution, click Preferences, select the POP account we set up earlier and delete it. Then click add, fill in the identity screen as for POP. Receiving Mail screen for IMAP should like this
imap_receiving_email
Sending email setting should be identical as for POP.
Now send a test message to root@linuxgravity.com to make sure that IMAP server works.
To configure our mail client for POPS (secure POP aka ssl), we will delete the account we set up for IMAP and add another one like before but will select SSL Encryption to be use as secure connection under Security as shown below
pop_ssl_receiving_mail
Once again finish the wizard and send a test email.
You will see the dummy certificate created by Dovecot for SSL as shown below
dovecot_dummy_certificate
IMAPS (secure IMAP) set up is similar to POPS except we choose IMAP at Receiving mail screen like below
pop_ssl_receiving_mail
Our next step is to create our own self-signed certificates for SSL. To do that we will first backup the dummy certificates
cd /etc/pki/dovecot
mv   certs/dovecot.pem     certs/dovecot.pem_bak
mv   private/dovecot.pem    private/dovecot.pem_bak
Then open dovecot-openssl.cnf
nano /etc/pki/dovecot/dovecot-openssl.cnf
and edit it like below (replacing information relevant to you). Pay special attention to common name (CN), it must be same as your email server name or *.yourdomain.com.
[ req ]
default_bits = 1024
encrypt_key = yes
distinguished_name = req_dn
x509_extensions = cert_type
prompt = no

[ req_dn ]
# country (2 letter code)
C=CA

# State or Province Name (full name)
ST=Quebec

# Locality Name (eg. city)
L=Montreal

# Organization (eg. company)
O=Linuxgravity

# Organizational Unit Name (eg. section)
OU=IT

# Common Name (*.example.com is also possible)
CN=mail.linuxgravity.com

# E-mail contact
emailAddress=admin@linuxgravity.com

[ cert_type ]
nsCertType = server
Run the certificates creation script that comes with Dovecot
/usr/share/doc/dovecot-1.0.7/examples/mkcert.sh
Restart Dovecot
/etc/init.d/dovecot restart
Try to use existing POPS (SSL) or IMAPS (SSL) account or create new one and you will be presented with the new public certificate as show
self-signed_certificate
Troubleshooting:
Don’t forget to open appropriate ports for SMPT, POP, IMAP, POPS, IMAPS. Checkout /etc/protocols which default ports are used by these protocols.
This was basic Postfix and POP/IMAP configuration. In future tutorials I will show you more complex set up that will be based on this one so make sure you followed this one correctly.




 For More Check Related Links :

0 comments:

Post a Comment