How To Setup A Mail Server In Ubuntu?

How To Setup A Mail Server In Ubuntu

If you use your own email server, it will control all the network traffic and help you in managing the services clearly. Let’s see how to set up a mail server on Ubuntu. In the following section, the steps to set up a VPS server that runs on Ubuntu 18.04 and How To Setup A Mail Server In Ubuntu are given.

Now, let us see how to set up to forward all the incoming emails to another email address.

Create an email server in Linux using Postfix by following the steps that are given below

Step 1: Updating your server

  1. First, sign in to your server using Secure Shell.
  2. Open the Terminal window and enter ssh Make sure to replace with your IP address or domain name.
  3. When you are prompted for confirmation, type Yes and then enter the root password.
  4. Now, update your server by typing apt-get update.

Step 2: Installing Bind

  1. You will require an additional tool named Bind to configure a DNS server using Postfix.
  2. Enter sudo apt install bind9.

Step 3: Configuring /var/cache/db.test

  1. Let us consider as the IP address of your server. Make sure you replace it with your server’s IP address.
  2. To create a new a new file with the information about the zone, type sudo nano /var/cache/bind/db.test.
  3. Now, you can add the following details in the file.
  4. $ORIGIN $TTL 1D @ IN SOA ns1 root(                 1 ;serial                 1D ;refresh                 2H ;retry                 2W ;expire                 5H ;minimum ); @ IN NS ns1 ns1 IN A mail IN A @ IN MX 5 mail

  5. To save the settings you have made, press the Ctrl+O buttons together, and press Ctrl+X to close the editor.

Step 4: Adding a new zone

  1. You have to check the configuration of the file before you enable the newly created zone.
  2. Type sudo named-checkzone /var/cache/bind/db.test.
  3. To bind configuration, enter sudo nano /etc/bind/named.conf.default-zones.
  4. Save the settings you have made and close the nano editor.

Step 5: Configuring /etc/bind/named.conf.options

  1. Include the Google DNS number If that entry is already present in the conf file, just remove the // symbols.
  2. Enter sudo nano /etc/bind/named.conf.options to make the changes to the file.

Step 6: Restarting Bind

  1. Now, you have to restart the Bind service before configuring the connection.
  2. Enter sudo systemctl reload bind9.

Step 7: Configuring the connection

  • You have to change the DNS server to the Ubuntu mail server by following the instructions given below.
  • Download Network Manager by entering sudo apt install network-manager.
  • Type sudo nmcli connection show to view all active connections.
  • Select the connection you want to edit. Now, enter sudo nmcli connection edit “[active connection name].” 
  • Make sure to replace ‘active connection name’ with the name of the connection.
  • To remove the existing DNS server, type Nmcli> remove ipv4.dns.
  • To create a new DNS Server, enter nmcli> set ipv4.dns [mail_server].
  • Enter nmcli> save and nmcli> exit to save and close it.
  • Now, you can restart your connection by entering the following commands. Make sure to replace your connection name in the active connection name section.
  • sudo nmcli connection down “[active connection name]” sudo nmcli connection up “[active connection name]”

  • Change your hostname by entering sudo hostnamectl set-hostname
  • Make sure to log out and then log in to your server before setting up the mail server.

Step 8: Installing and setting up a Mail server:

  1. To install the Postfix mail server, enter sudo apt install postfix.
  2. You can add users to the mail group by entering sudo usermod -aG mail $(whoami).
  3. We have to add users to send and receive emails. Here we add user1.
  4. sudo useradd -m -G mail -s /bin/bash/ user1
  5. Enter sudo passwd User to create a password.

Step 9: Testing the Ubuntu Mail server

  1. You have to install mailutils package to send and receive emails from the Terminal.
  2. Enter sudo apt install mailutils.
  3. To create an email, type mail
  4. Once you have added your subject and message, press the Ctrl+D buttons together.
  5. You can also add another email and test the server.
  6. You have now successfully set up the mail server on Ubuntu 18.04 and How To Setup A Mail Server In Ubuntu.