VPS

How to Install and Setup Mail Server on Ubuntu 18.04 LTS

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 install and set up an Ubuntu mail server on a VPS server running Ubuntu 18.04. There are numerous alternatives and ways to create an email server in Linux; however, we will focus on Postfix!

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.test

At this point, we must take into account that the IP address of our Ubuntu 18.04 machine is 192.168.250.7, it is necessary to replace it with the IP address where we will perform the installation. For this example we’ll use mail.test.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.test

Then, add the following:

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

Remember, we must replace the IP address with that of your server, and change the domain to the one 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 test.com. /var/cache/bind/db.test

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 "test.com." {
       type master;
       file "db.test";
};

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

Configuring the current connection

When finished, it is necessary to change the DNS server to the Ubuntu email server, since it will be our DNS server too.

1. Download the Network-Manager

To do this we can use the network-manager. Install it by entering the following command into the command line.

sudo apt install network-manager

2. Display All Active Connections

After that, we’ll show all the active connections.

sudo nmcli connection show

The active connection will be shaded green.

3. Select the Connection to Edit

Now that we know which connection to edit, we can move on. In the following command replace “[active connection name]” with that of yours:

sudo nmcli connection edit "[active connection name]"

4. Remove Existing DNS server

Remove the DNS server with the following command:

Nmcli> remove ipv4.dns

5.  Set the New DNS server

To set, the new DNS server you’ll need to run these following commands:

nmcli> set ipv4.dns [mail_server]

Save the changes and exit:

nmcli> save
nmcli> quit

6. Restart the Connection

The next step is to restart our connection for the changes to take effect. Remember to enter in your connection’s name:

sudo nmcli connection down "[active connection name]"
sudo nmcli connection up "[active connection name]"

7. Change Current Hostname

And the last step before installing the mail server – change the current hostname:

sudo hostnamectl set-hostname mail.test.com

We recommend to log out and in of your server before the next section.

How to Install and Setup Mail Server on Ubuntu 18.04

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 test.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)

This must be done because in Ubuntu 18.04 only users who are in the mail group can make use of this utility.

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@test.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@test.com

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

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 company, it can be very useful to have a configured and functional email server.

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!

 

About the author

Edward S.

Edward is Hostinger's copywriter. He's an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward's goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Add Comment

Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get Exclusive Content

Join thousands of webmasters who get our free newsletter with tips on how to drive more traffic and revenue to their websites!

Please wait...

Thank you for sign up!

More in VPS
How to Show Users in MySQL using Linux
Close