How to Setup Postfix Mail Server on CentOS 7

What is Postfix?

Postfix is a mechanism used to send emails from a host to global locations, once it is installed and configured. When running on a machine, Postfix turns the host into an SMTP server. It is a free, open-source Mail Transfer Agent (MTA).

There are three primary components of email services:

  • Mail User Agent (MUA): This component allows users to read and compose emails using email clients such as Thunderbird and Microsoft Outlook.
  • Mail Transport Agent (MTA): This component is responsible for transporting emails from one site to another, such as Sendmail or Postfix.
  • Mail Delivery Agent (MDA): This component delivers received messages to the appropriate user mailbox on the local machine. Examples include postfix-maildrop and procmail.

Environment.

Mail Server with webmail:
Operating System: CentOS 7 minimal server
Hostname: centos-vm2
IP Address: 192.168.2.123/24

Relay Host which can send and receive mail
Operating System: CentOS 7 minimal server
Hostname: centos-vm1
IP Address: 192.168.2.122/24

Relay Host which can only send mail
Operating System: CentOS 7 minimal server
Hostname: centos-vm3
IP Address: 192.168.2.124/24

This complete activity is divided into 5 parts.

PART-1: Installation and Configuration of postfix on centos-vm2
PART-2: Installation and Configuration of Dovecot on centos-vm2
PART-3: Installation and Configuration of Squirrel mail(Webmail) on centos-vm2
PART-4: Installation and Configuration of postfix(relay host which can send and receive mail) on centos-vm1
PART-5: Installation and Configuration of postfix(relay host which can send) on centos-vm3

PART-1: INSTALLATION/CONFIGURATION OF POSTFIX ON CENTOS-VM2

To install Postfix Mail on centos-vm2, first run the following command: 

# yum install postfix telnet mailx -y

Postfix Parameters details
myhostname : This option is used for specifying the hostname of the mail server. This is the internet hostname on which Postfix will receive emails on it.
mydomain : This option is the mail domain that you will be servicing.
myorigin : This setting defined how the sender address should be written in email. There are two ways to write it, with hostname and without hostname or with domain.
eg:
with hostname: vm2-user1@centos-vm2.darole.org
with domain: vm2-user1@darole.org
mydestination: The mydestination parameter specifies what domain this machine will deliver locally, instead of forwarding to another machine. The default is to receive mail for the machine itself.
mynetworks: This setting defines the list of networks that are allowed to use the email server. This setting provides basic protection against email abuse. This setting limits postfix to accept messages only from your trusty networks.
inet_interfaces: This setting defines the network interface on which postfix listens for messages. if this setting is set to localhost, Postfix will listen only from local system on loopback interface. if this setting is set to all, Postfix will listen from all systems on all network interfaces.
home_mailbox: The home_mailbox parameter specifies the optional pathname of a mailbox file relative to a user's home directory
relayhost: .

Once Postfix is installed, edit the configuration file located at /etc/postfix/main.cf to configure these parameters:

# vi /etc/postfix/main.cf
myhostname = centos-vm2.darole.org
mydomain = darole.org
myorigin = $mydomain            ### Uncomment ###
inet_interfaces = all                ### Uncomment ###
# inet_interfaces = localhost   ### comment ###
#mydestination = $myhostname, localhost.$mydomain, localhost  ### comment ###
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain ### Uncomment ###
mynetworks = 192.168.2.0/24, 127.0.0.0/8
home_mailbox = Maildir/  
### Uncomment ###

Save and exit the file.

Next, restart and enable Postfix service:

# systemctl restart postfix
# systemctl enable postfix

Finally, add some test users.

# useradd -c "vm2-user1" -m vm2-user1 ; echo redhat | passwd --stdin vm2-user1
# useradd -c "vm2-user2" -m vm2-user2 ; echo redhat | passwd --stdin vm2-user2

You can now test the Postfix server by logging in as vm2-user1 and sending an email to vm2-user2:

# su - vm2-user1
# mail -s "mail from user1" vm2-user2@centos-vm2.darole.org < /dev/null
# exit
# su - vm2-user2
ll /home/vm2-user2/Maildir/new

PART-2: INSTALLATION/CONFIGURATION OF DOVECOT ON CENTOS-VM2

Dovecot is an open source application that allows you to receive emails on a Linux server through IMAP and POP3 protocol.
Install Dovecot Package
# yum install -y dovecot

To Configure Dovecot we need to edit multiple configuration files.

Edit file /etc/dovecot/dovecot.conf file
# vi /etc/dovecot/dovecot.conf
protocols = imap pop3 lmtp         
## uncomment ##

Edit file /etc/dovecot/conf.d/10-mail.conf file
# vi /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:~/Maildir   ## uncomment ##

