Set up your own mail server on Ubuntu with Postfix

10.03.2021 859 0

If you want to have your own mail server on your Ubuntu Cloud or Dedicated server follow this in-depth step-by-step guide. 

Why should you have your own email server? 

Having your own mail server provides you with freedom on the number of users, sent emails, inbox and attachment sizes, control, privacy, and customization. 

Depending on how you organize, it could be a lot cheaper and you can be far less dependent on external companies. 

What is Postfix?

Postfix is an open-source software (IBM Public License or Eclipse Public License), mail transfer agent (MTA) that you can download and use on different OSes like Linux (Ubuntu, Debian, CentOS, etc.), BSD (including FreeBSD), macOS, Solaris, HP-UX, AIX. 

It offers an SMTP server and SMTP client, so it could directly receive and send emails. Having some spam protection, it could work together with other software products like Amavisd-new, Dovecot, Mailman, and more. 

The server runs in the background and contains different components like scheduler, local delivery server, address re-writer, and more. The client-side is used by the users to send emails. 

You can find Postfix as the default MTA for Ubuntu, CentOS, RedHat, Fedora, NetBSD, macOS, and more. 

According to E-Soft Inc, in August 2019 it had a 34% market share (publicly reachable mail-servers on the Internet), just a bit behind in popularity in comparison with Exim. 

Setting up a basic Postfix SMTP on Ubuntu

This article will clarify how to set up an SMTP (MTA) and once you’re done with the setup, you will be able to send and receive emails using your Ubuntu server with your own domain. 

Before we begin, we strongly recommend that you use a Long Term Support (LTS) version of Ubuntu like the latest Ubuntu 20.04.2 LTS (end of standard support April 2025) or Ubuntu 18.04.5 LTS (end of standard support April 2023 and end of life April 2028). 

Using another version of Ubuntu will require you to update often and that process might damage the functionality of your email server.

You will also need to purchase a domain name from an online domain name registrar. 

Hosting provider considerations

When you are choosing a hosting provider, you will need to pay attention to the following:

  • Choose a provider that does not block port 25. Or, in case it is blocked by default, you can ask the provider to unblock it. 
  • The IP address that the provider offers with the server is not in email blacklists. If it is, your emails might go directly to the spam folder and the whole idea of owning a mail server doesn’t work.
  • Edit DNS records like PTR records for improved delivery.
  • Check that there are no hourly, daily, or weekly limits of emails that you send. Or that at least the limit is in line with your goals. 

Before installing Postfix

Define the hostname for the server

The hostname can be a single word like Computer1234 or FQDN like mail.yourdomain.com. 

For this example, we will use the FQDN with the assumption that you have already purchased a domain name from a domain registrar. 

mail.yourdomain.com 

Mail is the node name. 

yourdomain.com is the domain name that you’ve bought. 

The FQDN will show in the smtpd banner. You will need the banner so you don’t get any messages rejected from MTAs for missing banner reasons. 

In the Terminal:

hostname -f

If there is no FQDN yet, use: 

sudo hostnamectl set-hostname mail.yourdomain.com

Replace the “mail.yourdomain.com” with your own FQDN in the command. 

Managing your DNS records

If you’re using a DNS server and managing the master zone for your domain name, do it from there. If you are using a Managed DNS provider, resort to their control panel or if you rely on your domain registrar, go there. 

Add an “A record” or an “AAAA record” or both

You have to add an A record that will serve as the link between your FQDN and the IP address of the server. 

Use the following parameters:

hostname: mail.yourdomain.com

Points to: IPv4 address of the server 

If your server uses IPv6, you should add the AAAA record. 

hostname: mail.yourdomain.com

Points to: IPv6 address of the server 

Add an MX record 

The MX records show the responsible servers for the incoming emails. 

You need to direct them to the mail.yourdomain.com. Note, the MX record should point to a name, not an IP address. 

Add a PTR record

PTR records serve for reverse DNS lookups. The other servers will try to check the IP address of your outgoing mail server and see if it belongs to the domain name. 

It looks like a reverse A record, where you first put an IP address (you can put one for IPv4 and another one for IPv6), and then direct it to the domain name (mail.yourdomain.com). 

Postfix, Installing process

On your Ubuntu server, we will need to run two commands: 

sudo apt-get update

This command asks for your sudo password and will show you the available updates, latest versions. 

The second command:

sudo apt-get install postfix -y

This will install the package postfix, confirming with -y. 

Wait a bit and you will get a question box that will start your Postfix configuration. 

No configuration – installation without any configuration.

Internet Site – Set Postfix to send and receive emails. 

Internet with smarthost – Set Postfix to receive emails, but use another software to send emails. 

Satellite system – All the emails to another host – smarthost, for delivery

Local only – For no network connectivity scenario, where the only delivered emails are those for local users. 

Select the “Internet Site” option, from the list. 

