Don’t miss the New Year’s Sale deals!
search

How to install and set up an Ubuntu mail server

How to install and set up an Ubuntu mail server

Having your own email server is a great idea for any medium-sized company. With this, all the traffic is controlled, and we can define the rules ourselves. This way, we can manage the service clearly and cleanly.

In this tutorial, we will show you how to use Postfix to install and set up a mail server on a virtual private server running Ubuntu.

Configuring the DNS server for an Ubuntu mail server

Just follow this step by step guide, and you shouldn’t have any problems setting up the configuration.

1. Log in and update your server

Log into your server using SSH. If you’re having trouble, check out our PuTTY tutorial. After logging in, you should update your machine using the following command:

apt-get update

2. Install Bind

To configure a DNS server that will use Postfix we’ll need an additional tool – Bind. Let’s install it first:

sudo apt install bind9

3. Configure /var/cache/db.example

At this point, we must take into account that the example IP address in this tutorial is 123.123.123.123 – you’ll need to replace it with the IP address where we will perform the installation. For this example we’ll use mail.example.com as a FQDNS.

So, now it is necessary to create a new zone for our example. To do this, create a new file with the zone information.

sudo nano /var/cache/bind/db.example

Then, add the following:

$ORIGIN example.com.
$TTL 1D
@       IN SOA     ns1 root(
1 ;serial
1D ;refresh
2H ;retry
2W ;expire
5H ;minimum
);
@       IN        NS ns1
ns1     IN        A 123.123.123.123
mail    IN        A 123.123.123.123
@       IN        MX 5 mail

Remember, we must replace the IP address with that of your server, and change example.com to the domain you wish to use. Press CTRL+O to save the changes and CTRL+X to close the nano editor.

4. Add new zone to bind configuration

Before enabling the newly created zone it is necessary to check the configuration of the file.

sudo named-checkzone example.com. /var/cache/bind/db.example

Now we can add our new zone to the Bind zone configuration file. To do this, run the following command:

sudo nano /etc/bind/named.conf.default-zones

And add the new zone:

zone "example.com." {
       type master;
       file "db.example";
};

Again, CTRL+O to save the changes and CTRL+X to close it.

5. Configure /etc/bind/named.conf.options

Now, in the file /etc/bind/named.conf.options it is necessary to uncomment the forwarders line and include the Google DNS – 8.8.8.8. For that simply remove the // symbols as shown in the screenshot below.

sudo nano /etc/bind/named.conf.optionsEditing DNS zone to set up an Ubuntu Mail Server

6. Restart Bind

Now, we have to restart the bind9 service. You can do it with one of two commands:

sudo systemctl reload bind9

or

sudo systemctl restart bind9

How to install and setup the mail server on Ubuntu

We’re almost there, your Ubuntu email server is ready to come online. Here’s what you should do:

1. Install Postfix email server

Now it is time to install Postfix. Postfix is an email server written in C. Its main feature is the speed of execution and open source nature. Install it with the following command:

sudo apt install postfix

During installation, we will be asked to configure the package. On the first screen, choose the option Internet Site.

Then, we have to enter the name of the server. In this case example.com.

Postfix is very flexible and allows extensive configuration, but for this tutorial we’ll fix with the default configuration.

2. Add user

Then, we have to add our user to the group mail:

sudo usermod -aG mail $(whoami)

After that, we have to create the users and add them to the mail group so they can send and receive mail. I’ll add Gabriel:

sudo useradd -m -G mail -s /bin/bash/ gabriel

Then, we need to set a password to the newly created user:

sudo passwd gabriel

Test the Ubuntu mail server

Now to prove what we just did, we will send and receive an email from the terminal. To do this, we will install the mailutils package:

sudo apt install mailutils

Next, we send an email to the other email account user named gabriel. Type in the subject and the message. After that, press CTRL+D to finish. To start writing an email enter the following command:

mail gabriel@example.com

Now we can log into another user and check the mail utility.

There, after running the mail command, we will see the email we just sent to the other test user. To access the email just write the number of the mail, in this case, 1.

To test outbound emails from this user, just try another email address:

mail angelo@example.com

That’s it! You’re sending emails from your very own email server on Ubuntu.

Conclusion

An email server is easy to set up but might be a bit complex to manage. In Linux, it is recommended to do it for its security and resource management.

On the other hand, in a bigger company, it can be very useful to have a fully configured and functional email service out of the box, like the one offered by Hostinger. Alternatively, host your own email server to get complete control over the service.

There are many ways to improve and maintain an email server. It is a process that takes time and is constantly evolving, so we recommend that you keep going deeper into the subject. Good luck and happy mailing!

Author
The author

Edward S.

Edward is a content editor with years of experience in IT writing, marketing, and Linux system administration. His goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.

What our customers say

Comments

Author
vikas

August 08 2019

i want create mail saver

Author
Jeremy Carter

October 20 2020

Okay now I have it set up, how do I retrieve the mail I sent as a test? Is there a GUI for this to use on other computers that I can have my users set up?

Author
Daniel Alberto Guglielmi

December 01 2020

Hello Edward, I was following the instructions you wrote, but when I tried to run 'sudo nmcli connection show' there is no connection listed, and I don't see for anywhere you take this into account. Do you have any suggestion about it? Regards,

Author
Patrick D

June 30 2021

Hello, I was trying to set up the e-mail server to send automatic emails to different users. When I'm trying to send a mail via the "mail" command, the mail.log file shows the following: Client host rejected: cannot find your reverse hostname I'm not receiving the mail either. I followed all of your steps, but somehow it doesn't work. Do you know what I'm doing wrong? Best regards.

Author
nandhan

September 08 2021

do we have any dashboard to view our inbox and compose mail after creating mail server

Author
Arham Shahyd

December 16 2021

Hi, I setup everything works fine. Email are sending to my personal gmail from [email redacted for privacy] but i want to read emails from my email accounts and also i want to add new email accounts using vps hosting. Could you please suggest me the simplest method. Thanks and regards!

Author
Aslan

February 01 2022

Can I host my domain with another hosting company and still send email through the hostinger VPS server? If yes how?

Author
Luis G

May 28 2022

What would be the difference if I want to do the same process but I have a debian as my operative system in my VPS from Hostinger? where I can find an article to make this procedure for debian?

Author
Roy

February 19 2024

Hi, new to LINUX and don't catch typos in command lines yet. I attempted to do the adduser noted in this article (sudo useradd -m -G mail -s /bin/bash/ gabriel) and got an error message. Seems in my version of UBUNTU (latest) you can't have the "slash" after bash. ie it should be (sudo useradd -m -G mail -s /bin/bash gabriel). Everything went well, I sent and review "sent" emails on my server to several users(on my server). Thank you. However, I am attempting to use a php form that uses mail() to send out the information. Works fine when I am using an external hosting site like Hostinger, but it doesn't work within my own server..... the result is: it opens up the php file showing the code. Any advice on how to set it up so php recognizes the mail() service I just installed based on the above article??

Leave a reply

Please fill the required fields.Please accept the privacy checkbox.Please fill the required fields and accept the privacy checkbox.

Thank you! Your comment has been successfully submitted. It will be approved within the next 24 hours.