Postfix is a free and open-source mail transfer agent that routes and delivers electronic mail. It is estimated that around 25% of public mail servers on the internet run Postfix.
In this guide, we will learn how to install and configure Postfix on an Ubuntu 20.04 server. We will use
s-nail (a Mail User Agent – MUA) to test that Postfix is able to correctly route mail.
We will be able to get the Postfix up and running with some bare bone 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.
To follow this guide to install and configure Postfix, then you must first have the following:
- An Ubuntu 20.04 server to function as a Postfix server
- A Fully Qualified Domain Name pointed at your Ubuntu 20.04 server.
Note that this tutorial assumes that you are configuring a host that has the FQDN of
mail.citizix.com. Wherever necessary, be sure to change
mail.citizix.com to reflect your own FQDN.
Step 1: Ensure that your server is up to date
Before proceeding, ensure that the server has updated packages. Use this command to achieve that:
sudo apt update sudo apt -y upgrade
Step 2: Installing Postfix
Postfix is included in the default Ubuntu repositories as
postfix. We can install it with APT.
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:
sudo DEBIAN_PRIORITY=low apt install postfix
This installation process will open a series of interactive prompts. 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.citizix.com. You will likely want to set the system mail name to
citizix.comso that, given the username
user1, Postfix will use the address
- Root and postmaster mail recipient: This is the Linux account that will be forwarded mail addressed to
postmaster@. Use your primary account for this. In this example case, admin.
- 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
0disables 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.
Note: If you need to ever return to change these settings, you can do so by typing:
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 3: Changing the Postfix Configuration
Postfix’s configuration settings are defined in the
/etc/postfix/main.cf file. You can use Postfix’s
postconf command to query or set configuration settings rather than editing the file directly.
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.
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:
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
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
sudo vim /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
[email protected] and
[email protected] and would like to have those emails delivered to the admin Linux user, you could set up your file like this:
[email protected] admin [email protected] admin
After you’ve mapped all of the addresses to the appropriate server accounts, save and close the file.
Apply the mapping by typing:
sudo postmap /etc/postfix/virtual
Restart the Postfix process to be sure that all of your changes have been applied:
sudo systemctl restart postfix
If you have ufw firewall installed and enabled, you can allow connections to the service by typing:
sudo ufw allow Postfix
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
s-nail will look for this variable to figure out where to find mail for your user.
To ensure that the
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:
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
With that complete, install the
s-nail email client with APT:
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:
sudo vim /etc/s-nail.rc
At the bottom of the file, add the following options:
. . . 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
Maildirdirectory to the internal
set record=+sentcreates a
sentmbox file for storing sent mail within whichever directory is set as the
foldervariable, in this case
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
Send the email by piping a string to the
s-nail command. Adjust the command to mark your Linux user as the recipient:
echo 'init' | s-nail -s 'init' -Snorecord admin
You can can check to make sure the directory was created by looking for your
ls -R ~/Maildir
You will see the directory structure has been created and that a new message file is in the
$ ls -R ~/Maildir /home/ubuntu/Maildir: cur new tmp /home/ubuntu/Maildir/cur: /home/ubuntu/Maildir/new: 1643972360.V10301Ifbca3M171366.dev-ubuntusrv.inv.re 1643972375.V10301Ifbca5M242683.dev-ubuntusrv.inv.re /home/ubuntu/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
In your console, you’ll see a rudimentary inbox with the
init message waiting:
$ s-nail s-nail version v14.9.15. Type `?' for help /home/admin/Maildir: 1 messages 1 new ▸N 1 Ubuntu 2022-02-04 10:59 14/432 init ?
ENTER to display the message:
[-- Message 1 -- 14 lines, 432 bytes --]: From [email protected] Fri Feb 4 10:59:35 2022 Date: Fri, 04 Feb 2022 10:59:35 +0000 To: [email protected] Subject: init Message-Id: <[email protected]> From: Ubuntu <[email protected]> init
You can get back to the message list by typing
h, and then
▸R 1 Ubuntu 2022-02-04 10:59 14/432 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
To get back to the terminal, type
q and then
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:
Inside, enter some text you’d like to send:
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
-roption allows you to override this with a valid address, such as one of those you defined in the
/etc/postfix/virtualfile. To illustrate, the following command uses
Also, be sure to change
[email protected] to a valid email address which you have access to:
cat ~/test_message | s-nail -s 'Test email subject line' -r [email protected] [email protected]
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:
From the email client, view your sent messages by typing:
You’ll see output like this:
$ s-nail s-nail version v14.9.15. Type `?' for help /home/admin/Maildir: 0 messages No more mail. ? file +sent +[/home/admin/Maildir/]sent: 1 message 1 new ▸N 1 [email protected] 2022-02-04 11:07 12/291 Test email subject line ?
You can manage sent mail using the same commands you use for incoming mail.
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.
Pingback: How to Install mysqmail-postfix-logger in Ubuntu 20.04