Post

How to set up your email server using Postfix

How to set up your email server using Postfix

Postfix Installation Guide on Ubuntu

🛠️ Things To Do Before Installing Postfix

1. Set a Correct Hostname

Postfix uses your server’s hostname to identify itself when communicating with other MTAs.

  • Recommended format: Fully Qualified Domain Name (FQDN)
    Example: mail.yourdomain.com

To check current FQDN:

1
hostname -f

To set an FQDN:

1
sudo hostnamectl set-hostname mail.yourdomain.com

Then reboot your server.


2. Set Up DNS Records

Set these in your DNS hosting provider’s dashboard (e.g., NameCheap, GoDaddy, AWS Route53).

MX Record

Tells MTAs where to send email for your domain:

1
2
3
4
Host: @  
Type: MX  
Value: mail.yourdomain.com  
Priority: 0

Add new MX record

A Record

Maps FQDN to your server’s IP:

1
mail.yourdomain.com    →    your_server_public_ipv4_address

Add new A record

PTR Record (Reverse DNS)

Links your IP to FQDN:

1
2
3
dig -x <your_ip> +short
# or
host <your_ip>

You must set this in your hosting provider’s dashboard. The PTR should match mail.yourdomain.com.

📌 Note: Gmail will check that the PTR’s hostname resolves back to the IP.


📦 Installing Postfix

1
2
sudo apt-get update
sudo apt-get install postfix -y

Configuration Options

Select Internet Site.

Select Postfix configuration

Set System Mail Name to your domain (e.g., yourdomain.com).

⚠️ Don’t use mail.yourdomain.com unless you want email addresses like [email protected].

Postfix creates /etc/postfix/main.cf and starts automatically.

Check version:

1
postconf mail_version

Example output:

1
mail_version = 3.6.4

Verify Postfix is listening:

1
sudo ss -lnpt | grep master

List installed Postfix binaries:

1
dpkg -L postfix | grep /usr/sbin/

🔓 Open TCP Port 25 in Firewall

If UFW is enabled:

1
sudo ufw allow 25/tcp

Scan open ports externally to verify.

Scan open ports externally


🌐 Check If TCP Port 25 (Outbound) Is Blocked

Install telnet:

1
sudo apt install telnet

Test connection:

1
telnet gmail-smtp-in.l.google.com 25

If blocked, contact your hosting provider or use an SMTP relay/VPS.

🔄 Can I change the port? No. SMTP servers expect port 25.


✉️ Sending Test Email

1
echo "test email" | sendmail [email protected]

Check inbox (or spam). From address is auto-generated using system mail name.

To find user inbox:

1
postconf mail_spool_directory

Mail log:

1
/var/log/mail.log

🪛 Troubleshooting Email Delivery

If you can’t send mail despite port 25 being open:

1
sudo nano /var/log/mail.log

You might see errors like:

1
550-5.7.1 ... does not meet IPv6 sending guidelines ...

➡️ Set AAAA and PTR records for your IPv6, or disable IPv6.


📬 Using the mail Program

Install:

1
sudo apt-get install mailutils

Send:

  • Press Ctrl+D to send
  • To read email:
1
mail

Mail Program Commands

CommandDescription
1Read first email
hShow headers
nNext message
d 1Delete message 1
d 1 2 3Delete multiple
reply 1Reply to message 1
qQuit (moves mail to mbox)
xExit without modifying mail

📎 Increase Attachment Size Limit

Check current size:

1
postconf | grep message_size_limit

Increase to 50MB:

1
sudo postconf -e message_size_limit=52428800

Check mailbox limit:

1
postconf | grep mailbox_size_limit

If it’s 0, there is no mailbox limit.

Restart Postfix:

1
sudo systemctl restart postfix

🏷️ Set Postfix Hostname

Edit config:

1
sudo nano /etc/postfix/main.cf

Set:

1
myhostname = mail.yourdomain.com

Restart:

1
sudo systemctl restart postfix

📬 Creating Email Alias

Edit:

1
sudo nano /etc/aliases

Add:

1
2
postmaster: root
root: yourusername

Rebuild aliases:

1
sudo newaliases

🌐 Disable IPv6 in Postfix (Optional)

1
2
3
4
5
postconf inet_protocols
# Output: inet_protocols = all

sudo postconf -e "inet_protocols = ipv4"
sudo systemctl restart postfix

⬆️ Upgrading Postfix

If prompted during upgrade, select No configuration to keep your settings.

Select Postfix upgrade


✅ Summary

You now have a working Postfix mail server capable of:

  • Sending/receiving plain text emails
  • Managing emails via command-line tools

🔜 Next Step

In the next part of this tutorial series, we will learn how to install Dovecot IMAP server and enable TLS encryption, which will allow us to use a desktop mail client like Mozilla Thunderbird to send and receive emails.

This post is licensed under CC BY 4.0 by the author.