Email postfix Ubuntu

How to Install and Configure Postfix on Ubuntu 18.04 LTS

Postfix is an open source Mail Transfer Agent (MTA). It is used by public mail servers all over the internet.  In this article, I will show you how to install and configure Postfix on Ubuntu 18.04 LTS.

Configuring DNS for Postfix:

To have a working configuration of Postfix, you must have a MX record added to your DNS server. I am setting up a DNS server using BIND 9 here just to demonstrate how everything works.

My Ubuntu 18.04 LTS machine has the IP address 192.168.199.170. This is going to be my DNS server and Mail server. So replace it with your IP address. I’ve chosen mail.linuxhint.com as the FQDN of my mail server. Replace it with yours.

First update the APT package repository cache with the following command:

$ sudo apt update

Install BIND 9 DNS server with the following command:

$ sudo apt install bind9

Now press y and then press <Enter> to continue.

BIND 9 should be installed.

Now create a zone file with the following command:

$ sudo nano /var/cache/bind/db.linuxhint

Now add the following lines and save the file. Replace 192.168.199.170 with the IP address of your computer. Also replace linuxhint.com with your domain name.

Now check whether the configuration file is okay with the following command:

$ sudo named-checkzone linuxhint.com. /var/cache/bind/db.linuxhint

Now run the following command to edit /etc/bind/named.conf.default-zones file:

$ sudo nano /etc/bind/named.conf.default-zones

Now add the following lines to configure your DNS zone. Adjust where necessary.

Now edit /etc/bind/named.conf.options file with the following command:

$ sudo nano /etc/bind/named.conf.options

Now uncomment forwarders (by removing // from the beginning of each line) and change 0.0.0.0 to Google’s DNS server 8.8.8.8

Before:

After:

Now restart BIND 9 with the following command:

$ sudo systemctl reload bind9

Now you have to change your DNS server. I used Network Manager to do that.

If you don’t have network manager installed, install it with the following command:

$ sudo apt install network-manager

List your network manager connection with the following command:

$ sudo nmcli connection show

As you can see, the name of my active connection is Wired connection 1

Now edit your connection with the following command:

$ sudo nmcli connection edit "Wired connection 1"

Remove the current DNS server:

nmcli> remove ipv4.dns

Now run the following nmcli command to change your DNS server for the connection.

nmcli> set ipv4.dns 192.168.199.170

Save the changes.

nmcli> save

Finally exit out of nmcli prompt:

nmcli> quit

Now restart the connection with the following commands:

$ sudo nmcli connection down "Wired connection 1"

$ sudo nmcli connection up "Wired connection 1"

Now set your hostname to mail.linuxhint.com with the following command:

$ sudo hostnamectl set-hostname mail.linuxhint.com

Now reboot your computer for the changes to take effect.

$ sudo reboot

Installing Postfix:

Now install Postfix with the following command:

$ sudo apt install postfix

Press <Tab> and then press <Enter> to continue.

Select Internet Site. Then press <Tab> to select <OK> and then press <Enter> to continue.

Type in your domain name. Then press <Tab> and then press <Enter> to continue.

Postfix should be installed and configured

Reconfiguring Postfix:

If you need to re-configure the Postfix server in future, run the following command:

$ sudo dpkg-reconfigure postfix

Terminal based Postfix configuration wizard should show up. Just answer the questions and you’re done.

Configuring Users for Sending and Receiving E-Mails:

On Ubuntu 18.04 LTS, only users added to the mail group can send and receive E-Mails. By default, none of the users are added to the mail group.

You can add the user you’re logged in as to the mail group with the following command:

$ sudo usermod -aG mail $(whoami)

If the user you’re logged in as don’t have sudo or super user privileges, you can add user, let’s say linda to the mail group with the following command:

$ sudo usermod -aG mail linda

If you want to create new users for sending and receiving E-Mails, run the following command:

$ sudo useradd -m -G mail USERNAME

Here, USERNAME is the username of your new user.

In my case, let’s say the USERNAME is Kelly:

$ sudo useradd -m -G mail -s /bin/bash kelly

Now run the following command to set a login password for the USERNAME kelly:

$ sudo passwd Kelly

Now enter the password for the user kelly and press <Enter>.

You will have to re-type the password again. Once you’re done, the password should be set.

Now the user kelly can send and receive E-Mails.

Sending and Reading E-Mails from the Command Line:

Ubuntu 18.04 LTS has many command line tools for sending emails such as Sendmail, mail, mutt etc.

In this section, I am going to show you how to use the mail command to send and read E-mails.

The mail program may not be installed by default on Ubuntu 18.04 LTS. Run the following command to install it:

$ sudo apt install mailutils

Now press y and then press <Enter> to continue.

It should be installed.

Now you can send an E-Mail with the following command:

$ mail EMAIL_ADDRESS

Let’s send an E-Mail to kelly,

$ mail kelly@linuxhint.com

You should be asked to provide Carbon Copy (CC). If you want to forward it to many mail addresses, type in the email addresses here. Otherwise leave it empty and press <Enter>.

Now type in the subject of your E-Mail. Once you’re done, press <Enter>.

Now type in your email.

Once you’re done, press <Ctrl> + d.

The mail should be sent.

Now kelly can read the E-Mail with the following command:

$ mail

As you can see, kelly received my E-Mail.

Now type in the number as marked below and press <Enter> to read the E-Mail:

This is the E-Mail I sent kelly.

Now kelly can send an E-Mail to me as well.

I can see kelly’s email.

This is kelly’s email.

That’s how you install and configure Postfix on Ubuntu 18.04 LTS. Thanks for reading this article.

About the author

Shahriar Shovon

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.