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.
Laboratory settings:
Step 1) Update the system
The first step is to ensure that the system software packages are up to date. To do this, update the system as follows:
# dnf update
Before proceeding, please also make sure that there are no other MTAs (such as Sendmail) as this will cause conflicts with Postfix configuration. For example, to delete Sendmail, run the following command:
# dnf remove sendmail
Step 2) Set the host name and update /etc/hosts
Use the following hostnamectl command to set the host name on the system:
# hostnamectl set-hostname server1.crazytechgeek.info
# exec bash
In addition, you need to add the hostname and IP of the system 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 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 the local to our server.
Step 5) Install mailx mail client
Before configuring the 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 configuration file of Postfix is located in /etc/postfix/main.cf. We need to make some changes to the configuration file, so please 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 is set to all##
inet_interfaces = all
## Change to all##
inet_protocols = all
## Comment##
# mydestination = $myhostname, localhost.$mydomain, localhost
## Uncomment##
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
## Uncomment and add IP range##
mynetworks =192.168.1.0/24,127.0.0.0/8
## Uncomment##
home_mailbox = Maildir/
When 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 valid, first, create a test user.
# useradd postfixuser
# passwd postfixuser
Next, run the following command to send mail from the 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 with the following command:
# dnf install telnet -y
When you run the command as described earlier, you should get the following output:
[ 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 to 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- 8 BITMIME
250- DSN
250 SMTPUTF8
Next, run the commands highlighted in orange, such as mail from, rcpt to, data, and finally enter quit:
mail from:<pkumar>2502.1.0 Ok
rcpt to:<postfixuser>2502.1.5 Ok
data
354 End data with<CR><LF>.<CR><LF>
Hello, Welcome to my mailserver(Postfix).2502.0.0 Ok: queued as B56BF1189BEC
quit
2212.0.0 Bye
Connection closed by foreign host
Complete the telnet command to send mail from the local user pkumar to another local user postfixuser, as shown below:
Send-email-with-telnet-centos8
If everything goes according to plan, then you should be able to view the sent mail in the new user's home directory:
# ls /home/postfixuser/Maildir/new1573580091.Vfd02I20050b8M635437.server1.crazytechgeek.info
#
To read the mail, just use the cat command as shown below:
# cat /home/postfixuser/Maildir/new/1573580091.Vfd02I20050b8M635437.server1.crazytechgeek.info
Read-postfix-email-linux
Postfix mail server log
Postfix mail server mail log 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
Protect Postfix mail server
It is recommended to always ensure the security of the communication between the client and the Postfix server. This can be achieved using SSL certificate, which can come from a trusted authority or a self-signed certificate. In this tutorial, we will use the openssl command to generate a self-signed certificate for Postfix,
I assume that openssl is already installed on your system. If it is not installed, use the following dnf command:
# dnf install openssl -y
Use the following openssl command to generate the private key and CSR (Certificate Signing Request):
# openssl req -nodes -newkey rsa:2048-keyout mail.key -out mail.csr
Postfix-Key-CSR-CentOS8
Now, use the following openssl command to generate a self-signed certificate:
# 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@linuxtechi
Getting Private key
#
Now copy the private key and certificate file to the /etc/postfix directory:
# cp mail.key mail.crt /etc/postfix
Update the path of the private key and certificate file 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 to make the above changes take effect:
# systemctl restart postfix
Let's try to use mailx client to send mail to internal local domain and external domain.
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 1217:341573580091.Vfd02I20050b8M635437.server1.crazytechgeek.info
- rw-------.1 postfixuser postfixuser 612 Nov 1302:401573612845.Vfd02I20050bbM466643.server1.crazytechgeek.info
# cat 1573612845.Vfd02I20050bbM466643.server1.crazytechgeek.info
Read-Postfixuser-Email-CentOS8
Send mail from postfixuser to 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 the email you sent to the external domain will be sent, otherwise it will be bounced and your IP will be blacklisted by databases like spamhaus.
Check Postfix mail queue
Use the mailq command to list the mail in the queue:
# mailq
Mail queue is empty
#
carry out! Our Postfix configuration is working! That's it for now. We hope you find this tutorial insightful and you can easily set up a local Postfix server.
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts