How to make a mail server in ubuntu

Setting up a mail server in Ubuntu! That's a great project. Here's a step-by-step guide to help you get started:

Prerequisites:

  1. Ubuntu 18.04 or later (other versions may work, but this guide is specifically written for 18.04)
  2. A static IP address for your server (if you're using a dynamic IP, you'll need to configure your router to forward incoming mail traffic to your server)
  3. A domain name (e.g., example.com) and a DNS provider (e.g., Cloudflare, Google Domains)

Step 1: Install required packages

Open a terminal and run the following commands:

sudo apt update
sudo apt install postfix dovecot-imapd dovecot-pop3d opendkim opendmarc

These packages will install the Postfix mail transfer agent, Dovecot IMAP and POP3 server, OpenDKIM (for domain key signing), and OpenDMARC (for message authentication).

Step 2: Configure Postfix

Edit the Postfix configuration file:

sudo nano /etc/postfix/main.cf

Add the following lines at the end of the file:

mydestination = example.com
myhostname = mail.example.com
myorigin = $myhostname

Replace example.com with your domain name.

Step 3: Configure Dovecot

Edit the Dovecot configuration file:

sudo nano /etc/dovecot/dovecot.conf

Add the following lines at the end of the file:

mail_location = maildir:~/Maildir
first_valid_uid = 1000
first_valid_gid = 1000

These settings configure Dovecot to use the maildir format for storing mail and set the minimum UID and GID for user accounts.

Step 4: Configure OpenDKIM

Edit the OpenDKIM configuration file:

sudo nano /etc/opendkim.conf

Add the following lines at the end of the file:

KeyTable: /etc/opendkim/KeyTable
SigningTable: /etc/opendkim/SigningTable

Create the KeyTable and SigningTable files:

sudo nano /etc/opendkim/KeyTable

Add the following lines:

default._domainkey.example.com example.com:default

Create the SigningTable file:

sudo nano /etc/opendkim/SigningTable

Add the following lines:

default example.com

Step 5: Configure OpenDMARC

Edit the OpenDMARC configuration file:

sudo nano /etc/opendmarc.conf

Add the following lines at the end of the file:

p = example.com
sp = example.com

Replace example.com with your domain name.

Step 6: Restart services

Restart the Postfix, Dovecot, OpenDKIM, and OpenDMARC services:

sudo service postfix restart
sudo service dovecot restart
sudo service opendkim restart
sudo service opendmarc restart

Step 7: Configure DNS

Update your DNS records to point to your mail server:

  1. Create a TXT record for example.com with the value v=spf1 a mx ip4:your-static-ip -all
  2. Create a TXT record for example.com with the value k=rsa; p=your-public-key
  3. Create a MX record for example.com with the priority 10 and the value mail.example.com

Step 8: Test your mail server

Use a mail client (e.g., Thunderbird, Outlook) to send an email to a test address (e.g., [email protected]). Verify that the email is delivered successfully.

That's it! You now have a basic mail server setup in Ubuntu. You can further customize and secure your mail server by configuring additional settings, such as spam filtering, virus scanning, and SSL/TLS encryption.