Setting Up An SMTP Server On Linux
Setting Up An SMTP Server On Linux
Given the long history of email (dating back to 1971), and the massive number of email
servers online, you’d be right to assume that there are many different email server platforms
available. However, according to the securityspace.com survey, three types of email servers
account for more than 95% of the global market share:
1.
1. Exim
2. Postfix
3. Sendmail
The top three email servers on the internet are Exim (with over 55%), Postfix (over 30%), and
Sendmail (about 4%). All three of these email platforms, and Qmail, were originally
developed for Unix-Like or Linux based Operating Systems.
Today, with the widespread availability of easy and low-cost cloud-hosted server providers,
anyone can setup their own email server. This guide focuses on deploying, configuring, and
discussing best practices and security concerns for Exim, Postfix, Sendmail, and Qmail.
Exim – Pros
• Default MTA on Debian-based Linux distributions
• Secure
Exim – Cons
• Arguably more difficult to maintain/administer post-deployment than Postfix
Postfix – Pros
• Easy to configure and use
• Secure
Postfix – Cons
• No Windows port
Sendmail – Pros
• Native Windows port available
Sendmail – Cons
• Not as easy to configure as Exim or Postfix
Qmail – Pros
• Secure
• Config file structure and general operation is less elegant than Postfix or Exim
• No Windows port
o 1 vCPU
o 20GB SSD
o 1 TB Bandwidth
These procedures assume you are starting with a fresh installation of Ubuntu.
Also, we will need to allow TCP port 25 (the default SMTP port) through the firewall. In our
Amazon Lightsail environment, that looks like this:
To install Mutt:
mutt -f ~/Maildir
Mutt will ask you if you want to create /home/ubuntu/Mail (we don’t) so just press n
2. Enter the email address to which you want to send a test email
4. You will then enter Nano text editor to write the body of your message. Write
whatever you want.
Finally, press y to send the email, and you should see “mail sent.”
Now, let’s go check our email to see if the message was delivered. Since this is a brand new
email server with no DNS configuration or SPF, DKIM or DMARC, our test message will
most likely be flagged as spam (if it gets delivered at all). For our tests, we used a Gmail
account, and we were able to find our message in the spam folder:
Our email was delivered! This means our email server is correctly sending outbound mail.
Basic Install & Config Procedure
for Exim
Exim – Intro & Unique Considerations
Exim version 4 is the default MTA for Debian-based Linux distributions, including Ubuntu
18. Therefore, installing Exim4 on Ubuntu is very straightforward.
sudo update-exim4.conf
(At the time of this writing, Exim version 4.90-1 is available in the default repositories.)
To configure Exim4, run the following, which will display a user interface for configuring
many Exim parameters:
You should see the following screen, allowing you to choose the type of mail server
configuration you want. For this example, we will be configuring Exim as an “internet site” or
an email server that sends and receives email directly over the internet via SMTP.
The next screen will prompt you to enter the “mail name” of the server. This should be the
domain name where you want to receive email. We will use the name “sjno.net.”
The next screen asks us to define on which IP addresses we want to listen for incoming SMTP
connections. Since we want this server to be publicly available to the internet, we will set this
to blank.
The next screen asks us to define our “local domains.” Local domains are domains for which
this email server should be considered the final destination. We will use the domain
“sjno.net.”
The next screen asks us to define our “relay domains.” Relay domains are non-local domains.
In our case, this is everything other than “sjno.net.”For now, we will leave this as the default
value (blank).
The next screen asks us to define IP addresses for which this system will unconditionally
relay email messages. This is useful if we are setting up this email server to be a dedicated
“Mail Relay” or “Smarthost.” For now, we will leave this as the default value (blank).
The next screen configures DNS lookup settings. For now, we will leave this as the default
value (No).
The next screen asks us our preference between two common local mail formats. These two
formats are called “mbox” and “maildir.” This choice is mostly user preference. We prefer
Maildir, so that is what we will select here.
The next screen asks us whether or not we want to split Exim4’s configuration into smaller
files, as opposed to the default “monolithic” configuration. There are pros and cons to each.
For this example, we are going to choose not to split the config files (No).
The next screen asks us where mail addressed to postmaster@ and root@ should be delivered.
If we leave this blank, mail for those recipients will be saved in /var/mail/mail (not
recommended). We will enter the username of our default user, which is “ubuntu.”
Exim – Testing Initial Installation
We now have a basic installation of Exim! Let’s make sure it started and is running OK.
Run this:
If Exim is running, the above command should output something similar to this (we can see
that there is a running process /usr/sbin/exim4):
If Exim was started successfully, the command above should output something similar to this
(we can see that the active status shows “active (running)” ):
If we point the “Test Email Server” test to the public IP address of our Amazon Lightsail
instance, we should see something like this:
To send a test email using the Mutt email client, refer to the section: How to Use Mutt Email
Client to Send a Test Email
These tests prove that our basic Exim email server is up, listening, and can send mail. You
may be thinking “but that’s just an IP Address! What about DNS?” Continue on to the DNS
Setup for Mail Server section for steps on configuring the basic DNS-related settings.
Exim – Troubleshooting
Server doesn’t appear to be listening or responding from the outside?
Don’t forget to open port 25 on your firewall. (See instructions for AWS in the environment
notes section.)
/etc/postfix/main.cf
/etc/postfix/master.cf
To stop Postfix:
To restart Postfix:
sudo postconf
man postconf
Postfix – Procedure
First, let’s see what Postfix package is available in the default repositories:
(At the time of this writing, Postfix version 3.3.0 is available in the default repositories.)
You should see the following screen, allowing you to choose the type of mail server
configuration you want. For this example, we will be configuring Postfix as an “internet site”
or an email server that sends and receives email directly over the internet via SMTP.
The next screen will prompt you to enter the “mail name” of the server. This should be the
domain name where you want to receive email. We will use the name “sjno.net.”
Immediately after hitting enter on this screen, the Postfix configuration user interface will
close and Postfix will start scrolling through some automated configuration and startup
output. Be sure to read this output for useful tips on performing additional Postfix
configuration.
By default, Postfix uses Mbox mail format. We prefer Maildir. First, let’s manually create the
Maildir directory structure for our Ubuntu user:
mkdir -p /home/ubuntu/Maildir/{cur,new,tmp}
Then configure Postfix to use Maildir:
If Postfix is running, the above command should output something similar to this (we can see
that there are several Postfix processes running):
If Postfix started successfully, the command above should output something similar to this
(we can see that the active status shows “active (exited)”):
If we point the “Test Email Server” tool to the public IP address of our Amazon Lightsail
instance, we should see something like this:
To send a test email using the Mutt email client, refer to the section How to Use Mutt Email
Client to Send a Test Email
These tests prove that our basic Postfix email server is up, listening, and can send mail! You
may be thinking “but that’s just an IP Address. What about DNS?” Continue on to the DNS
Setup for Mail Server section for steps on configuring the basic DNS-related settings.
Postfix – Troubleshooting
Server doesn’t appear to be listening or responding from the outside?
Don’t forget to open port 25 on your firewall. (See instructions for AWS in the environment
notes section.)
/etc/hosts
/etc/mail/sendmail.mc
To stop Sendmail:
sudo sendmailconfig
sudo newaliases
Sendmail – Procedure
First, let’s see what Sendmail package is available in the default repositories:
(At the time of this writing, Sendmail version 8.15.2 is available in the default repositories.)
We need to modify the first line that starts with “127.0.0.1 localhost” to include the FQDN
(fully qualified domain name) or our mail server. For our example, we will use
“mail.sjno.net”:
Save the /etc/hosts file, then run the following command and answer “Y” to every prompt:
sudo sendmailconfig
When it’s finished, you should see something like this (note: the first part of the output is
omitted):
By default, Sendmail only listens on the local loopback interface (127.0.0.1). To tell Sendmail
to listen on all interfaces, we need to modify etc/mail/sendmail.mc:
If Sendmail is running, the command above should output something similar to this (we can
see that there is a running process “sendmail: MTA: accepting connections”):
If Sendmail was started successfully, the command above should output something similar to
this (we can see that the active status shows “active (running)”):
If we point the “Test Email Server” tool to the public IP address of our Amazon Lightsail
instance, we should see something like this:
To send a test email using the Mutt email client, refer to the section How to Use Mutt Email
Client to Send a Test Email
These tests prove that our basic Exim email server is up, listening, and can send mail! You
may be thinking “but that’s just an IP Address. What about DNS?” Continue on to the DNS
Setup for Mail Server section for steps on configuring the basic DNS-related settings.
Sendmail – Troubleshooting
Server doesn’t appear to be listening or responding from the outside?
Don’t forget to open port 25 on your firewall. (See instructions for AWS in the environment
notes section.)
To stop Qmail
To restart Qmail:
Qmail – Procedure
First, let’s see what Qmail package is available in the default repositories:
(At the time of this writing, Qmail version 1.06 is available in the default repositories.)
At the time of this writing, installing the default Qmail package on Ubuntu 18.04 does not
automatically install at least one necessary dependency to make Qmail work. Let’s manually
install that package:
Then, copy and paste the following to do some basic Qmail setup. Be sure to replace all
instances of “sjno.net” below with your domain.
Also, we should now be able to use a free web-based tool to verify that our server is up and
listening to the world. We like to use the “Test Email Server” tool from mxtoolbox.com’s
SuperTool (https://mxtoolbox.com/SuperTool.aspx#).
If we point the “Test Email Server” tool to the public IP address of our Amazon Lightsail
instance, we should see something like this:
To send a test email using the Mutt email client, refer to the section How to Use Mutt Email
Client to Send a Test Email
These tests prove that our basic Qmail email server is up, listening, and can send mail! You
may be thinking “but that’s just an IP Address. What about DNS?” Continue on to the DNS
Setup for Mail Server section for steps on configuring the basic DNS-related settings.
Qmail – Troubleshooting
If you see errors like this, make sure you have the “daemontools-run” package installed:
In order to tell the world how to get to your email server, we need to do some DNS setup. We
will also setup SPF at the same time.
1. You must own a domain name. In this example, we will use the domain sjno.net.
2. You must have the domain name configured with DNS servers. This is configured at
your registrar (for example, godaddy.com).
3. You must have access to make changes / add records on your DNS servers for your
domain.
Assuming the three requirements above are met, we can create the necessary DNS records.
There are many DNS providers available and all of their user interfaces will look different.
Or, you could even be running your own CLI-only Linux DNS server with Bind9. Ignoring
the differences between user interfaces, the concepts are the same. This example will show
the DNS configuration with free DNS provider freedns.afraid.org.
First, let’s create an A Record for our new mail server. An A Record is the most basic type of
DNS record. We are just creating a name for our mail server, and pointing it to the public IP
address of our server. We’re going to call our server “mail.sjno.net”:
After saving our A Record and waiting a few minutes, we should be able to ping our mail
server by name and see that the DNS is automatically resolving the IP address for us:
Next, we’re going to create an MX Record. An MX Record tells the world where to send
email destined for our domain. We’re going to leave the “subdomain” blank (or @), and point
it to the name of the A Record we created in the previous step:
After saving our MX Record and waiting a few minutes, we should be able to use our trusty
online tool (https://mxtoolbox.com/SuperTool.aspx) to do an “MX Lookup” for our domain:
Next, we’re going to configure an SPF Record to tell the world that our email server is an
allowed and trusted sender for our domain. Even though this is an SPF Record, we’re not
going to use the SPF Record type, we’re actually going to use a TXT Record to record our
SPF info. There are a few different ways to configure SPF, but we’re going to do it like this:
This is a shorthand way to say “trust the email server we have defined in the MX Record for
this domain.” The destination or “content” of your SPF TXT record must be wrapped in
quotes. Some user interfaces will automatically add the quotes for you, and others won’t, so
watch out for that.
Once again, using our trusty tool (https://mxtoolbox.com/SuperTool.aspx) and this time
performing an “SPF Record Lookup,” we should see that our SPF Record has been published
successfully for our domain:
What is SMTP?
SMTP or Simple Mail Transfer Protocol is a communication protocol first defined in 1982.
SMTP is the main protocol behind what we know as email. SMTP defines how email should
be submitted to an email server from an email client, and also how email is sent and received
between email servers. SMTP is an application layer protocol which requires that a TCP
connection first be established to the email server before SMTP data can be sent across the
connection.
Why is SMTP important? How is it used?
SMTP is important because it is the main internet standard behind email. The first email
message was sent in 1971. Nearly 50 years later, email has become the single most important
communication tool driving the modern world. Global communications today rely on email
more than any other method of communication. The impact that email has had on global
business is immeasurable.
DKIM (DomainKeys Identified Mail) is an authentication method used to deter spam and
phishing emails. DKIM utilizes cryptographic signatures to prove that an email was sent from
a trusted email server and has not been tampered with. DKIM relies on DNS to publish DKIM
information for a given email domain, usually via DNS TXT Record.
DMARC
DNS
DNS (Domain Name System) is arguably the most important protocol on the internet. To use
a simple analogy, DNS is like the “phone book” of the internet. The DNS system allows
computers to determine the IP Address for a given domain name.
IMAP
IMAP (Internet Message Access Protocol) is a protocol used by email clients to retrieve or
download email messages from an email server to an email client. In most cases, IMAP is
recommended as a better and more full-featured alternative to POP3.
Mail Relay
Mail Relay is both the service of routing email to another mail server for delivery, and a term
for a mail server that provides that service.
MTA
An MTA (message transfer agent) is one component of an overall MHS (message handling
system) or email server, that is responsible for transferring and routing email messages to the
recipient’s email server.
Mutt
Mutt is a CLI (command line interface) email client that is useful on CLI-only Linux email
servers for sending and receiving emails and also for troubleshooting email functionality.
MX Record
An MX record is a specific type of DNS record that defines the email server where emails
should be sent for a given domain. For example, if you want to send an email
to [email protected], your email client must first determine the email server used by the
gmass.co domain. To make that determination, a DNS query will be made for the MX record
for the gmass.co domain. Using a web-based MX lookup tool, we can see this information for
ourselves. Refer to the screenshot (mx_lookup_1.png or mx_lookup_2.png). (I used
MXToolbox.com and dnschecker.org to generate these screenshots.)
POP3
POP3 (Post Office Protocol) is a protocol used by email clients to retrieve or download email
messages from an email server to an email client. POP3 is a very basic protocol. In most
cases, IMAP is recommended as a better and more full-featured alternative to POP3.
SASL
SASL (Simple Authentication and Security Layer) is a standardized method for adding
authentication support to connection-based protocols. SASL decouples authentication from
the application, allowing secure authentication to numerous compatible application layer
protocols (including SMTP).
SPF
SPF (Sender Policy Framework) is a basic email authentication protocol that is used to deter
spam and spoofed email on the internet. SPF utilizes DNS (typically via a DNS TXT Record)
to publish trusted email servers for a given domain.
SPF Record
An SPF (Sender Policy Framework) record is a specific type of DNS record that is used to
publish trusted email sending servers for a given email domain. SPF records have mostly been
deprecated in favor of DNS TXT Records, which can contain the same information and serve
the same purpose.
STARTTLS
STARTTLS is a protocol command that is issued by an email client to tell an email server that
the client wants to upgrade the current connection to a secure connection utilizing SSL/TLS
certificates.
TXT Record
A TXT (or text) Record is a specific type of DNS record used to publish arbitrary text related
to a given domain. TXT Records are often used to prove domain ownership. TXT Records are
also used to publish SPF, DKIM, and DMARC information related to email service for a
domain.