Tutorial

How To Install and Configure Postfix on Ubuntu 20.04

Published on May 21, 2020
English
How To Install and Configure Postfix on Ubuntu 20.04
Not using Ubuntu 20.04?Choose a different version or distribution.
Ubuntu 20.04

A previous version of this tutorial was written by Justin Ellingwood

Introduction

Postfix is a popular open-source Mail Transfer Agent (MTA) that can be used to route and deliver email on a Linux system. It is estimated that around 25% of public mail servers on the internet run Postfix.

In this guide, you’ll learn how to install and configure Postfix on an Ubuntu 20.04 server. Then, you’ll test that Postfix is able to correctly route mail by installing s-nail, a Mail User Agent (MUA), also known as an email client.

Note that the goal of this tutorial is to help you get Postfix up and running quickly with only some bare-bones email functionality. You won’t have a full featured email server by the end of this guide, but you will have some of the foundational components of such a setup to help you get started.

Prerequisites

Setting up and maintaining your own mail server is complicated and time-consuming. For most users, it’s more practical to instead rely on a paid mail service. If you’re considering running your own mail server, we encourage you to review this article on why you may not want to do so.

If you’re sure you want to follow this guide to install and configure Postfix, then you must first have the following:

  • A server running Ubuntu 20.04 to function as your Postfix mail server. This server should have a non-root user with sudo privileges and a firewall configured with UFW. You can follow our Ubuntu 20.04 initial server setup guide to set this up.
  • A Fully Qualified Domain Name pointed at your Ubuntu 20.04 server. You can find help on setting up your domain name with DigitalOcean by following our Domains and DNS Networking documentation. Be aware that if you plan on accessing mail from an external location, you will need to make sure you have an MX record pointing to your mail server as well.

Note that this tutorial assumes that you are configuring a host that has the FQDN of mail.example.com. Wherever necessary, be sure to change example.com or mail.example.com to reflect your own FQDN.

Step 1 — Installing Postfix

Postfix is included in Ubuntu’s default repositories, so you can install it with APT.

To begin, update your local apt package cache:

  1. sudo apt update

Then install the postfix package with the following command. Note that here we pass the DEBIAN_PRIORITY=low environmental variable into this installation command. This will cause the installation process to prompt you to configure some additional options:

  1. sudo DEBIAN_PRIORITY=low apt install postfix

This installation process will open a series of interactive prompts. For the purposes of this tutorial, use the following information to fill in your prompts:

  • General type of mail configuration?: For this, choose Internet Site since this matches our infrastructure needs.
  • System mail name: This is the base domain used to construct a valid email address when only the account portion of the address is given. For instance, let’s say the hostname of your server is mail.example.com. You will likely want to set the system mail name to example.com so that, given the username user1, Postfix will use the address user1@example.com.
  • Root and postmaster mail recipient: This is the Linux account that will be forwarded mail addressed to root@ and postmaster@. Use your primary account for this. In this example case, sammy.
  • Other destinations to accept mail for: This defines the mail destinations that this Postfix instance will accept. If you need to add any other domains that this server will be responsible for receiving, add those here. Otherwise, the default will be sufficient.
  • Force synchronous updates on mail queue?: Since you are likely using a journaled filesystem, accept No here.
  • Local networks: This is a list of the networks for which your mail server is configured to relay messages. The default will work for most scenarios. If you choose to modify it, though, make sure to be very restrictive in regards to the network range.
  • Mailbox size limit: This can be used to limit the size of messages. Setting it to 0 disables any size restriction.
  • Local address extension character: This is the character that can be used to separate the regular portion of the address from an extension (used to create dynamic aliases). The default, + will work for this tutorial.
  • Internet protocols to use: Choose whether to restrict the IP version that Postfix supports. For the purposes of this tutorial, pick all.

To be explicit, these are the settings used in this guide:

  • General type of mail configuration?: Internet Site
  • System mail name: example.com (not mail.example.com)
  • Root and postmaster mail recipient: The username of your primary Linux account (sammy in our examples)
  • Other destinations to accept mail for: $myhostname, example.com, mail.example.com, localhost.example.com, localhost
  • Force synchronous updates on mail queue?: No
  • Local networks: 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
  • Mailbox size limit: 0
  • Local address extension character: +
  • Internet protocols to use: all

Note: If you need to ever return to change these settings, you can do so by typing:

  1. sudo dpkg-reconfigure postfix

The prompts will be pre-populated with your previous responses.

