How to relay email from Postfix mail server to Gmail on Ubuntu 22.04

This tutorial shows you how to configure Postfix mail server on Ubuntu 22.04 to relay email through Gmail. The first step is to install Postfix (if you have not done that yet).

Install Postfix SMTP server

sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules

Configure Postfix to relay email

Now open the Postfix main configuration file /etc/postfix/ in a text editor. I will use the nano editor here:

sudo nano /etc/postfix/

Add the following lines to the end of the file:

# Relay settings
relayhost = []:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes

Postfix Email relay settings

Next, we will have to create a file containing the Gmail username and password. This file will be named sasl_passwd file. There we add our Gmail account credentials:

sudo nano /etc/postfix/sasl_passwd

Postfix Sasl password file

Replace the username and password with your Gmail user and password. Add the following line:


Save and close the file.

Protect the password in the sasl_passwd file by making it only readable by root:

sudo chmod 600 /etc/postfix/sasl_passwd

Now we have to use postmap command to create a hashed version of the file:

sudo postmap /etc/postfix/sasl_passwd

Finally, restart Postfix to apply the changes:

sudo systemctl restart postfix

Now Postfix will relay all outgoing emails through your Gmail account. Please note that Gmail may require you to allow less secure apps to access your account for this to work.

Leave a Comment