How to Install and Configure Postfix Mail Server on CentOS 8

How to Install and Configure Postfix Mail Server on CentOS 8

Postfix is ​​a free and open source MTA (Mail Transfer Agent) used to route or deliver emails on Linux systems. In this guide, you will learn how to install and configure Postfix on CentOS 8.

Lab Setup:

  • System: CentOS 8 Server
  • IP address: 192.168.1.13
  • Hostname: server1.crazytechgeek.info (make sure the domain name points to the IP of your server)

Step 1) Update the system

The first step is to make sure your system packages are up to date. To do this, update your system as follows:

# dnf update

Before continuing, also make sure that no other MTAs (such as Sendmail) exist, as this will cause conflicts with the Postfix configuration. For example, to remove Sendmail, run the following command:

# dnf remove sendmail

Step 2) Set the hostname and update /etc/hosts

Use the hostnamectl command below to set the hostname on your system:

# hostnamectl set-hostname server1.crazytechgeek.info
# exec bash

Additionally, you need to add the system's hostname and IP in /etc/hosts:

# vim /etc/hosts
192.168.1.13 server1.crazytechgeek.info

Save and exit the file.

Step 3) Install Postfix mail server

After verifying that no other MTAs are running on the system, run the following command to install Postfix:

# dnf install postfix 

Install-Postfix-Centos8

Step 4) Start and enable Postfix service

After successfully installing Postfix, run the following command to start and enable the Postfix service:

# systemctl start postfix
# systemctl enable postfix

To check the Postfix status, run the following systemctl command:

# systemctl status postfix 

Start-Postfix-check-status-centos8

Great, we have verified that Postfix is ​​up and running. Next, we will configure Postfix to send mail from our local machine to our server.

Step 5) Install mailx email client

Before configuring Postfix server, we need to install mailx. To install it, run the following command:

# dnf install mailx 

Install-Mailx-CentOS8

Step 6) Configure Postfix mail server

The Postfix configuration file is located in /etc/postfix/main.cf. We need to make some modifications to the configuration file, so open it with your favorite text editor:

# vi /etc/postfix/main.cf

Change the following lines:

myhostname = server1.crazytechgeek.info
mydomain = crazytechgeek.info
myorigin = $mydomain
## Uncomment and set inet_interfaces to all##
inet_interfaces = all
## Change to all ##
inet_protocols = all
## Notes ##
#mydestination = $myhostname, localhost.$mydomain, localhost
## Uncomment ##
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
## Uncomment and add IP ranges##
mynetworks = 192.168.1.0/24, 127.0.0.0/8
## Uncomment ##
home_mailbox = Maildir/

When you are finished, save and exit the configuration file. Restart the postfix service for the changes to take effect:

# systemctl restart postfix

Step 7) Test Postfix mail server

To test whether our configuration is effective, first, create a test user.

# useradd postfixuser
# passwd postfixuser

Next, run the following command to send mail from local user pkumar to another user postfixuser.

# telnet localhost smtp
Or # telnet localhost 25

If the telnet service is not installed, you can install it using the following command:

# dnf install telnet -y

When you run the command as described previously, you should get output similar to the following:

[root@linuxtechi ~]# telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 server1.crazytechgeek.info ESMTP Postfix

The above results confirm that the connection with the postfix mail server is normal. Next, enter the command:

#ehlo localhost

The output looks like this:

250-server1.crazytechgeek.info
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-DSN
250 SMTPUTF8

Next, run the commands highlighted in orange, such as mail from, rcpt to, data, and finally type quit:

mail from:<pkumar>
250 2.1.0 OK
rcpt to:<postfixuser>
250 2.1.5 OK
data
354 End data with <CR><LF>.<CR><LF>
Hello, Welcome to my mailserver (Postfix)
.
250 2.0.0 Ok: queued as B56BF1189BEC
quit
221 2.0.0 Bye
Connection closed by foreign host

Complete the telnet command to send mail from local user pkumar to another local user postfixuser as follows:

Send-email-with-telnet-centos8

If everything went according to plan, you should be able to view sent mail in the new user's home directory:

# ls /home/postfixuser/Maildir/new
1573580091.Vfd02I20050b8M635437.server1.crazytechgeek.info
#

To read the mail, just use the cat command as follows:

# cat /home/postfixuser/Maildir/new/1573580091.Vfd02I20050b8M635437.server1.crazytechgeek.info

Read-postfix-email-linux

Postfix mail server logs

The mail log of Postfix mail server is saved in the file /var/log/maillog. Use the following command to view the real-time log:

