Welcome to Salem Houali ‘s Oracle Developer Notes

Postfix SMTP Server installation and configuration on Centos8

About Postfix

Postfix is a free and open-source mail transfer agent (MTA) that routes and delivers electronic mail.

Originally written in 1997 by Wietse Venema at the IBMThomas J. Watson Research Center in New York, and first released in December 1998, Postfix continues as of 2020 to be actively developed by its creator and other contributors. The software is also known by its former names VMailer and IBM Secure Mailer.

In August 2019 a study performed by E-Soft, Inc., approximately 34% of the publicly reachable mail-servers on the Internet ran Postfix, making it the second most popular mail server behind Exim.

Ref.:  https://en.wikipedia.org/wiki/Postfix_(software)

Step1. Install and configure Postfix.
Step2. Use Postfix to test emails sent by scheduled jobs in crontab
For the demo, we have a job that creates crontab backups  and restores the most recent backup on the server

Step1.: Postfix installation and configuration

Postfix configuration files

By default, Postfix configuration files are in /etc/postfix. The two most important files are main.cf and master.cf; these files must be owned by root


Before Installing Postfix

You need first, to properly set up your CentOS Server.

By default, Postfix uses your server’s hostname to identify itself when communicating with other MTAs. Hostname can have two forms: a single word and FQDN (fully qualified domain name ).

The single word form is used mostly on personal computers. Your Linux home computer might be named linux, debian, ubuntu etc. FQDN (Fully Qualified Domain Name) is commonly used on Internet-facing servers and we should use FQDN on our mail servers. It consists of two parts: a node name and a domain name. For example: mail.linecode-notes.com is an FQDN.

mail  is the nodename, linecode-notes.com  is the domain name. FQDN will appear in the smtpd banner. Some MTAs reject messages if your Postfix does not provide FQDN in smtpd banner. Some MTAs even query DNS to see if FQDN in the smtpd banner resolves to the IP of your mail server.

Enter the following command to see the FQDN form of your hostname:

hostname -f

[root@oc8-19 etc]# hostname -f
oc8-19.localdomain
if your CentOS/ server doesn’t have an FQDN yet, you can use hostnamectl to set one.

hostnamectl set-hostname mail.linecode-notes.com

[root@oc8-19 etc]# hostnamectl set-hostname mail.linecode-notes.com
[root@oc8-19 etc]# hostname -f
mail.linecode-notes.com

Installing Postfix

On your CentOS server, run the following two commands.

dnf update

dnf install postfix -y

start Postfix SMTP server.
[root@oc8-19 etc]# systemctl start postfix

Enable auto-start at boot time.
[root@mail oracle]# systemctl enable postfix

Check its status with.
[root@mail oracle]# systemctl status postfix

Postfix is now active (running)

root@mail oracle]# postconf mail_version
mail_version = 3.3.1
root@mail oracle]#
[root@mail oracle]# rpm -ql postfix | grep /usr/sbin/


Configuring Postfix

As we are running Postfix on a virtual network interface, we have to look at the other parameters listed here as well:

Postconf is a Postfix configuration utility that can display the value of parameters in Postfix main configuration file (/etc/postfix/main.cf).

# Global Postfix configuration file. This file lists only a subset
# of all parameters. For the syntax, and for a complete parameter
# list, see the postconf(5) manual page (command: "man 5 postconf").
#
myhostname
mydomain
myorigin
inet_interfaces
inet_protocols 
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 168.100.189.0/28, 127.0.0.0/8
home_mailbox = Maildir/
#
#

or type the following commands to perform changes.

[root@mail postfix]# postconf inet_interfaces
inet_interfaces = all

By default, Postfix SMTP server uses the OS’s hostname. You can display the current Postfix hostname with the following command.

[root@mail postfix]# postconf myhostname
myhostname = oc8-19.localdomain ( wrong value)

Change the value:

[root@mail postfix]# postconf -e "myhostname = mail.linecode-notes.com"
[root@mail postfix]# postconf myhostname
myhostname = mail.linecode-notes.com (correct value)

The $mydomain parameter specifies the local internet domain name. The default is to use $myhostname minus the first component. You can display the current value of $mydomain with:

[root@mail postfix]# postconf mydomain
mydomain = localdomain (wrong value)

It should be your  domain name, like linecode-notes.com
Change the value:

postconf -e “mydomain = linecode-notes.com”

[root@mail postfix]# postconf -e "mydomain = linecode-notes.com"
[root@mail postfix]# postconf mydomain
mydomain = linecode-notes.com (correct value)

The myorigin parameter specifies the domain that appears in mail that is posted on this machine

The $myorigin parameter specifies the default domain name that is appended to sender and recipient addresses that have no @domain part. The default is to use the value of $myhostname, as can be seen with:

[root@mail postfix]# postconf myorigin
myorigin = $mydomain (wrong value)

change the value to linecode-notes.com

[root@mail postfix]# postconf -e "myorigin = linecode-notes.com"
[root@mail postfix]# postconf myorigin
myorigin = linecode-notes.com (correct value)

The $mydestination parameter specifies the list of domains that your server considers itself the final destination for. You can display the current value of $mydestination with:

[root@mail postfix]# postconf mydestination
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

The default value allows your Postfix SMTP server to receive emails coming for someone@mail.yourdomain.com, someone@localhost.yourdomain.com and someone@localhost, but it won’t allow you Postfix SMTP server to receive emails coming for someone@yourdomain.com. In order to achieve that, add yourdomain.com to the list of domains.

Change the value:
postconf -e “mydestination = linecode-notes.com, \$myhostname, localhost.\$mydomain, localhost”

