How to Install and Configure Postfix on Ubuntu 20.04 for Reliable Email Server Setup
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.

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.

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.
Conclusion
Postfix is a powerful and widely used mail server that offers extensive functionality with relatively straightforward setup. In this tutorial, we covered the essential steps to install Postfix on Ubuntu 20.04, configure DNS records, set the hostname, and send a test email. After this basic configuration, your mail server is ready to operate. However, for full functionality and improved reliability, it is recommended to implement additional features such as encryption, DKIM, SPF, and virtual mailboxes. These enhancements will help ensure a secure, stable, and efficient mail server environment.
FAQ
- Q1: What DNS records are needed for Postfix to work properly?
A: You need to configure an A record for your mail subdomain (e.g., mail.domain.com) and an MX record for your root domain that points to this subdomain. - Q2: Can I use a dynamic IP address for my mail server?
A: It is strongly recommended to use a static public IP address, as dynamic IPs often get blacklisted and cause mail delivery issues. - Q3: Why do my emails end up in spam or get rejected by Gmail?
A: To improve email deliverability, you should configure SPF, DKIM, DMARC records and enable TLS encryption for your mail server. - Q4: How can I verify that Postfix is installed and working correctly?
A: You can send a test email using the mail command and check for successful delivery and reception, as well as monitor the mail server logs. - Q5: How do I set up virtual mailboxes with Postfix?
A: Setting up virtual mailboxes involves additional configuration of Postfix and creating virtual users, allowing mail delivery without system user accounts.