When the installation process finishes, you’re ready to make a few updates to your Postfix configuration.

Step 2 — Changing the Postfix Configuration

Now you can adjust some settings that the package installation process didn’t prompt you for. Many of Postfix’s configuration settings are defined in the /etc/postfix/main.cf file. Rather than editing this file directly, you can use Postfix’s postconf command to query or set configuration settings.

To begin, set the location for your non-root Ubuntu user’s mailbox. In this guide, we’ll use the Maildir format, which separates messages into individual files that are then moved between directories based on user action. The alternative option that isn’t covered in this guide is the mbox format, which stores all messages within a single file.

Set the home_mailbox variable to Maildir/. Later, you will create a directory structure under that name within your user’s home directory. Configure home_mailbox by typing:

  1. sudo postconf -e 'home_mailbox= Maildir/'

Next, set the location of the virtual_alias_maps table, which maps arbitrary email accounts to Linux system accounts. Run the following command, which maps the table location to a hash database file named /etc/postfix/virtual:

  1. sudo postconf -e 'virtual_alias_maps= hash:/etc/postfix/virtual'

Now that you’ve defined the location of the virtual maps file in your main.cf file, you can create the file itself and begin mapping email accounts to user accounts on your Linux system. Create the file with your preferred text editor; in this example, we’ll use nano:

  1. sudo nano /etc/postfix/virtual

List any addresses that you wish to accept email for, followed by a whitespace and the Linux user you’d like that mail delivered to.

For example, if you would like to accept email at contact@example.com and admin@example.com and would like to have those emails delivered to the sammy Linux user, you could set up your file like this:

/etc/postfix/virtual
contact@example.com sammy
admin@example.com sammy

After you’ve mapped all of the addresses to the appropriate server accounts, save and close the file. If you used nano, do this by pressing CTRL + X, Y, then ENTER.

Apply the mapping by typing:

  1. sudo postmap /etc/postfix/virtual

Restart the Postfix process to be sure that all of your changes have been applied:

  1. sudo systemctl restart postfix

Assuming you followed the prerequisite Initial Server Setup guide, you will have configured a firewall with UFW. This firewall will block external connections to services on your server by default unless those connections are explicitly allowed, so you’ll have to add a firewall rule to allow an exception for Postfix.

You can allow connections to the service by typing:

  1. sudo ufw allow Postfix

With that, Postfix is configured and ready to accept external connections. However, you aren’t yet ready to test it out with a mail client. Before you can install a client and use it to interact with the mail being delivered to your server, you’ll need to make a few changes to your Ubuntu server’s setup.

Step 3 — Installing the Mail Client and Initializing the Maildir Structure

In order to interact with the mail being delivered, this step will walk you through the process of installing the s-nail package. This is a feature-rich variant of the BSD xmail client which can handle the Maildir format correctly.

Before installing the client, though, it would be prudent to make sure your MAIL environment variable is set correctly. s-nail will look for this variable to figure out where to find mail for your user.

To ensure that the MAIL variable is set regardless of how you access your account — whether through ssh, su, su -, or sudo, for example — you’ll need to set the variable in the /etc/bash.bashrc file and add it to a file within /etc/profile.d to make sure it is set for all users by default.

To add the variable to these files, type:

  1. echo 'export MAIL=~/Maildir' | sudo tee -a /etc/bash.bashrc | sudo tee -a /etc/profile.d/mail.sh

To read the variable into your current session, source the /etc/profile.d/mail.sh file:

  1. source /etc/profile.d/mail.sh

With that complete, install the s-nail email client with APT:

  1. sudo apt install s-nail

Before running the client, there are a few settings you need to adjust. Open the /etc/s-nail.rc file in your editor:

  1. sudo nano /etc/s-nail.rc

At the bottom of the file, add the following options:

/etc/s-nail.rc
. . .
set emptystart
set folder=Maildir
set record=+sent

Here’s what these lines do:

  • set emptystart: allows the client to open even with an empty inbox
  • set folder=Maildir: sets the Maildir directory to the internal folder variable
  • set record=+sent creates a sent mbox file for storing sent mail within whichever directory is set as the folder variable, in this case Maildir

Save and close the file when you are finished. You’re now ready to initialize your system’s Maildir structure.

A quick way to create the Maildir structure within your home directory is to send yourself an email with the s-nail command. Because the sent file will only be available once the Maildir is created, you should disable writing to it for this initial email. Do this by passing the -Snorecord option.