The next step is to add the FQDN. Here you will need to add it as yourdomain.com, not as mail.yourdomain.com. 

Wait for the installation to finish. Later on you can edit the configuration from /etc/postfix/main.cf. 

Check whether your installation was successful with this command: 

postconf mail_version

The output will be 3.3.0 if you are running Ubuntu 18.04 or it will be 3.4.10 or newer for Ubuntu 20.04.

Let’s use the SS utility and see that Postfix uses port 25 (TCP). 

sudo ss -lnpt | grep master

Ubuntu and firewall 

On Ubuntu, you don’t have a firewall by default, but it is recommended to use one. If you have already enabled a UFW firewall, you will need to open the port 25. 

sudo ufw allow 25/tcp

Now, we will use nmap to scan for the open ports on the mail server, from another computer.

For Ubuntu-based Linux distros, install it with: 

sudo apt install nmap

Then use the command:

sudo nmap youripaddress

Replace “youripaddress” with a real IP address. 

The output will show you that the 25/tcp is open (smtp service). 

You can also check if the port is blocked from the mail server by using the following command: 

telnet gmail-smtp-in.l.google.com 25

The command will connect the server to a Gmail SMTP, using port 25. If you get “Connected” in the output, it is open. If it shows “Trying…” the port is blocked. If it is not open, talk to your hosting provider and ask them to unblock it. 

You can change the port, but it might not work, because on the other end, some other SMTP will be expecting only port 25 for the purpose of mail exchange. 

Testing if your email server works

By now, you will be able to use your mail server for sending and receiving emails. 

We can create a test account called sender1, and the email address for it will be sender1@yourdomain.com

We can do that by adding a user on Ubuntu:

adduser sender1

For the purpose of the example the username is sender1. 

Ok, now let’s use the sendmail binary (/usr/sbin/sendmail) to send an email to your gmail address: 

echo “test of email functionality” | sendmail yourgmailaccount@gmail.com

“Test of email functionality” is the text of the message. 

Try to respond to the email from your Gmail account. 

Search for the answer in /var/spool/mail/sender1 or /var/mail/sender1, or use the command: 

postconf mail_spool_directory

If you have a problem

If you’ve already enabled port 25, and tried to send the email to your other account and it is not working, take a look at the log. 

Use the command:

sudo nano /var/log/mail.log

Check what the log error shows. A common problem is that “this message does not meet IPv6…”, which means that either your AAAA and PTR record for it are not configured properly or they are missing. Direct them properly. 

Send emails with another software

You can install another software for sending and receiving emails. 

In this case, we will try Mailutils. To install it: 

sudo apt-get install mailutils

After that, we can use it to send emails. Try the following command:

mail yourgmailaccount@gmail.com

You can send an email to another account, just change yourgmailaccount@gmail.com

user@mail:~$ mail yourgmailaccount@gmail.com

Cc: 

Subject: Test of email functionality

Here is the text of the second test that we are performing. 

Once you’ve written the email, pres Ctrl+D, and the mail will be sent. 

If you want to read the incoming emails just use the command: 

mail

It is a bit less comfortable to use, but for these tests it is ok. 

Increase the attachment size limit

The default size of the attachment is set to 10MB. You can see it with: 

postconf | grep message_size_limit

and you will get the output: message_size_limit = 10240000

You can change the size with a simple command: 

sudo postconf -e message_size_limit=30720000

This will increase it 3 times. You can change the limit with a bigger or smaller number. 

When increasing this limit, make sure that it is not a number larger than the mailbox size limit (mailbox_size_limit). Its default value is 51200000 bytes (Postfix). 

Setting the Postfix hostname

If you don’t change anything, Postfix will use Ubuntu’s default hostname. Let’s use the Nano text editor and edit it with this command: 

sudo nano /etc/postfix/main.cf

Inside the Postfix configuration file, find the myhostname and put the mail.yourdomain.com 

Use your domain in place of mail.yourdomain.com.

myhostname = mail.yourdomain.com

Save the file with Ctrl+0, and press Enter for confirmation. In the end, restart Postfix with: 

sudo systemctl restart postfix

Use IPv4 only

Not all hosting providers will offer both IPv4 and IPv6, but by default, the Postfix is set to use them both. Check the configuration of the internet protocols: 

postconf inet_protocols

You will see:

inet_protocols = all

To limit the use to IPv4 only, and prevent problems with IPv6, use the command: 

sudo postconf -e "inet_protocols = ipv4"

After this action, restart the Postfix: 

sudo systemctl restart postfix

Conclusion

Now you know how to easily install your own mail server on your Ubuntu server. You can use it with our dedicated or cloud servers. Set up your mail server on a server, that you control, with the software that will not let you down. Save money and improve the performance now! 

In case you have missed some important bits of information about Ubuntu, here you can read more:

Leave a Reply

Your email address will not be published.