How to install and configure Postfix smtp relay using Gmail on ubuntu 16.04 | 18.04 | 20.04

Postfix is an Mail Transfer Agent , Its is very powerful and easy to configure email server, On this post we will install and configure smtp relay using Gmail.

To configure Postfix as a relay smtp server using Gmail, You must have a Gmail email id and password as well as You have allow less-secure in your gmail account to allow postfix to use the gmail credentials.

Updating hostname

Step 1:- To install all required dependencies by following commands with root privileges, You must set your machine hostname like

sudo -i

hostnamectl set-hostname

Now you need to reboot the ubuntu machine to get effects on machine hostname.

Installation dependencies and Postfix packages.

Step 2:- After this we can performed the required packages.

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

Note :- While installing Postfix, You wiil be asked for hostname name you need enter your after configuration of your FQDN within postfix, We are ready to configure smmtp relay with Gmail credentials.

Step 3:- After this We need to specified our Gmail username and password by following the commands.

vim /etc/postfix/sasl_passwd

You need to replace your username and password in the file


Save and exit from the editor.

Postfix configuration

Step 4:- In next step, You need to open the postfix mail config with by using following commands.

vim /etc/postfix/

Add the following the lines.

relayhost = []:587

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

Save and exit from your editor.

Update the permission

Step 5:- Now you need to update the permission of sasl_passwd file:

chmod 400 /etc/postfix/sasl_passwd

postmap /etc/postfix/sasl_passwd

Step 6:- After this you need to verify the certificate certificates by following the commands.

cat /etc/ssl/certs/thawte_Primary_Root_CA.pem | sudo tee -a /etc/postfix/cacert.pem

You should get output like this :-




In  case you did not get ant signature of the certificate so then you need to use

to Thawte_Premium_Server_CA.pem 

Restart the Postfix service

Step 7:- After getting certificate signature details all thing is going good, Now the time has come to restart the postfix the services to get effects.

systemctl restart postfix

Testing Postfix 

Step 8:- After this we need to test the postfix is working or not, To do that you need to You need to use the given commands to sent a test email to your email address to verify its proper working or not.

echo "Postfix email from smtp relay server" | mail -s "Testing Postfix"

Note :- If you want to use postfix smtp relay in your monitoring tools like Nagios you have postfix commands, To get your mail command path by using the following cli.

which mail

You should get output like this – /usr/bin/mail this is use in your Nagios SMTP configuration with Postfix.

How to install and configure Postfix smtp relay using Gmail on ubuntu 16.04 | 18.04 | 20.04

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top