Setting up an email server using Postfix
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.
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>
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 reload
sudo service postfix restart
Your /etc/postfix/main.cf will look like this:
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.
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.
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. 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! 💚