Edit /etc/dovecot/conf.d/10-auth.conf
# vi /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = yes     ## uncomment ##
auth_mechanisms = plain login ## Add the word: "login" ##

Edit file /etc/dovecot/conf.d/10-master.conf,
# vi /etc/dovecot/conf.d/10-master.conf

unix_listener auth-userdb {

#mode = 0600
user = postfix     ## Line 91 - Uncomment and add "postfix"
group = postfix   ## Line 92 - Uncomment and add "postfix"

}

Start and enable Dovecot service:
# systemctl start dovecot
# systemctl enable dovecot

Testing Dovecot
# telnet centos-vm2.darole.org pop3
user vm2-user1
pass redhat

quit

PART-3: INSTALLATION/CONFIGURATION OF SQUIRREL MAIL(WEBMAIL) ON CENTOS-VM2

Squirrel Mail is a standards-based webmail package written in PHP. It includes built-in pure PHP support for the IMAP and SMTP protocols

To install Squirrel mail you need remirepo
# yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Install Squirrel Mail Package
# yum install squirrelmail -y

Configuring Squirrel-Mail
# cd /usr/share/squirrelmail/config/
# ./conf.pl



Enter 1 and chage the organization name to DAROLE.org
Press S to Save
Press R for the previous menu (Return to the main menu)

Enter 2 for Server settings
Press 1
and type: darole.org (Name of Domain)
Press 3
Press 2 for switching from Send Mail to SMTP

Type S and Press Q for quit

Add squirrelmail configuration is apache config file:
# vi /etc/httpd/conf/httpd.conf
Alias /webmail /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 and enable Apache service.
# systemctl restart httpd
# systemctl enable httpd

Open Web Browser and type the below address
http://192.168.2.123/webmail


Log in to Web browser using user vm2-user1@centos-vm2.darole .org and send mail to vm2-user2@centos-vm2.darole .org, then log in again to Web Brower using user vm2-user2@centos-vm2.darole .org and check the mail.

PART-4: INSTALLATION/CONFIGURATION OF POSTFIX (RELAYHOST WHICH CAN SEND AND RECEIVE MAIL) ON CENTOS-VM1

Steps to Install Postfix Relay Servers on centos-vm1

Install Postfix Mail Server, telnet and mailx package on centos-vm1.
# yum install postfix telnet mailx -y

Configure Postfix
After installation, we need to edit the postfix configuration file located in /etc /postfix/main.cf and make the following parameters changes .

# vi /etc/postfix/main.cf
myhostname = centos-vm1.darole.org
mydomain = darole.org
myorigin = $mydomain ### Uncomment ###
inet_interfaces = all ### Uncomment ###
# inet_interfaces = localhost ### comment ###
#mydestination = $myhostname, localhost.$mydomain, localhost ### comment ###
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain ### Uncomment ###
mynetworks = 192.168.2.0/24, 127.0.0.0/8
relayhost = centos-vm2.darole.org

Save and exit the file.

Restart/Enable Postfix service:
# systemctl restart postfix
# systemctl enable postfix

Add the below users for testing.
# useradd -c "vm1-user1" -m vm1-user1 ; echo redhat | passwd --stdin vm1-user1
# useradd -c "vm1-user2" -m vm1-user2 ; echo redhat | passwd --stdin vm1-user2

PART-5: INSTALLATION/CONFIGURATION OF POSTFIX (RELAYHOST WHICH CAN SEND MAIL) ON CENTOS-VM3

Steps to Install Postfix Relay Servers on centos-vm3
Install Postfix Mail Server, telnet and mailx package on centos-vm3.
# yum install postfix telnet mailx -y

Configure Postfix
After installation, we need to edit the postfix configuration file located in /etc /postfix/main.cf and make the following parameters changes .

# vi /etc/postfix/main.cf
relayhost = centos-vm2.darole.org

Save and exit the file.

Start/restart Postfix service now:
# systemctl restart postfix
# systemctl enable postfix


Add the below users for testing.
# useradd -c "vm3-user1" -m vm3-user1 ; echo redhat | passwd --stdin vm3-user1
# useradd -c "vm3-user2" -m vm3-user2 ; echo redhat | passwd --stdin vm3-user2

Test the Postfix Server:

Now login to centos-vm3 servers with vm3-user1 and send mail to centos-vm1 user vm1-user1 and check.

# ssh centos-vm3
# su - vm3-user1
# mail -s "mail from centos-vm3" vm1-user1@centos-vm1.darole.org < /dev/null
# exit
# ssh centos-vm1
# su - vm1-user1
# mail

Troubleshooting. 

Check the mail logs.
# tail -f /var/log/maillog



No comments:

Post a Comment