How to setup mail server in ubuntu

Setting up a mail server in Ubuntu can be a bit complex, but I'll guide you through the process. We'll use Postfix, a popular and widely-used mail transfer agent (MTA), to set up a basic mail server.

Prerequisites:

  1. Ubuntu 18.04 or later (other versions may work, but this guide is specifically for 18.04)
  2. A static IP address or a dynamic DNS (DDNS) service
  3. A domain name (e.g., example.com)
  4. A mail client (e.g., Thunderbird, Outlook) to test the mail server

Step 1: Install Postfix

Open a terminal and run the following command:

sudo apt-get update
sudo apt-get install postfix

This will install Postfix and its dependencies.

Step 2: Configure Postfix

Edit the Postfix configuration file using the following command:

sudo nano /etc/postfix/main.cf

Add the following lines to the end of the file:

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

Replace example.com with your domain name.

Step 3: Configure DNS

You'll need to set up DNS records for your domain. Create the following records:

  1. A record: mail.example.com pointing to your server's IP address
  2. MX record: mail.example.com with a priority of 10 (you can use a lower priority if you have multiple mail servers)

You can use a DNS service like Cloudflare or your registrar's DNS management tool to create these records.

Step 4: Configure Postfix to use DNS

Edit the Postfix configuration file again:

sudo nano /etc/postfix/main.cf

Add the following lines to the end of the file:

mynetworks = 127.0.0.0/8 [::1]/128
relayhost =

This will allow Postfix to use DNS to resolve mail server addresses.

Step 5: Restart Postfix

Restart the Postfix service to apply the changes:

sudo service postfix restart

Step 6: Test the Mail Server

Use a mail client (e.g., Thunderbird) to send an email to a test address (e.g., [email protected]). If everything is set up correctly, you should receive the email.

Additional Configuration (Optional):

  1. Spam filtering: You can install SpamAssassin to filter out spam emails. Run sudo apt-get install spamassassin and configure it according to the documentation.
  2. Authentication: You can set up authentication using SASL (Simple Authentication and Security Layer) or Dovecot. This will allow users to log in to the mail server using their email addresses and passwords.
  3. Webmail: You can install a webmail client like Roundcube or SquirrelMail to access your email account through a web interface.

Troubleshooting:

If you encounter issues, check the Postfix logs for errors:

sudo tail -f /var/log/mail.log

You can also use the postfix command to test the mail server:

sudo postfix check

This will check the Postfix configuration and report any errors.

That's it! You now have a basic mail server set up in Ubuntu using Postfix.