Setting up Postfix on a Ubuntu VM for sending emails

Ward Poel

Postfix is an open-source Mail Transfer Agent for Linux systems.

Ubuntu VM Settings

Installing Postfix

First of all we update your local apt cache:

sudo apt update

Then install postfix with the following command:

sudo apt install postfix

This installation will open an interactive prompt. The first prompt will ask what type of mail configuration you want. Select Internet Site and continue.

In the second prompt you will be asked to give your system mail name. In this tutorial we will use the domain So fill in as system mail name.

Note: If you ever need to return to this interactive prompt windows. You can use the following command:

sudo dpkg-reconfigure postfix

Changing the Postfix configurations

We need to tell Postfix to use the correct hostname and destination. This can be changed in the file /etc/postfix/

sudo nano /etc/postfix/

Change the fields myhostname and mydestination:

myhostname =

mydestination = $myhostname,, localhost.localdomain, localhost

Change your Ubuntu VM hostname

Make sure /etc/hostname contains your domain you want to mail from.

Also update your /etc/hosts file: ubuntu-vm ubuntu-vm localhost ubuntu-vm

ubuntu-vm is the name of the machine, you don't have to change that. Just make sure you add your domain.

Update the hostnamectl with the following command:

hostnamectl set-hostname

Setting up DomainKeys Identified Mail (DKIM)

DKIM is an email authentication method designed to detect email spoofing.

The first step is to generate a public and private key. Make sure you generate these keys with openssl.

# Generate private key
openssl genrsa -out private.key 1024

# Generate public key based on private key
openssl rsa -in private.key -pubout -out public.key

You will need this public key for your DNS records.

Please keep in mind that using DKIM does not encrypt the content of your mail.

DNS Settings

For example IP we use:

DNS Type: Hostname - Value

A: -

MX: -

TXT: - v=spf1 a mx ip4: ~all


!! Make sure you setup reverse DNS (rDNS) for you IP. rDNS is a protocal used to translate the sending server IP address into its host name ( This is needed so the receiving mail server can check if you are sending mails from a server that is connected to that IP address. If you need to do this with DNS, you need to setup a PTR record. !!

When using a DigitalOcean droplet, just change the name of your droplet to your domain. In this example You can change the name of a droplet by just clicking on the droplet name.

Please keep in mind that DNS changes may take some time

That's it! Your server and domain are now configured for sending mails 🥳

Interesting posts:

If you have any questions, I'm @WardPoel on Twitter.