Configure Postfix as a Send-Only SMTP Server on Ubuntu 20.04

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.

Prerequisites

  1. Ubuntu 20.04 server
  2. A user with sudo privileges
  3. 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
192.1.2.3 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.

Postfix Package Configuration Screen

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.

Postfix Mail Type Configuration

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 domain.com only.

Postfix System Mail Name Configuration

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
Postfix Service Status

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

Test Postfix

To test Postfix, run the command below. The most important thing here is to replace recipient@externaldomain.com with a valid external recipient address where this test message will be delivered.

$ echo "This is the message body to test Postfix" | mail -afrom:shola@shola.com -s "This is the message subject to test Postfix" recipient@externaldomain.com

Check your external mailbox to confirm if the message was delivered. It would most likely be placed in the junk/spam folder.

Postfix Test Message

Conclusion

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.

Leave a Comment