Postfix: configure a mail relay (MTA) on Ubuntu

In this tutorial, I will explain to you how to install (it’s the easiest) and configure Postfix on Ubuntu as an email relay to send emails from applications, equipment (copier, switch, etc.).

This type of server is often used in businesses to send emails from different equipment and software, even if you have an internal messaging solution such as Exchange, I recommend setting up a third-party relay for sending messages.

We are going to set up an MTA (Mail Transfer Agent) server.

Installing Postfix on Ubuntu

This part is quite simple, from a terminal enter the following command:

sudo apt install postfix
Install Postfix

Confirm Postfix installation:

During installation, a wizard asks you for what use Postfix should be configured, leave Website and confirm .

Postfix configuration

The wizard also asks you to provide the FQDN name of the server.

Wait while installing Postfix…

Postfix is installed, we will now move on to configuring it.

Configuring Postfix

We will now move on to configuring Postfix.

The configuration files are located in the following location: /etc/postfix

The file that interests us at the moment is the file which contains the configuration elements. - postfix

The first element that we will configure is the mynetworks parameter, by default Postfix only authorizes access to its service on local addresses (

Comment the current mynetworks line:

#mynetworks = [::ffff:]/104 [::1]/128

Add this line below:

mynetworks =

Restart the Postftix service:

sudo systemctl restart postfix

The postfix server is now operational, for the moment emails will be transferred using DNS MX records.

We will now see different postfix configuration elements that can be found in businesses.

Transfer emails to a relay server

If you already have a mail relay and you want to forward messages to this relay, you must indicate the IP address of the relay or the DNS name in the relayhost parameter.

Open the /etc/postfix/ file.

Search :

relayhost =

Indicate the relay address here:

relayhost =

Restart the Postftix service:

sudo systemctl restart postfix

Transfer based on domain

Here we will see how to transfer depending on the recipient’s domain to a mail server and otherwise send to another relay which would not be in DMZ for example.

To illustrate the configuration, here is the diagram:

Messages for the domain must be sent to the Exchange server and messages for the domain are sent to the Exchange servers or All other messages are sent to the MTA at DMZ to be sent over the Internet.

For this we will use a transport map (transport_map).

Open the configuration file: /etc/postfix/

Before: # TLS parameters


# Transport
transport_maps = hash:/etc/postfix/transport

In the /etc/postfix folder, create a file named transport.

Open the transport file and paste the content below:    smtp:         smtp:,
*                           relay:[]:25

The configuration of the transport file must be adapted to your environment.

Build the transport index for Postfix:

sudo postmap /etc/postfix/transport

Restart Postfix:

sudo systemctl restart postfix

Now postfix will route messages based on the domain to the configured server otherwise it will be sent to another relay.

If you want to send directly from Postfix replace:

*                           relay:[]:25


*                           smtp

When using transport_maps, it is recommended not to configure the relayhost parameter, i.e. to leave it empty.

After each modification of the transport file, you must rebuild the index with the command sudo postmap /etc/postfix/transport and restart the Posfix service.

Postfix Troubleshooting

To check that it is working correctly, you will find the logs in the file: /var/log/mail.log

To display the list of pending messages : sudo mailq

To purge the queue : sudo postqueue -f

Some links on Postfix to go further if you wish:

Leave a Comment