11.12.2024

How to Install and Configure Postfix on Ubuntu 20.04

Postfix is a fairly common mail server due to sufficient functionality for most tasks and easy setup. This tutorial will cover the installation and basic configuration of Postfix and the server for it.

Preparation

The server must have a static public IP address. You will also need a domain name. Let's first configure the necessary DNS records.
For the mail server, a 3rd level subdomain is usually used. As an example, we will use mail.domain-name.com. Replace the domain-name.com with your domain name. The 3rd level domain name can be arbitrary. The main thing is to use the appropriate option in further configuration. DNS records can usually be configured in the control panel of a domain name registrar or on your DNS server.
Add a DNS A record for your domain named mail and "target" equal to the IP address of the future mail server.

mail A xxx.xxx.xxx.xxx

Add DNS MX record for the root domain with a value equal to the domain name of the mail server:

@ MX mail.domain-name.com

If the service returns an error as a result, try not to use nothing instead of @.

Server configuration

With the selected 3rd level domain name mail.domain-name.com, the hostname will be mail. Let’s configure it.

sudo hostnamectl set-hostname mail

Now open the hosts file.

sudo nano /etc/hosts

Let's add a line for this server with its IP address, your domain name and hostname. This entry must be placed directly below the entry for localhost.

xxx.xxx.xxx.xxx domain-name.com mail

Postfix installation

Let’s install Postfix and mail package mailutils.

sudo apt install postfix mailutils

During installation, you will be prompted for options interactively. Set mail server configuration type to Internet Site.

Screenshot 1. Set mail server configuration type.

System mail name must match the domain name for which we are setting up the mail server. In our case, this is domain-name.com.

Screenshot 2. Set system mail name.

Postfix testing

Now the Postfix mail server is ready to work. Let’s test it and send our first email. Put your mail to the following command and run it:

echo "Postfix test" | mail -s "Subject" mail@server.com

Due to the fact that encryption and things like DKIM are not yet configured, your letter will most likely end up in the Spam folder. Some mail servers like gmail.com won't accept it at all. We'll fix this later.
Now you can reply to this letter in your mailbox and see this answer on the mail server.

mail

Output:

"/var/mail/root": 1 message 1 new
>N 1 Sender Name Wed Mar 17 04:32 66/2877 RE: Subject
?

Enter 1 and you will see your reply.

The next step is to set up Postfix to use virtual mailboxes.