Postfix is an open-source Mail Transport Agent (MTA). An MTA is a component of the Simple Mail Transfer Protocol (SMTP) responsible for transferring mail from one mail server to another.
This tutorial describes how you can install and configure Postfix as a send-only SMTP server on Ubuntu 20.04. You can use this for sending notification emails to an external address when specific events occur on the server -- such as when a backup job succeeds or fails.
- Ubuntu 20.04 server
- A user with sudo privileges
- A registered domain name pointing to your server's public IP address
Configure Server Fully Qualified Domain Name (FQDN)
A fully qualified domain name comprises a hostname and a domain name. For example, mail.domain.com -- where mail is the hostname and domain.com is the domain name. The FQDN uniquely identifies your server on the Internet.
Firstly, run the command below to open the hostname file for editing.
$ sudo nano /etc/hostname
Next, delete the existing entry if you do not want to use that, and enter your desired hostname (e.g. mail or smtp). After that, save your changes and close the hostname file.
To configure the domain name part, run the command below.
$ sudo nano /etc/hosts
In this file, you need to specify your server's public IP address, the FQDN, and the hostname. The following is an example.
127.0.0.1 localhost 127.0.1.1 smtp.domain.com smtp 126.96.36.199 smtp.domain.com smtp
Save your changes and close the hosts file.
Install and Configure Postfix
Now, let us proceed to install and configure Postfix. Run the command below to check for package updates.
$ sudo apt update
Next, run the command below to install mailutils which is a collection of mail utilities, including Postfix.
$ sudo apt install mailutils -y
At the initial Postfix package configuration screen, read the information displayed and press enter to proceed.
On the next screen, the default selection should be Internet Site. This is fine because our goal is to send emails across the Internet. Press the tab key to move the selection to Ok and then press enter.
Next, the system mail name may already be populated for you based on your server’s FQDN. Delete the host part so it becomes your
Press the tab key to select Ok and then press enter to complete the installation.
Run the next command to check the status of Postfix.
$ sudo systemctl status postfix
To ensure that postfix is automatically started whenever the server is rebooted, run:
$ sudo systemctl enable postfix
Now, open the main Postfix configuration file with the following command.
$ sudo nano /etc/postfix/main.cf
In this file, look for inet_interfaces and set the value to loopback-only. This will ensure that Postfix will only listen on the localhost interface and can prevent it from being used as an open relay.
Save your changes and close the main.cf file.
Reload Postfix configuration with the next command:
$ sudo systemctl reload postfix
To test Postfix, run the command below. The most important thing here is to replace firstname.lastname@example.org with a valid external recipient address where this test message will be delivered.
$ echo "This is the message body to test Postfix" | mail -afrom:email@example.com -s "This is the message subject to test Postfix" firstname.lastname@example.org
Check your external mailbox to confirm if the message was delivered. It would most likely be placed in the junk/spam folder.
In this tutorial, we have described the steps to install and configure Postfix as a send-only SMTP server on Ubuntu 20.04. This tutorial focused on the basic configuration of Postfix. To learn more, see the Postfix documentation.