Send the email by piping a string to the s-nail command. Adjust the command to mark your Linux user as the recipient:

  1. echo 'init' | s-nail -s 'init' -Snorecord sammy

Note: You may get the following response:

Output
Can't canonicalize "/home/sammy/Maildir"

This is normal and may only appear when sending this first message.

You can can check to make sure the directory was created by looking for your ~/Maildir directory:

  1. ls -R ~/Maildir

You will see the directory structure has been created and that a new message file is in the ~/Maildir/new directory:

Output
/home/sammy/Maildir/: cur new tmp /home/sammy/Maildir/cur: /home/sammy/Maildir/new: 1463177269.Vfd01I40e4dM691221.mail.example.com /home/sammy/Maildir/tmp:

Now that the directory structure has been created, you’re ready to test out the s-nail client by viewing the init message you sent and sending a message to an external email address.

Step 4 — Testing the Client

To open the client, run the s-nail command:

  1. s-nail

In your console, you’ll see a rudimentary inbox with the init message waiting:

Output
s-nail version v14.9.15. Type `?' for help "/home/sammy/Maildir": 1 message 1 new >N 1 sammy@example.com 2020-05-19 15:40 14/392 init

Press ENTER to display the message:

Output
[-- Message 1 -- 14 lines, 369 bytes --]: From sammy@example.com Tue May 19 15:40:48 2020 Date: Tue, 19 May 2020 15:40:48 +0000 To: sammy@example.com Subject: init Message-Id: <20160513220749.A278F228D9@mail.example.com> From: sammy@example.com init

You can get back to the message list by typing h, and then ENTER:

  1. h
Output
>R 1 sammy@example.com 2020-05-19 15:40 14/392 init

Notice that the message now has a state of R, indicating that it’s been read.

Since this message isn’t very useful, you can delete it by pressing d, and then ENTER:

  1. d

To get back to the terminal, type q and then ENTER:

  1. q

As a final test, check whether s-nail is able to correctly send email messages. To do this, you can pipe the contents of a text file into the s-nail process, like you did with the init message you sent in the previous step.

Begin by writing a test message in a text editor:

  1. nano ~/test_message

Inside, enter some text you’d like to send:

~/test_message
Hello,

This is a test.  Please confirm receipt!

Save and close the file after writing your message.

Then, use the cat command to pipe the message to the s-nail process. You can do so with the following example, which uses these options:

  • -s: This defines the subject line of the email message
  • -r: An optional change to the “From:” field of the email. By default, the Linux user you are logged in as will be used to populate this field. The -r option allows you to override this with a valid address, such as one of those you defined in the /etc/postfix/virtual file. To illustrate, the following command uses contact@example.com

Also, be sure to change user@email.com to a valid email address which you have access to:

  1. cat ~/test_message | s-nail -s 'Test email subject line' -r contact@example.com user@email.com

Then, navigate to the inbox for the email address to which you sent the message. You will see your message waiting there almost immediately.

Note: If the message isn’t in your inbox, it may have been delivered to your Spam folder.

You can view your sent messages within your s-nail client. Start the interactive client again:

  1. s-nail

From the email client, view your sent messages by typing:

  1. file +sent

You’ll see output like this:

Output
+[/home/sammy/Maildir/]sent: 1 message 1 new ▸N 1 contact@example.com 2020-05-19 15:47 12/297 Test email subject line

You can manage sent mail using the same commands you use for incoming mail.

Conclusion

You now have Postfix configured on your Ubuntu 20.04 server. Managing email servers can be a tough task for new system administrators, but with this configuration, you should have enough MTA email functionality to get yourself started.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Default avatar

Manager, Developer Education

Technical Writer @ DigitalOcean


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

As others mentioned, cant get test mails to work and nothing appears. Also it was noted that SMTP could be disabled for new accounts.

Step I’m stuck at: ls -R ~/Maildir

mailq shows this connection timed-out error:

-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient------- 811D513BD4C 373 Mon Jan 4 17:04:12 root@xxx.com (connect to alt2.gmail-smtp-in.l.google.com[74.125.137.26]:25: Connection timed out)

I’ve done up to this stage: $echo ‘init’ | s-nail -s ‘init’ -Snorecord sammy

But I get the following: ls: cannot access ‘/home/sammy/Maildir’: No such file or directory

Please note that when I created a new user I literally used the same name “Sammy” (as in the tutorial)

stuck at step 3 when I type echo ‘init’ | s-nail -s ‘init’ -Snorecord relaxslow and enter it shows nothing and next I type ls -R ~/Maildir it gives error msg ls: cannot access ‘/home/relaxslow/Maildir’: No such file or directory

can you please tell me the reason

As many have angrily noted in the comment section of the older 18.04 article, I too failed to send email to my default Gmail address after following this tutorial. Here is an excerpt from my /var/log/mail.log, domains and email addresses replaced.

postfix/pickup[11583]: F274EBEB1B: uid=1000 from=<info@mydomain.org>
postfix/cleanup[12288]: F274EBEB1B: message-id=<20200626102426.unOmh%info@mydomain.org>
postfix/qmgr[11584]: F274EBEB1B: from=<info@mydomain.org>, size=374, nrcpt=1 (queue active)
postfix/smtp[12290]: connect to gmail-smtp-in.l.google.com[2a00:1450:4013:c01::1b]:25: Connection timed out
postfix/smtp[12290]: F274EBEB1B: to=<my.email@gmail.com>, relay=gmail-smtp-in.l.google.com[108.177.126.26]:25, delay=30, delays=0.02/0.02/30/0.3, dsn=5.7.28, status=bounced (host gmail-smtp-in.l.google.com[108.177.126.26] said: 550-5.7.28 [37.139.21.211       1] Our system has detected an unusual rate of 550-5.7.28 unsolicited mail originating from your IP address. To protect our 550-5.7.28 users from spam, mail sent from your IP address has been blocked. 550-5.7.28 Please visit 550-5.7.28  https://support.google.com/mail/?p=UnsolicitedIPError to review our 550 5.7.28 Bulk Email Senders Guidelines. y24si415499edv.573 - gsmtp (in reply to end of DATA command))
postfix/cleanup[12288]: 758D6BEB1D: message-id=<20200626102457.758D6BEB1D@myserver>
postfix/qmgr[11584]: 758D6BEB1D: from=<>, size=3144, nrcpt=1 (queue active)
postfix/bounce[12294]: F274EBEB1B: sender non-delivery notification: 758D6BEB1D
postfix/qmgr[11584]: F274EBEB1B: removed

The error “Our system has detected an unusual rate of unsolicited mail originating from your IP address” is rather weird because this is the first mail ever sent from my Droplet. Maybe the IP has been previously been a source of spam but I think insufficient configuration is the most probable cause. No one in the older article seemed to be able to provide a sounding fix to this issue. I hope DO can.

This comment has been deleted

    1)

    sudo nano /etc/postfix/virtual
    

    It didn’t work for me (postfix 550 5.1.1 “User doesn’t exist”). So I removed this step and remove it in /var/postfix/main.cf

    2)

    echo 'init' | s-nail -s 'init' -Snorecord sammy
    

    It didn’t work either, so I create the folded by hand:

    mkdir ~/Maildir
    mkdir ~/Maildir/cur
    mkdir ~/Maildir/new
    mkdir ~/Maildir/tmp
    

    and I checked the permissions and it worked.

    Also

    3) While I opened the firewall but I also opened the firewall of my virtual machine (IP port 25)

    4) I added the next entries to the DNS:

    • MX: <domain>, <domain>, 10
    • TXT: _dmarc, “v=DMARC1; p=none; rua=mailto:sender@<domain>”
    • TXT: <domain>, “v=spf1 ip4:<ip> include:<domain> +all”

    changing the IP and the domain for the right values.

    tnX for guide but i get this error

    E: Package ‘postfix’ has no installation candidate

    i did sudo apt update still same error what now?

    Like a lot of people, I was having issues getting this to work. Maildir/ was not being created, running $s_nail was outputting

    user@ip-xx-xx-xx-xx:/$ s-nail s-nail: /home/user/Maildir: Is a directory

    I eventually got this to work, by not using sudo to restart postfix, instead I used systemctl restart Postfix and authenticated with my user password. This then started postfix under the correct user, which then allowed it to read the config file and initiate the maildir/ under the correct user.

    I also agree with mattkeaveney in that procmail should be disabled in the initial set up of postfix.

    Same issue echo 'init' | s-nail -s 'init' -Snorecord sammy doesn’t create Maildir structure

    I’ve to create it manually: mkdir -p ~/Maildir/{cur,new,tmp}

    Hello, If the MailDir mailbox has not been created, try to leave a space at the end before “/”

    home_mailbox = Maildir /
    

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.