Postfix is a widely used open source Mail Transfer Agent (MTA) for routing and delivering mails on the internet. The SMTP (Simple Mail Transfer Protocol) server started life in 1997 and was originally written by Wietse Venema.
If you would like to get crucial emails from your Ubuntu 18.04 server (e.g. from cron jobs, automated backups completion, e.t.c) you can install Postfix as a send-only mail server.
Postfix is easy to administer, fast and resource efficient. The email server is also security focused and has an active development team. According to a recent report by Security Space, Postfix controls 33% of all emails sent on the web as of June 2018.
In this guide, we focus on the steps needed to completely setup Postfix on your Ubuntu 18.04 to ensure that you don’t miss those crucial emails from your system.
- A domain name
- An Ubuntu 18.04 server
- A non-root user with sudo privileges
Step 1: Point your server to a Fully Qualified Domain Name (FQDN)
The first thing is to make sure that your server hostname is set to a Fully Qualified Domain Name.(FQDN).
A FQDN is a unique address that identifies your server on the internet and consists of two parts; a hostname(e.g. mail or pluto or nyc ) and a domain name(e.g. example.com).
So the basic syntax of a FQDN should be:
$ [host name].[domain].[tld].
To setup a FQDN on your Ubuntu 18.04 server, first, edit the ‘/etc/hostname’ file and change the name to ‘mail’. You can use any other name depending on your needs and most people use names of planets, cities e.t.c
$ sudo nano /etc/hostname
‘/etc/hostname’ File excerpt
Press CTRL +X, Y and Enter to save the changes on that file.
To enter the domain name part and create a FQDN, we will need to open the ‘etc/hosts’ file:
$ sudo nano /etc/hosts
Set the values below at the top of the file. Remember to replace ‘example.com’ and ‘198.51.100.3’ with your domain name and server public IP address respectively.
127.0.0.1 localhost 127.0.1.1 mail.example.com mail 198.51.100.3 mail.example.com mail
Step 2: Install Postfix
Update the package information by running the command below:
$ sudo apt-get update
To install Postfix with all associated utilities for testing out the email, run the command below:
$ sudo apt-get install mailutils
Before the installation is completed, you will be presented with an option to customize postfix.
On the General type of email configuration, choose ‘Internet Site’. Press, TAB to highlight ‘OK’ and then press Enter.
On the ‘System mail name’ field, enter your domain name (e.g. example.com NOT ‘mail.example.com’)
Press TAB again and Enter to proceed. Your postfix installation will now be completed successfully.
Step 3: Configure Postfix
Most Postfix configuration settings are found on the ‘/etc/postfix/main.cf’ file. We will do one major change on this file to ensure that Postfix can only send emails coming from our local server to avoid it from being used as an open relay by spammers.
To do this open the configuration file using a nano editor:
$ sudo nano /etc/postfix/main.cf
Look for the entry ‘inet_interface’ and change its value from ‘all’ to ‘loopback-only’
inet_interfaces = loopback-only
Also, check the entry ‘myhostname’ and change its value to the fully qualified domain name of your server
Press CTRL + X, Y and Enter to save the changes
Step 4: Restart Postfix
Once you make the changes, you need to reload Postfix server to pick the new changes by running the command below:
$ sudo service postfix restart
Step 5: Test the email sending functionality
We installed Postfix together with all associated mail utilities for sending emails. So we can use the command below to test if our email server is working as expected. To do this, run the command below:
$ echo "This email confirms that Postfix is working" | mail -s "Testing Posfix" firstname.lastname@example.org
Remember to replace email@example.com with the right address where you want the email to be sent. You can send the email to your Yahoo, Google or Microsoft account.
You can now go ahead and check your email address and see if the email was delivered successfully. Remember to check the spam folder
That’s all when it comes to configuring and setting up Postfix as an email server on your Ubuntu 18.04 VPS. If you get any problems, check with your hosting provider if they block port 25 and if they do ask them if they can enable it for you. If you followed the guide, email notifications from your server should work pretty well without any problems.