How to Install and Configure Postfix as a Send-Only SMTP Server on CentOS 8
Configuring a mail server to send messages only can be very useful when administering servers. For example, to receive notifications from Cron or monitoring systems. In this tutorial we will look at how to install and configure Postfix as a send-only SMTP server on CentOS 8. First, you need to have a domain name from which mail will be sent, as well as authorize on the server as root.
Installing and configuring Postfix
Let’s install the required packages. Postfix is the mail server itself, and mailx is the environment for handling email.
dnf install -y postfix mailx
Activate the start together with the system and run Postfix.
systemctl enable --now postfix
Open the Postfix configuration file.
You need to find the inet_interfaces parameter and assign it the localhost value if it is different at the moment. In my case, the parameter was listed several times, but with the correct value.
inet_interfaces = localhost
Set the myhostname parameter to your domain name.
myhostname = domain-name.com
Uncomment this line:
myorigin = $myhostname
Make the line look like this and replace the domain name with your own:
mydestination = $myhostname, domain-name.com, localhost.$mydomain, localhost
Save and close the file and restart the system.
Now you need to configure the hostname for the server.
echo "domain-name.com" > /etc/hostname
These settings are sufficient in many cases, but some mail servers have fairly strict spam filters and will reject mail. In this case, additional steps are required. One of which is setting up a PTR record for a domain. Usually, to do it, you need to contact the support service of the company that provided you with a public IP address.
To send a test message, use the command:
echo "Mail delivery test" | mail -s "[Server] New e-mail" email@example.com
The mail address must be replaced with a real one, and the text in quotation marks represents the body of the letter and its header.
Configuring mail forwarding
Sometimes you need to receive the host’s internal mail to an external mailbox. For example, to receive system notifications. There are aliases for these purposes. Let’s take a look at setting up mail forwarding for root. Open the file with aliases.
To add mail forwarding for root to an external mailbox, add a line with your mailbox:
Save the file and enter the command to apply the changes: