Install Postfix MTA on Ubuntu 16.04 / 17.10 / 18.04

Postfix is a widely used Mail Transfer Agent (MTA) that most Linux systems use to router email messages… It’s probably the default MTA software on all of Linux distributions… This bref tutorial is going to show students and new users how to install and configure Postfix on Ubuntu 16.04 / 17.10 and 18.04 LTS servers..

In order to set up Postfix to work in a public environmnet, you’ll have to configure Postfix with a qualify domain name pointed to your server… ex. mail.example.com pointing to your Ubuntu servers… Don’t forget to point your MX records to the servers as well.

Once you’re done with the initial setup, continue below to get Postfix installed and configured on Ubuntu.

Step 1: Install Postfix on Ubuntu

To install Postfix on Ubuntu, simple run the commands below…

sudo apt install postfix sasl2-bin

During the installation, you’ll be prompted how you’d like to configure Postfix… for this tutorial, we’ll choose ‘No configuration‘… this will allow us to manually configure Postfix settings to suit yoiur environment… This gives you more control of how Postfix functions in your environements…

Package configuration

  ┌────────────────────────┤ Postfix Configuration ├────────────────────────┐
  │                                                                         │ 
  │ Please select the mail server configuration type that best meets your     
  │ needs.                                                                    
  │                                                                           
  │  No configuration:                                                        
  │   Should be chosen to leave the current configuration unchanged.          
  │  Internet site:                                                           
  │   Mail is sent and received directly using SMTP.                          
  │  Internet with smarthost:                                                 
  │   Mail is received directly using SMTP or by running a utility such       
  │   as fetchmail. Outgoing mail is sent using a smarthost.                  
  │  Satellite system:                                                        
  │   All mail is sent to another machine, called a 'smarthost', for          
  │ delivery.                                                                 
  │  Local only:                                                              
  │                                                                           
  │                             <Ok>                                                  
  │                                                                         │ 
  └─────────────────────────────────────────────────────────────────────────┘ 

When prompted to choose Postfix configuration, select ‘No configuration‘ option… We’re going to manually configure Postfix…

                    ┌──────┤ Postfix Configuration ├───────┐
                    │ General type of mail configuration:  │ 
                    │                                      │ 
                    │       No configuration               │ 
                    │       Internet Site                  │ 
                    │       Internet with smarthost        │ 
                    │       Satellite system               │ 
                    │       Local only                     │ 
                    │                                      │ 
                    │                                      │ 
                    │       <Ok>              <Cancel>     │ 
                    │                                      │ 
                    └──────────────────────────────────────┘ 

After installing continue below to configure…

Step 2: Configure Postfix

Now that Postfix is installed, run the commands below to create its default configuratiion file from the distribution template…

sudo cp /usr/share/postfix/main.cf.dist /etc/postfix/main.cf

Now that Postfix configuration file is created… run the commands below to open it…

sudo nano /etc/postfix/main.cf

Then use these settings to make it work in most environments…

# Global Postfix configuration file. This file lists only a subset
# of all parameters. For the syntax, and for a complete parameter
# list, see the postconf(5) manual page (command: "man 5 postconf").
#
# To avoid breaking things, Postfix will use backwards-compatible
# default settings and log where it uses those old backwards-compatible
# default settings, until the system administrator has determined
# if any backwards-compatible default settings need to be made
# permanent in main.cf or master.cf.
#
# When this review is complete, update the compatibility_level setting
# below as recommended in the RELEASE_NOTES file.
#
# The level below is what should be used with new (not upgrade) installs.
#
compatibility_level = 2
# by the mail_owner account (see below).
#
data_directory = /var/lib/postfix

# QUEUE AND PROCESS OWNERSHIP
#
# The mail_owner parameter specifies the owner of the Postfix queue
# and of most Postfix daemon processes. Specify the name of a user
# account THAT DOES NOT SHARE ITS USER OR GROUP ID WITH OTHER ACCOUNTS
# AND THAT OWNS NO OTHER FILES OR PROCESSES ON THE SYSTEM. In
# particular, don't specify nobody or daemon. PLEASE USE A DEDICATED
# USER.
#
mail_owner = postfix