[root@mail postfix]# postconf -e "mydestination = linecode-notes.com, \$myhostname, localhost.\$mydomain, localhost"
[root@mail postfix]# postconf mydestination
mydestination = linecode-notes.com, $myhostname, localhost.$mydomain, localhost

Restarting Postfix

Finally, we need to restart Postfix for the changes to take effect

[root@mail postfix]# systemctl restart postfix

Open Port 25 (inbound) in Firewall.Run the following commands to open port 25 (inbound), so Postfix can receive emails from other SMTP servers.

firewall-cmd –permanent –zone=public –add-port=25/tcp

[root@mail postfix]# firewall-cmd --permanent --zone=public --add-port=25/tcp 
FirewallD is not running

# Global Postfix configuration file. This file lists only a subset # of all 
# parameters. For the syntax, and for a complete parameter # list, see the 
# postconf(5) manual page (command: "man 5 postconf").
###### Starting POSTFIX CONFIGURATION 
##### Salem .H
#
myhostname = mail.linecode-notes.com
mydomain = linecode-notes.com
myorigin = linecode-notes.com
inet_interfaces = all
inet_protocols = all
mydestination = linecode-notes.com, $myhostname, localhost.$mydomain, localhost
mynetworks = 168.100.189.0/28, 127.0.0.0/8

#### Ending  POSTFIX CONFIGURATION

[root@mail new]# pwd
/home/postfixuser/Maildir/new
-rw-------. 1 postfixuser postfixuser 586 Apr 12 14:22 1586715770.Vfd02I1007e5cM535282.mail.linecode-notes.com
[root@mail new]# cat 1586715770.Vfd02I1007e5cM535282.mail.linecode-notes.com
Return-Path: <oracle@linecode-notes.com>
X-Original-To: postfixuser
Delivered-To: postfixuser@linecode-notes.com
Received: from localhost (localhost [127.0.0.1])
by mail.linecode-notes.com (Postfix) with ESMTP id 8BC961BA219
for <postfixuser>; Sun, 12 Apr 2020 14:18:29 -0400 (EDT)
Message-Id: <20200412181843.8BC961BA219@mail.linecode-notes.com>
Date: Sun, 12 Apr 2020 14:18:29 -0400 (EDT)
From: oracle@linecode-notes.com

Hello,

This a final test for POSTFIX configuration

To view this mail, just display the content of the following folder
/home/postfixuser/Maildir/new

Thnks.
[root@mail new]#
[root@mail postfix]# sendmail -v postfixuser@mail.linecode-notes.com </tmp/mail.txt
[root@mail new]# vi 1586716684.Vfd02I1008127M424524.mail.linecode-notes.com
return-Path: <root@linecode-notes.com>
X-Original-To: postfixuser@mail.linecode-notes.com
Delivered-To: postfixuser@mail.linecode-notes.com
Received: by mail.linecode-notes.com (Postfix, from userid 0)
id 59DD71BA219; Sun, 12 Apr 2020 14:38:04 -0400 (EDT)
Subject: Mail Delivery Test
Message-Id: <20200412183804.59DD71BA219@mail.linecode-notes.com>
Date: Sun, 12 Apr 2020 14:38:04 -0400 (EDT)
From: root <root@linecode-notes.com>

Hello, this a mail test - delivery

postfix - mail server testing on centos8.

Postfix mail server logs

Postfix mail server  store mail logs in “/var/log/maillog“, use below command to view the live logs,

[root@mail new]# tail -f /var/log/maillog
Apr 12 14:39:14 oc8-19 postfix/qmgr[14471]: B3B971BA219: from=<root@linecode-notes.com>,
 size=382, nrcpt=1 (queue active)
Apr 12 14:39:14 oc8-19 postfix/local[15236]: B3B971BA219: to=<postfixuser@mail.linecode-notes.com>, 
relay=local, delay=0.14, delays=0.06/0/0/0.08, dsn=2.0.0, status=sent (delivered to maildir)

Apr 12 14:39:14 oc8-19 postfix/local[15236]: CEA2F1BA22C: to=<root@linecode-notes.com>, relay=local, 
delay=0.12, delays=0.05/0/0/0.07, dsn=2.0.0, status=sent (delivered to maildir)

Step2. Sample Crontab backup with postfix SMPT email testing 

Pererequistes:
x x x x x  crontab -l > /work_dir/CR_BKP/crontab_bkp_`date +\%y\%m\%d-\%H\%M\%S`

For our  test, we have crontab – backup each 05 mn.

-rw-r–r–. 1 oracle oinstall 404 Apr 14 20:15 crontab_bkp_200414-201501
-rw-r–r–. 1 oracle oinstall 404 Apr 14 20:20 crontab_bkp_200414-202001
-rw-r–r–. 1 oracle oinstall 404 Apr 14 20:25 crontab_bkp_200414-202501
-rw-r–r–. 1 oracle oinstall 404 Apr 14 20:30 crontab_bkp_200414-203001
-rw-r–r–. 1 oracle oinstall 404 Apr 14 20:35 crontab_bkp_200414-203501
-rw-r–r–. 1 oracle oinstall 404 Apr 14 20:40 crontab_bkp_200414-204002
-rw-r–r–. 1 oracle oinstall 404 Apr 14 20:45 crontab_bkp_200414-204501



The script look for the crontab backup in /work_dir/TMP – folder, check it’s creation date and finally send an emal to postfixuser.


restore_backup – dir contains the most recent crontab – backup


crontab_check.log


Check the mail : /home/postfixuser/Maildir/new

-rw——-. 1 postfixuser postfixuser 798 Apr 14 21:13 1586913226.Vfd02I1003f02M169235.mail.linecode-notes.com


This ends our post
Posted on: 2020-11-29

Leave a Reply