---
title: Setting up an email server using Postfix
description: The purpose of this post is to enable you to set up your own email server. For this purpose, we chose Postfix as a mail server.
image: https://www.cegal.com/hubfs/Microsoft-sql-server-2022.jpg
---

# Setting up an email server using Postfix

Cegal Tech Advisors Fighting fuzz by striving to make your coding life more straight forward.

03/24/2020 |

The purpose of this post is to enable you to set up your own email server. For this purpose, we chose Postfix as a mail server. Postfix is a free and open-source mail transfer agent that routes and delivers electronic mail.

In this post, we will explain how to install and configure the Postfix mail server on the Ubuntu 18.4 server.

## Prerequisites

For installing a Postfix server, your user should have sudo privileges (admin rights for installing Postfix). If you want to receive emails from the internet, you should have DNS Records for Your Mail Server. You must ensure Port 25 is open so Postfix can receive emails from other SMTP servers.

## Overview of steps

- **Goal 1**: Setting up a mail server for receiving mail from a local network.
- **Goal 2**: Setting up a mail server for receiving mail from the internet.

## Goal 1: Setting up a mail server for receiving mail from local network

Before starting configuring the Postfix server, you need to make sure that proper entries in /etc/hosts as most of programs will not accept an email using just @localhost as a domain.

```

  vi /etc/hosts
  
```

 

```
Your host file will look something like this.
```

