How to Install and Set Up 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 install and set up an Ubuntu mail server on a virtual private server running Ubuntu. 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 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.options
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 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 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)
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.
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 server 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!
Comments
August 08 2019
i want create mail saver
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?
February 02 2021
Hi there, Jeremy! You should be able to connect your email to email clients like Outlook, Thunderbird, etc. You'd need to use mail.domain.tld or IP as a host.
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,
February 09 2021
Hi, Daniel :) Could you share what message you get after running that command on your server?
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.
September 16 2021
Hi Patrick! From the error you mentioned, it looks like your PTR record might not be setup - make sure to check that out.
September 08 2021
do we have any dashboard to view our inbox and compose mail after creating mail server
September 20 2021
Hi there! From here, you can go ahead and install a webmail application like Roundcube or set up your emails with an email client like Thunderbird, Apple Mail, Outlook, etc.
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!
December 22 2021
Hi Arham! I have removed the email address you've shared to make sure you don't get spammed :) As I understand, you'd like to create new email accounts for your VPS and then connect them to your Gmail, is that right? In that case, you'd need to create emails with VPS as per usual and then connect them to Gmail following this method - just make sure to use your VPS mail server details instead of those of Hostinger mail.
February 01 2022
Can I host my domain with another hosting company and still send email through the hostinger VPS server? If yes how?
February 04 2022
Hi there, yes, it is possible, but you'd need to make sure to setup your DNS well. You can find the necessary A and MX DNS records in step 3. If you're having any trouble with it, feel free to check in with our Customer Success team :)
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?
May 31 2022
Hi there! We don't have a tutorial about it yet, but I'll be sure to pitch it to the team and create one eventually! In the meantime, feel free to check this one - it shouldn't be too different on Hostinger VPS ?
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??
February 22 2024
Hi Roy! Make sure the php.ini file is set up correctly to utilize the sendmail path or SMTP settings that match your mail server setup: 1. Open php.ini with root permissions in a text editor. 2. Look for the line with sendmail_path. It may be commented out (prefixed with a ;). 3. Set it to point to your sendmail executable, typically /usr/sbin/sendmail, with the correct options like -t -i. For instance: sendmail_path = "/usr/sbin/sendmail -t -i" 4. Save and close the editor, then restart the web server. Hope it helps ?