Hello everyone, today you will learn how to set up your own email server on Ubuntu or Linux but this being an Ubuntu website first, this is where the article will go from. This tutorial is perfect for beginner’s or pro’s alike as everything is listed step by step so that you won’t get lost, matter of fact, you will be doing a lot more copy and pasting more than anything as you learn.
So have you been wondering how people set up their own mailing server on Ubuntu? How they manage to send or receive emails from their own servers? It’s certainly cheaper than using external email solutions, those can be costly these days and that’s why most people go for Self-Hosted solutions, it could be self-hosting a website or just an email server.
A lot of Linux users use a software known as Mail-in-a-Box, fancy little name right? Yes, and they include the dashes for some reason, but either way, if you are ready to begin installing it, let’s go right ahead.
What is Mail-in-a-box?
Mail-in-a-Box lets you become your own mail service provider in a few easy steps. It’s sort of like making your own gmail, but one you control from top to bottom. Technically, Mail-in-a-Box turns a fresh cloud computer into a working mail server. But you don’t need to be a technology expert to set it up.
- Mail in a Box is an awesome tool that allows you to get your email server up and running in minutes.
- Mail in a Box also uses Fail2ban and SSL certificate for extra layer of security. It supports SPF, DKIM and UFW Firewall to make sure your emails never land in the spam folder.
Note: This tutorial requires minimum of 1GB RAM on your computer.
Steps to set up email server on Ubuntu
Step 1 – Configure the Hostname
First, connect to your instance using SSH:
ssh root@yourinstanceIPaddress
The hostname of your server should be box.yourdomain.com.
You should also name your instance as box.yourdomain.com, so change you instance name and check if it matches what appears in /etc/hostname file, by entering this command:
#~ Hostname
The output should be:
Box.yourdomain.com
If your output is something else, correct it by entering:
sudo echo "box.yourdomain.com" > /etc/hostname
Step 2 – Modify /etc/hosts file
The hostname should be associated with the server’s IP address, for that you can edit the host file using nano editor:
sudo nano /etc/hosts
If nano editor is not installed already, install it by using the below command:
Sudo apt install nano
Then, Modify the host file as shown below:
127.0.0.1 localhost.localdomain localhost
your_instance_ip box.yourdomain.com box
Replace your-_instance_ip and yourdomain.com with your instance’s IP and domain name.
Step 3 – Create Glue records for your server
This step is not necessary, but recommended. You can continue using your Domain Registrar’s provided DNS settings, but handling DNS responsibilities to the Mail-in-a-box server make things a lot easier and hassle free.
To set up the glue records, you’ll have to create 2 nameservers inside your Domain Registrar’s account.
Ns1.box.yourdomain.com.yourinstanceIP
Ns2.box.yourdomain.com.yourinstanceIP
Wait for your DNS settings to propagate.
Step 4 – Installing Mail-in-a-box on your instance.
In this step I’ll show you how to install Mail-in-a-box using 1 single command.
This command will install all necessary packages to set up a mail server on your instance.
First cd into your home directory using the below command:
cd /
Now, enter the following command to install Mail-in-a-box:
curl -s https://mailinabox.email/bootstrap.sh | sudo bash
You’ll be presented with a similar screen
Select OK to continue
It’ll ask you to create an email address, you can use any prefix along with your domain name, like [email protected], [email protected], etc.
Next, it’ll ask you to enter your hostname, which in our case is: box.yourdomain.com
(Remember to replace yourdomain.com with your actual domain name)
Select your Country.
During the installation, it’ll also ask you to enter a password for your email account, Enter any password of your choice.
Once, the installation is completed you’ll see a similar output:
Step 5 – Configure Main-in-a-box account
First, log into your mail-in-a-box account by entering this url in your browser:
https://your_instance_IP/admin#
Now, you’ll be asked to confirm security exceptions, click on “confirm security exceptions”
Now, log into your account using the credentials you created in Step 4.
On your first log in, Mail-in-a-box will verify if all the data is configured correctly. It may ask you to reboot your server, which can be done by entering this simple command in the terminal:
Sudo reboot
If you see a warning about incorrect DNS or MX records, you’ll need to repeat Step 3 again.
Step 6 – Send a Test Email
With everything configured correctly, you should be able to send an email from your server.
To log in to webmail dashboard, click on Mail>Instructions and click on the URL provided.
It’ll be something like this:
https://box.yourdomain.com/mail
Login with the same user credentials.
Here, you can try sending and receiving emails from your alternate email ID. If you can send and receive emails properly, then everything went fine and all you need to do now is install an SSL certificate.
Step 7 – Install SSL
Note: Installing an SSL will get rid of the domain insecure warning.
First, generate a certificate from Letsencrypt, then go to System>SSL>Certificates
There you can paste your SSL certificate and install.
Now, you won’t have to face Unsecure warnings every time you visit your webmail.
That’s it your webmail is now set up and working. You can send and receive emails easily without paying anything.
If you are like me, you might learn easier by watching the installation video below, let us know what you think in the comments and good luck.