# INTERNET HOST AND DOMAIN NAMES
#
# The myhostname parameter specifies the internet hostname of this
# mail system. The default is to use the fully-qualified domain name
# from gethostname(). $myhostname is used as a default value for many
# other configuration parameters.
#
myhostname = mail.example.com
#myhostname = virtual.domain.tld

# The mydomain parameter specifies the local internet domain name.
# The default is to use $myhostname minus the first component.
# $mydomain is used as a default value for many other configuration
# parameters.
#
mydomain = example.com

# Note: you need to stop/start Postfix when this parameter changes.
#
inet_interfaces = all
#inet_interfaces = $myhostname
#inet_interfaces = $myhostname, localhost
# See also below, section "REJECTING MAIL FOR UNKNOWN LOCAL USERS".
#
#mydestination = $myhostname, localhost.$mydomain, localhost
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
# mail.$mydomain, www.$mydomain, ftp.$mydomain

# The right-hand side of the lookup tables is conveniently ignored.
# In the left-hand side, specify a bare username, an @domain.tld
# wild-card, or specify a user@domain.tld address.
#
local_recipient_maps = unix:passwd.byname $alias_maps
#local_recipient_maps = proxy:unix:passwd.byname $alias_maps
#local_recipient_maps =
#
#mynetworks = 168.100.189.0/28, 127.0.0.0/8
#mynetworks = $config_directory/mynetworks
#mynetworks = hash:/etc/postfix/network_table
mynetworks = 127.0.0.0/8, 10.0.0.0/24
# It will take a minute or so before changes become visible. Use
# "postfix reload" to eliminate the delay.
#
#alias_maps = dbm:/etc/aliases
alias_maps = hash:/etc/aliases
#alias_maps = hash:/etc/aliases, nis:mail.aliases
#alias_maps = netinfo:/aliases
#alias_database = dbm:/etc/aliases
#alias_database = dbm:/etc/mail/aliases
alias_database = hash:/etc/aliases
#alias_database = hash:/etc/aliases, hash:/opt/majordomo/aliases
# DELIVERY TO MAILBOX
#
# The home_mailbox parameter specifies the optional pathname of a
# mailbox file relative to a user's home directory. The default
# mailbox file is /var/spool/mail/user or /var/mail/user. Specify
# "Maildir/" for qmail-style delivery (the / is required).
#
#home_mailbox = Mailbox
home_mailbox = Maildir/
# sendmail_path: The full pathname of the Postfix sendmail command.
# This is the Sendmail-compatible mail posting interface.
#
sendmail_path = /usr/sbin/postfix

# newaliases_path: The full pathname of the Postfix newaliases command.
# This is the Sendmail-compatible command to build alias databases.
#
newaliases_path = /usr/bin/newaliases

# mailq_path: The full pathname of the Postfix mailq command. This
# is the Sendmail-compatible mail queue listing command.
#
mailq_path = /usr/bin/mailq

# setgid_group: The group for mail submission and queue management
# commands. This must be a group name with a numerical group ID that
# is not shared with other accounts, not even with the Postfix account.
#
setgid_group = postdrop

#html_directory =
# manpage_directory: The location of the Postfix on-line manual pages.
#manpage_directory =
# sample_directory: The location of the Postfix sample configuration files.
#sample_directory =
#readme_directory =
# add to the end of file: limit an email size to 10M
message_size_limit = 10485760
# limit mailbox size to 1G
mailbox_size_limit = 1073741824

After editing the lines above, save the file….

Next, run the commands below and restart Postfix…..

sudo newaliases
sudo systemctl restart postfix

Postfix is now installed and ready to use…

If you want to use SMTP authentication, you may want to use Dovecot….

Dovecot is an open source IMAP and POP3 email server for Linux/UNIX-like systems, written with security primarily in mind. Dovecot is an excellent choice for both small and large installations. It’s fast, simple to set up, requires no special administration and it uses very little memory.

When using Dovecot… add the hightlighted configuration lines to the end of Postfix main.cf file…

# SMTP-Auth settings for Dovecot
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions = permit_mynetworks,permit_auth_destination,permit_sasl_authenticated,reject

Then restart Postfix and you’re done…

You may also like the post below:

Setup Opigno LMS on Ubuntu 16.04 / 17.10 / 18.04 with Nginx, MariaDB and PHP 7.1-FPM