Postfix is an open source mail transfer agent (MTA) for delivering emails in Linux. In this article, we will look at how to install and configure Postfix in CentOS Linux.
Postfix mail server configuration in CentOS Linux step by step
Here are the steps to install & configure Postfix mail server.
1. Update system
Open terminal and run the following command to update Ubuntu system.
# dnf update
Also remove any other mail transfer agent (MTA) that may cause conflict with postfix. Here is an example to remove sendmail.
# dnf remove sendmail
2. Set hostname and add host
Run the following command to set hostname to your domain/website.
# hostnamectl set-hostname www.example.com # exec bash
Add system’s hostname and IP address in /etc/hosts file.
# vim /etc/hosts 192.168.15.25 www.example.com
3. Install Postfix
Install Postfix with the following command.
# dnf install postfix
4. Start & Enable Postfix service
Run the following commands to start postfix and enable it to autorun during system reboot.
# systemctl start postfix # systemctl enable postfix
5. Install mailx email client
We need to install mailx service to be able to use postfix. Install it with the following command.
# dnf install mailx
6. Configure Postfix mail server
Open Postfix configuration file in a text editor.
$ sudo vi /etc/postfix/main.cf
Replace myhostname, mydomain with your domain, and also change IP address mentioned in mynetworks as per your requirement.
myhostname = www.example.com mydomain = example.com myorigin = $mydomain inet_interfaces = all inet_protocols = all mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain mynetworks = 192.168.1.0/24, 127.0.0.0/8 home_mailbox = Maildir/
Save and close the file. Restart postfix mail server.
# systemctl restart postfix
7. Test Postfix Mail Server
We need to create Linux user to test our configuration.
# useradd postfixuser # passwd postfixuser
Next, use the telnet command to send email from your logged in user to the newly created user.
# telnet localhost smtp or # telnet localhost 25
If telnet is not installed on your system, install it with the following command.
# dnf install telnet -y
When you issue the telnet command, you will see the following kind of output.
[root@server1 ~]# telnet localhost 25 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. 220 www.example.com ESMTP Postfix
It indicates that you are able to connect to postfix mail server successfully. Next, run the following command.
# ehlo localhost
You will see the following output.
250-www.example.com 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-8BITMIME 250-DSN 250 SMTPUTF8
Next, run the following commands in bold to compose and send the email in terminal. When you issue quit command, your email will be sent.
mail from:<ubuntu> 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
If the mail was sent successfully, you can view it in your home directory.
# ls /home/postfixuser/Maildir/new 1573580091.Vfd02I20050b8M635437.www.example.com #
You can view the email content with cat command.
# cat /home/postfixuser/Maildir/new/1573580091.Vfd02I20050b8M635437.www.example.com
You can view your mail server’s logs at /var/log/maillog
# tail -f /var/log/maillog
If you want to check your server’s mail queue, use the mailq command
# mailq Mail queue is empty
8. Secure Mail Server
It is advisable to secure your mail server using an SSL certificate. Here is a command to create a private key and certificate signing request.
# openssl req -nodes -newkey rsa:2048 -keyout mail.key -out mail.csr
Run the following command to generate self-signed certificate.
# openssl x509 -req -days 365 -in mail.csr -signkey mail.key -out mail.crt
Copy the SSL certificate to /etc/postfix directory.
# cp mail.key mail.crt /etc/postfix
Run the following command to open postfix configuration file.
# vi /etc/postfix/main.cf
Add the following lines to update SSL certificate location in Postfix configuration file.
……… 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 ………
Save and close the file. Restart postfix server.
# systemctl restart postfix
Here’s a simple command to test if secure emails work on your server.
# echo "test email" | mailx -s "Test email from Postfix MailServer" -r email@example.com firstname.lastname@example.org
To send emails to outside users, replace email@example.com with external email address.
That completes postfix mail server configuration. In this article, we have learnt how to install, setup & configure postfix mail server, and also secure it.
How to Install GoAccess Log Analyzer in Ubuntu
How to Check Version in Python
How to Install Go (Golang) in Ubuntu
How to Extract Substring from String in Bash
How to Split String Into Array in Shell Script