How to install Postfix in Ubuntu

Shubham Soni
3 min readNov 19, 2023

--

To install Postfix on Ubuntu, you can use the following steps. Postfix is a popular mail server that can be used to send and receive emails on a Linux system.

Update Package List:

Before installing any new software, it’s a good idea to update the package list to ensure you get the latest versions of packages.

$sudo apt update

1.) Install the packages:

$sudo apt-get install postfix -y
$sudo apt-get install mailx -y

To send and receive mail you need to install some packages.

2.) Postfix configurations:

To configure the postfix go to the location

$cd /etc/postfix

Now, we have to work on the main.cf file. So for that, create a backup for the file so that the main data is secure in the backup file

cp main.cf main.cf_bkp

open the file in any editor and edit file

vim main.cf

{ADD THE FOLLOWING LINES}

relayhost = [smtp.gmail.com]:587
myhostname = <your hostname/SURAJ-LAPTOP>

587 is the port for secure TLS

To check your hostname use “hostname -f”

#Location of sasl_passwd we saved
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
#Enables SASL authentication for postfix
smtp_sasl_auth_enable = yes
smtp_tls_security_level = encrypt
#Disallow methods that allow anonymous authentication
smtp_sasl_security_options = noanonymous

3.) now create a file at /etc/postfix/sasl/ named sasl_passwd

cd /etc/postfix/sasl/
vim sasl_passwd

Add the below line and change the “email.gmail” to your Gmail.

[smtp.gmail.com]:587 email@gmail.com:<password>

Go to mail which is used here and go to security and ENABLE 2-STEP VERIFICATION

Now go to app passwords in security => Click on select app => Choose other

Write “smtp” and click on generate

It will show a window in which a password is written. Copy the password and paste it in the above file “sasl_passwd” at <password>.

4.) Convert the sasl_passwd file into db file

postmap /etc/postfix/sasl/sasl_passwd

a new file sasl_passwd.db is created here

as it contain sensitive info so we only allow user to read and write permissions on this file

chmod 600 /etc/postfix/sasl/*

5.) start the postfix service

# To start the postfix service
systemctl start/enable postfix
# To stop the postfix service
systemctl stop/disable postfix
# To re-start the postfix service
systemctl restart postfix

6.) How to send an email

echo "Test Mail" | mail -s "Postfix TEST" email@gmail.com

7.) If you encounter any kind of error then go to main.cf file and comment on the line

# smtp_tls_security_level = may

After editing the file do

systemctl restart postfix.service

8.) to attach any file with the mail

Create a file testfile

vim testfile
#write something in the file to verify
"Suraj soni attach a file with the mail"

Save the file

echo "Test Mail" | mail -s "Postfix TEST" -a <filename/testfile> email@gmail.com

After all steps are completed, and to verify whether the Postfix service is working or not you can check Gmail and there you will see a mail regarding the same.

--

--