How To Install Postfix CentOS 7

Published on 2019-08-21· Updated on 2021-12-15

The author voluntarily contributed this tutorial as a part of Pepipost Write to Contribute program.

Introduction

Postfix is one of the most popular open-source Mail Transfer Agent (MTA) which route and delivers mails. It is an alternative to Sendmail MTA which comes pre-installed in all version before Centos/RHEL 5. CentOS Postfix installation is a process which requires a lot of precision.

Let us look at Wikipedia's definition of Postfix, which says,

"Postfix is a free and open-source mail transfer agent that routes and delivers electronic mail. It is released under the IBM Public License 1.0 which is a free software license. Alternatively, starting with version 3.2.5, it is available under the Eclipse Public License 2.0 at the user's option." - Wikipedia

The main job of the (CentOS) postfix is to relay mails locally or to a destination server outside the network. In order to install postfix and avoid conflicts, you need to remove sendmail if it is already installed.

Before starting you can also refresh your concepts on how email works with Postfix as a reference. This would help you go further with this content.

Step 1: Checking And Removing Sendmail (Required Only If Sendmail Is Installed)

Input: Run the below command to check whether sendmail is installed or not:

rpm -qa | grep sendmail

Output: If sendmail is installed on your server, then the following output will come:

If you didn’t get any output that means you don’t have sendmail installed and you are good to skip to step 2.

If you get an output similar to one shown above, then you need to remove Sendmail using the below command:

sudo yum remove sendmail*

Once you have successfully removed Sendmail, you will be getting an output similar to one shown below:

Step 2: Install Postfix

It is always one of the easiest ways to install postfix using yum installer (ideally if you are using Centos/RHEL > 5 postfix comes pre-installed).

You can check if postfix centos 7 is already installed or not using the below command:

rpm -qa | grep postfix

You will get the above output, if Postfix is already installed. In case Postfix is not installed, then use the below command to install postfix:

sudo yum install postfix

Keep saying "Yes" to the prompt each time it asks. Once all the components are downloaded, you will have the postfix centos 7 installed successfully.

Step 3: Configure Postfix.

We need to edit /etc/postfix/main.cf file.

vim /etc/postfix/main.cf

Make changes according to the below steps.

Note: Mostly you will find the line which needs to be changed on line 67.

Add hostname to the file by unhashing and editing at line no 75

myhostname = smtp.example.local

Uncomment and set domain name at line no 83

mydomain = example.local

Uncomment line no 99

myorigin = $mydomain

Uncomment and Set ipv4 at line no 113

inet_interfaces = all

edit line no 119 to all

inet_protocols = all

Comment at line no 164

mydestination = $myhostname, localhost.$mydomain, localhost,

Uncomment and add IP range at line no 264

mynetworks = 192.168.1.0/24, 127.0.0.0/8

Uncomment at line no 419

home_mailbox = Maildir/

save and exit the file.

Enable the service using the below command

systemctl enable postfix

Start/restart the postfix service.

systemctl restart postfix

once you have restarted postfix need check the status of the service using below command:

systemctl status postfix 
OR 
service postfix status
[root@nl587 ~]# service postfix status
Redirecting to /bin/systemctl status postfix.service
â—Ź postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2019-08-04 18:02:45 IST; 51min ago
Main PID: 4501 (master)
CGroup: /system.slice/postfix.service
├─ 4501 /usr/libexec/postfix/master -w
├─ 4506 pickup -l -t unix -u
├─ 4507 qmgr -l -t unix -u
├─15652 smtpd -n smtp -t inet -u -s 2
├─15653 proxymap -t unix -u
└─15654 anvil -l -t unix -u

Aug 04 18:02:08 nl587 systemd[1]: Starting Postfix Mail Transport Agent...

Step 4: Testing Postfix Server

Let's add a user for testing and call it as “postfixtester”

useradd postfixtester

add password for the user postfixtester

passwd postfixtester

after adding user lets check the server access using telnet.

telnet localhost smtp

Once you have successfully done with the configuration you get the following output
[root@nl587 ~]# telnet localhost smtp
Trying ::1…
Connected to localhost.
Escape character is '^]'.
220 smtp.example.local ESMTP Postfix

Start your transaction writing below command.

ehlo localhost
250-smtp.example.local
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN

Once you get 250 DSN you can sendmail.

Case 1: Successful Test

mail from:<postfixtester>
250 2.1.0 Ok [success output]
# rcpt to:<postfixtester>
250 2.1.5 Ok [success output]
# data # type this to pass input of email body
354 End data with <CR><LF>.<CR><LF> [success output ]
This is test mail from your local host server #mail body
. # dot(.) for completing the mail body
250 2.0.0 Ok: queued as E2R572042D75 [success output]
quit # quit from the telnet command prompt
221 2.0.0 Bye
Connection closed by foreign host.

Case 2: Failed Testcase

mail from:<postfixtester>
250 2.1.0 Ok [success output]
# rcpt to:<postfixtester>
454 4.7.1 <postfixtester>: Relay access denied [failure point]

This usually occurs if your domain is not mapped with the server.
for example :: you have mention mydomain = example.com in /etc/postfix/main.cf file and it is not mapped with your hostname/server postfix will show above error.
If every thing is working fine you can navigate to your newuser (“postfixtester”) directory and check the mail accordingly.

ls /home/postfixtester/Maildir/new/

Output:

1336588056.Vfe01I786e3e7M938078.smtp.example.local

If you have received something like this we have successfully received an email. To read the following mail, just cat the file:

cat /home/postfixtester/Maildir/new/1336588056.Vfe01I786e3e7M938078.smtp.example.local

Output:

Return-Path: <[email protected]>
X-Original-To: postfixtester
Delivered-To: [email protected]
Received: from localhost (localhost [IPv6:::1])
by smtp.example.local (Postfix) with ESMTP id E2R572042D75
for <postfixtester>; Sun, o4 Aug 2019 17:42:36 +0530 (IST)
Message-Id: <[email protected]>
Date: Sun, 04 Aug 2019 17:42:36 +0530 (IST)
From: [email protected]

This is test mail from your localhost server.

Finally! You have postfix installed and emails getting sent! You are all set to use your server as your private SMTP server to send emails.

Grade My Email
Check your spam now?

Netcorecloud's toolkit is the solution to all your email problems.

Vikram Sahu 🦉

Developer Evangelist, Pepipost

💻 Senior Software engineer → 🥑 Developer Relations guy → 🤓 Building Community for Email Geeks 🎟 → Speaker 🎙

You can also explore

Netcore connects & unifies your data across all sources, connects to your marketing channels and provides you with control over AI Powered automation and personalization.

Deploy emails that are
screenshot worthy!

Stop settling for static emails - use Netcore's platform to create dynamic, interactive emails that convert.
Let's Deploy
Get Started Now