[![message](https://github.com/sysco-middleware/sysco-middleware.github.io/raw/master/images/2020-03-24-Setting-up-email-sever-using-Postfix/hosts.png)](https://github.com/sysco-middleware/sysco-middleware.github.io/blob/master/images/2020-03-24-Setting-up-email-sever-using-Postfix/hosts.png)

### Installing and Configuring Postfix server on Ubuntu with Command-line interface

Installing Postfix on the Ubuntu server is quite a simple task, but after installing Postfix, you need to make sure that you configure Postfix server properly.

Install the Postfix package if it is not installed already.

```

  sudo apt-get install postfix 
  
```

 

### **Configure a Catch-all Address**

Enabling this, you can use any email address ending with "@localhost" or "@localhost.com".

If it does not exist, create file /etc/postfix/virtual:

```

  sudo nano /etc/postfix/virtual 
  
```

```
Add the following 2 lines content, replacing with your Unix user account:
```

```

   @localhost <your-user>
@localhost.com <your-user> 
  
```

 

Save and close the file.

 

### **Configure a Catch-all Address**

To Change the configuration of the Postfix server we need to read this file:

```

  sudo vi /etc/postfix/main.cf 
  
```

And check if this line is enabled in main.cf, or add it if it does not exist:

```

  virtual_alias_maps = hash:/etc/postfix/virtual 
  
```

Save and close this file and activate these changes by running the below commands:

```

  sudo postfix reload
sudo service postfix restart
  
```

### Configure Postfix to use Maildir-style mailboxes and other settings

Open Postfix configuration file

```

  sudo vi /etc/postfix/main.cf 
  
```

Add below lines after inet_protocoles and save main.cf

```

  luser_relay = sysco@localhost
local_recipient_maps =
home_mailbox = Maildir/
disable_mime_output_convesion = yes
strict_8bitmime = yes
strict_7bit_headers = no
strict_8bitmime_body = yes
  
```

Reload postfix and restart it:

```

  sudo postfix reloadsudo service postfix restart
  
```

Your /etc/postfix/main.cf will look like this:

[![message](https://github.com/sysco-middleware/sysco-middleware.github.io/raw/master/images/2020-03-24-Setting-up-email-sever-using-Postfix/maincf.png)](https://github.com/sysco-middleware/sysco-middleware.github.io/blob/master/images/2020-03-24-Setting-up-email-sever-using-Postfix/maincf.png)

### **Optional steps**

Let Postfix know about the domains that it should consider local:

```

  
sudo postconf -e "mydestination = gitlab.example.com, localhost.localdomain, localhost"

  
```

Let Postfix know about the IPs it should consider as part of the LAN: We’ll assume 192.168.1.0/24 is your local LAN. You can safely skip this step if you don’t have other machines in the same local network.

```

  sudo postconf -e "mynetworks = 127.0.0.0/8, 192.168.1.0/24" 
  
```

Remember to reload the Postfix configuration and restart it.

Checking Postfix server status.

```

  sudo service postfix status
  
```

Starting Postfix server.

```

  sudo service postfix start 
  
```

Stopping Postfix server.

```

  sudo service postfix stop 
  
```

```
 
```

### Testing Postfix server

For testing the Postfix server, first, we will install mailutils for sending mail to the local server.

```

  sudo apt install mailutils 
  
```

Execute the below command to send mail to the local mail server

```

  mail -s "Test Subject" 27148485@localhost.com < /dev/null
  
```

and check mail /Maildir/new. If you receive any file in this folder, then your postfix server is configured properly, and your mail server setup is complete for receiving emails from the local network.

## Goal 2: Setting up a mail server for receiving emails from the internet

Postfix requires the server’s hostname to identify itself when communicating with other MTAs. The hostname can have a Single word or a Fully Qualified Domain Name(FQDN). FQDN is usually used for internet-facing servers. You should set up DNS records for your mail server in your DNS hosting service. In this, you have to setup MX record and A record.

**MX reco*rd**: An MX record tells other MTAs that your mail server mail. syscotest.com is responsible for email delivery for your domain name.

```

  MX record    @           mail.syscotest.com
  
```

**  
A record**: An A record maps an FQDN to an IP address.

```

  mail.syscotest.com        <IP-address>
  
```

### **Installing Postfix**

Run below command in terminal to install Postfix server.

```

      sudo apt-get update    sudo apt-get install postfix -y
  
```

```
The installation process will ask you to select mail configuration. You need to select Internet Site for this.
```

[![message](https://github.com/sysco-middleware/sysco-middleware.github.io/raw/master/images/2020-03-24-Setting-up-email-sever-using-Postfix/PostfixInternetSite.png)](https://github.com/sysco-middleware/sysco-middleware.github.io/blob/master/images/2020-03-24-Setting-up-email-sever-using-Postfix/PostfixInternetSite.png)

**No configuration** means the installation process will not configure any parameters.

**Internet Site** means using Postfix for sending emails to other MTAs and receiving email from other MTAs.

**Internet with smarthost** means using Postfix to receive emails from other MTAs but using another smart host to relay emails to the recipient.

**Satellite system** means using a smart host for sending and receiving email.

**Local only** means emails are transmitted only between local user accounts. (For Goal 1, select this option)

In step, it will ask you to enter the domain name for the system mail name. In our case, we have entered “syscotest.com” as the system mail server.

[![message](https://github.com/sysco-middleware/sysco-middleware.github.io/raw/master/images/2020-03-24-Setting-up-email-sever-using-Postfix/syscotest.png)](https://github.com/sysco-middleware/sysco-middleware.github.io/blob/master/images/2020-03-24-Setting-up-email-sever-using-Postfix/syscotest.png)

Once installed, check Postfix configuration file “/etc/postfix/main.cf” by opening it in your favorite editor. Your main.cf will look like this:

```

  myhostname = syscotest.com
mydomain = syscotest.com
myorigin = $mydomain
mydestination = $myhostname, localhost, $mydomain, localhost.localdomain
mynetworks = 127.0.0.0/8, /32
relay_domains = $mydestination
inet_interfaces = all
inet_protocols = all
home_mailbox = Maildir/
  
```

Check Postfix status by executing the below command:

```

  sudo service postfix status 
  
```

```

```

if this command returns the status running, then Postfix is up and running.

**Testing mail server** Before testing this setup, ensure you have mapped your mail server user in “/etc/postfix/virtual”. Your virtual file should look like this:

```

  contact@syscotest.com sysco
admin@syscotest.com Sysco
  
```

```
We can apply the mapping by typing:
```

```

  sudo postmap /etc/postfix/virtual
  
```

Restart the Postfix process to be sure that all our changes have been applied:

```

  sudo systemctl restart postfix
  
```

Go to your Gmail account and send mail to [admin@syscotest.com](mailto:admin@syscotest.com). Mail should be sent successfully, and you should receive mail in “/Maildir/new”.

## Conclusion

Configuring and Managing mail servers can be a difficult task for new administrators, but with this configuration, you should have basic MTA email functionality to get you started. The success factor for mail server configuration depends upon how you configure and run your mail server.

Hope this was helpful! 💚

- [TechBlog ](https://www.cegal.com/en/tech-blog/tag/techblog)
- [Ubuntu ](https://www.cegal.com/en/tech-blog/tag/ubuntu)

<https://www.linkedin.com/shareArticle?mini=true&url=https://www.cegal.com/en/tech-blog/setting-up-an-email-server-using-postfix>

<https://www.facebook.com/sharer/sharer.php?u=https://www.cegal.com/en/tech-blog/setting-up-an-email-server-using-postfix>

## Related articles

<https://www.cegal.com/en/tech-blog/microservices-are-not-always-the-best-choice>

Microsoft

 Microservices Are Not Always The Best Choice!

Christian Gjedrem Christian is a well-versed senior...

![arrow](https://www.cegal.com/hubfs/cegal-website/vectors/resource-card-arrow.svg)

<https://www.cegal.com/en/tech-blog/when-service-broker-fills-your-tempdb>

Database Microsoft

 When Service Broker Fills Your Tempdb: A Real-World SQL Server...

Michael Pettersson Database Specialist with expertise in...

![arrow](https://www.cegal.com/hubfs/cegal-website/vectors/resource-card-arrow.svg)

<https://www.cegal.com/en/tech-blog/security-log-lake-is-this-the-future-or-is-the-current-siem-strategy-enough>

Cyber Security

 Security Log Lake: is this the future or is the current SIEM...

Daniel Andersson Senior Information Security Consultant,...

![arrow](https://www.cegal.com/hubfs/cegal-website/vectors/resource-card-arrow.svg)

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Cegal Tech Advisors",
    "url" : "https://www.cegal.com/en/tech-blog/author/cegal-tech-advisors"
  },
  "dateModified" : "2023-11-28T18:33:41.405Z",
  "datePublished" : "2020-03-24T15:56:00.000Z",
  "headline" : "Setting up an email server using Postfix",
  "image" : [ "https://www.cegal.com/hubfs/Microsoft-sql-server-2022.jpg" ],
  "mainEntityOfPage" : {
    "@id" : "https://www.cegal.com/en/tech-blog/setting-up-an-email-server-using-postfix",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://www.cegal.com/hubfs/Cegal%20white.png"
    },
    "name" : "Cegal "
  }
}
```

```json
{
  "@context" : "https://schema.org",
  "@type" : "Article",
  "author" : [ {
    "@type" : "Person",
    "name" : "Cegal Tech Advisors",
    "url" : "https://www.cegal.com/en/tech-blog/author/cegal-tech-advisors"
  } ],
  "dateModified" : "2023-11-28T18:33:41",
  "datePublished" : "2020-03-24T15:56:00",
  "headline" : "Setting up an email server using Postfix",
  "image" : [ "https://7270560.fs1.hubspotusercontent-na1.net/hubfs/7270560/Microsoft-sql-server-2022.jpg" ]
}
```