How To Install and Configure Postfix On Ubuntu 22.04 - DigitalOcean
How To Install and Configure Postfix On Ubuntu 22.04 - DigitalOcean
Prerequisites
Step 1 — Installing Postfix
Step 2 — Changing the Postfix Configuration
Step 3 — Installing the Mail Client and Initializing the Maildir Structure
Step 4 — Testing the Client
This site uses cookies and related technologies, as described in MANAGE CHOICES
Conclusion
our privacy policy, for purposes that may include site operation, analytics,
enhanced user experience, or advertising. You may choose to consent to
our use of these technologies, or manage your own preferences. AGREE & PROCEED
// TUTORIAL //
Introduction
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 22.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 22.04 initial server setup guide to set this up.
A Fully Qualified Domain Name pointed at your Ubuntu 22.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.
Postfix is included in Ubuntu’s default repositories, so you can install it with APT.
To begin, update your local apt package cache:
This site uses cookies and related technologies, as described in
our privacy policy, for purposes that may include site operation, analytics, Copy
enhanced user experience, or advertising. You may choose to consent to
our use of these technologies, or manage your own preferences.
$ 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:
Copy
$ 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.
This site uses cookies and related technologies, as described in
To be explicit, these are the settings used in this guide:
our privacy policy, for purposes that may include site operation, analytics,
enhanced user experience, or advertising. You may choose to consent to
General type of mail configuration?: Internet Site
our use of these technologies, or manage your own preferences.
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:
Copy
When you are prompted to restart services, accept the defaults and choose OK .
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
This site uses cookies and related technologies, as described in
under that name within your user’s home directory. Configure home_mailbox by typing:
our privacy policy, for purposes that may include site operation, analytics,
enhanced user experience, or advertising. You may choose to consent to
our use of these technologies, or manage your own preferences.
Copy
$ 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 :
Copied!
$ 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 :
Copy
$ 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 likeProducts
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 Solutions
file like this:
Developers
/etc/postfix/virtual
Partners
Pricing
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 thisLogbyinpressing CTRL + X , Y , then
SignENTER
up .
This site uses cookies and related technologies, as described in
Apply
our thepolicy,
privacy mapping by typing:
for purposes that may include site operation, analytics,
enhanced user experience, or advertising. You may choose to consent to
our use of these technologies, or manage your own preferences. Blog
Copy
$ sudo postmap /etc/postfix/virtual
Docs
Get Support
Restart the Postfix process to be sure that all of your changes have been applied:
Contact Sales
Copy
Tutorials Questions
$ sudo systemctl Learning Paths
restart postfix For Businesses Product Docs Soc
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:
Copy
$ 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.
This site uses cookies and related technologies, as described in
our privacy policy, for purposes that may include site operation, analytics,
To ensure that the MAIL variable is set regardless of how you access your account —
enhanced user experience, or advertising. You may choose to consent to
whether through ssh , su , su - , or sudo , for example — you’ll need to set the variable in
our use of these technologies, or manage your own preferences.
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:
Copy
$ echo 'export MAIL=~/Maildir' | sudo tee -a /etc/bash.bashrc | sudo tee -a /et
To read the variable into your current session, source the /etc/profile.d/mail.sh file:
Copy
$ source /etc/profile.d/mail.sh
With that complete, install the s-nail email client with APT:
Copy
$ 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:
Copy
$ sudo nano /etc/s-nail.rc
Send the email by piping a string to the s-nail command. Adjust the command to mark
your Linux user as the recipient:
Copy
$ echo 'init' | s-nail -s 'init' -Snorecord sammy
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:
This site uses cookies and related technologies, as described in
our privacy policy, for purposes that may include site operation, analytics, Copy
enhanced user experience, or advertising. You may choose to consent to
our use of these technologies, or manage your own preferences.
$ 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
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
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 [email protected] 2022-04-18 15:09 14/452 init
init
You can get back to the message list by typing h , and then ENTER :
Copy
? h
Output
>R 1 [email protected] 2022-04-18 15:09 14/452 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 :
Copy
? d
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.
This site uses cookies and related technologies, as described in
Begin byuser
writing a test message
experience, in a You
or advertising. textmay
editor:
our privacy policy, for purposes that may include site operation, analytics,
enhanced choose to consent to
our use of these technologies, or manage your own preferences.
Copy
$ nano ~/test_message
Also, be sure to change user @ email.com to a valid email address which you have access
to:
Copy
$ cat ~/ test_message | s-nail -s ' Test email subject line ' -r contact@exampl
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.
This site uses cookies and related technologies, as described in
our privacy policy, for purposes that may include site operation, analytics,
You can view your sent messages within your
enhanced user experience, or advertising. You may choose
client. Start the interactive client
to consent to
s-nail
again:
our use of these technologies, or manage your own preferences.
Copy
$ s-nail
You can manage sent mail using the same commands you use for incoming mail.
Conclusion
You now have Postfix configured on your Ubuntu 22.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 us ->
Comments
4 Comments
Leave a comment...
This site uses cookies and related technologies, as described in
our privacy policy, for purposes that may include site operation, analytics,
Click below to sign up and get $200 of credit to try our products over 60 days!
Sign up
Popular Topics
Ubuntu
Linux Basics
JavaScript
Python
This site uses cookies and related technologies, as described in
MySQL
our privacy policy, for purposes that may include site operation, analytics,
Docker
enhanced user experience, or advertising. You may choose to consent to
our use of these technologies, or manage your own preferences.
Kubernetes
All tutorials ->
Congratulations on unlocking the whale ambience easter egg! Click the whale button in
the bottom left of your screen to toggle some ambient whale noises while you read.
Thank you to the Glacier Bay National Park & Preserve and Merrick079 for the sounds
behind this easter egg.
Interested in whales, protecting them, and their connection to helping prevent climate
change? We recommend checking out the Whale and Dolphin Conservation.
Reset easter egg to be discovered again / Permanently dismiss and hide easter egg
Get paid to write technical tutorials and select a tech-focused charity to receive a
matching donation.
Learn more
Featured Tutorials
DigitalOcean Products
Sign up and get $200 in credit for your first 60 days with DigitalOcean.
Get started
This promotional offer applies to new accounts only.
Company
Products
Community
This site uses cookies and related technologies, as described in
our privacy policy, for purposes that may include site operation, analytics,
Solutions
enhanced user experience, or advertising. You may choose to consent to
our use of these technologies, or manage your own preferences.
Contact