# tail -f /var/log/maillog 

postfix-maillogs-centos8

Securing Postfix Mail Server

It is recommended to always secure the communication between the client and the Postfix server, which can be achieved using SSL certificates, either from a trusted authority or self-signed certificates. In this tutorial, we will use the openssl command to generate a self-signed certificate for Postfix.

I assume openssl is already installed on your system, if not, use the following dnf command:

# dnf install openssl -y

Generate a private key and CSR (certificate signing request) using the openssl command below:

# openssl req -nodes -newkey rsa:2048 -keyout mail.key -out mail.csr

Postfix-Key-CSR-CentOS8

Now, generate a self-signed certificate using the following openssl command:

# openssl x509 -req -days 365 -in mail.csr -signkey mail.key -out mail.crt
Signature ok
subject=C = IN, ST = New Delhi, L = New Delhi, O = IT, OU = IT, CN = server1.crazytechgeek.info, emailAddress = root@linuxtechgeek
Getting Private key
#

Now copy the private key and certificate files to the /etc/postfix directory:

# cp mail.key mail.crt /etc/postfix

Update the paths to the private key and certificate files in the Postfix configuration file:

# vi /etc/postfix/main.cf
………
smtpd_use_tls = yes
smtpd_tls_cert_file = /etc/postfix/mail.crt
smtpd_tls_key_file = /etc/postfix/mail.key
smtpd_tls_security_level = may
………

Restart the Postfix service for the above changes to take effect:

# systemctl restart postfix

Let us try to send mails to internal local domain and external domain using mailx client.

To send internal local mail from pkumar to postfixuser:

# echo "test email" | mailx -s "Test email from Postfix MailServer" -r root@linuxtechi root@linuxtechi

Use the following command to check and read the mail:

# cd /home/postfixuser/Maildir/new/
#ll
total 8
-rw------. 1 postfixuser postfixuser 476 Nov 12 17:34 1573580091.Vfd02I20050b8M635437.server1.crazytechgeek.info
-rw------. 1 postfixuser postfixuser 612 Nov 13 02:40 1573612845.Vfd02I20050bbM466643.server1.crazytechgeek.info
# cat 1573612845.Vfd02I20050bbM466643.server1.crazytechgeek.info

Read-Postfixuser-Email-CentOS8

To send mail from postfixuser to an external domain ([email protected]):

# echo "External Test email" | mailx -s "Postfix MailServer" -r root@linuxtechi root@linuxtechi

NOTE: If your IP is not blacklisted anywhere then your mail to external domain will be delivered, otherwise it will bounce with a message saying your IP is blacklisted by databases like spamhaus.

Check the Postfix mail queue

Use the mailq command to list the mail in the queue:

# mailq
Mail queue is empty
#

Finish! Our Postfix configuration is working! That's all for now. We hope that you found this tutorial insightful and that you can easily set up a local Postfix server.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Java sends emails via exchange protocol
  • PHP implements email sending example code based on SMTP protocol
  • Java uses Socket to implement SMTP protocol to send emails
  • Tutorial on how to build a mail server in Ubuntu
  • Mail protocol and server working principle

<<:  Vite2.0 Pitfalls

>>:  Detailed explanation of the implementation principle of ACID transaction in Mysql

Recommend

Example steps for using AntV X6 with Vue.js

Table of contents 0x0 Introduction 0x1 Installati...

Vue advanced usage tutorial dynamic components

Table of contents Basic description AST parsing R...

Detailed explanation of the calculation method of flex-grow and flex-shrink in flex layout

Flex(彈性布局) in CSS can flexibly control the layout...

CSS3 property line-clamp controls the use of text lines

Description: Limit the number of lines of text di...

Sharing of web color contrast and harmony techniques

Color contrast and harmony In contrasting conditi...

In html table, set different colors and widths for each cell

It is recommended that you do not set the width, h...

React's transition from Class to Hooks

Table of contents ReactHooks Preface WhyHooks? Fo...

Vue implements verification whether the username is available

This article example shares the specific code of ...

In-depth analysis of MySQL query interception

Table of contents 1. Query Optimization 1. MySQL ...

Implementation of Vue large file upload and breakpoint resumable upload

Table of contents 2 solutions for file upload Bas...

How to create your own Docker image and upload it to Dockerhub

1. First register your own dockerhub account, reg...

The magic of tr command in counting the frequency of English words

We are all familiar with the tr command, which ca...

WeChat applet canvas implements signature function

In the WeChat applet project, the development mod...