How to Install and Configure Postfix as a Mail Transfer Agent

Postfix is the most widely used open-source Mail Transfer Agent (MTA) on Linux, handling the sending and relaying of email. This guide covers a basic production-ready installation.

Prerequisites

  • Ubuntu 22.04/24.04 or Debian 11/12 VPS
  • A domain name with DNS access
  • Root or sudo access
  • Hostname already set (see How to Set Up Reverse DNS for details)

Step 1 — Set the System Hostname

sudo hostnamectl set-hostname mail.yourdomain.com

Step 2 — Install Postfix

sudo apt update
sudo apt install postfix -y

During installation, choose "Internet Site" and enter your domain (e.g. yourdomain.com) as the system mail name.

Step 3 — Review the Main Configuration

sudo nano /etc/postfix/main.cf

Verify these key values:

myhostname = mail.yourdomain.com
mydomain = yourdomain.com
myorigin = /etc/mailname
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

Step 4 — Enable TLS for Encrypted Mail Transfer

smtpd_tls_cert_file=/etc/letsencrypt/live/mail.yourdomain.com/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/mail.yourdomain.com/privkey.pem
smtpd_use_tls=yes
smtpd_tls_security_level=may
smtp_tls_security_level=may

Obtain the certificate first via Certbot (standalone mode, since no web server may be running on this host):

sudo apt install certbot -y
sudo certbot certonly --standalone -d mail.yourdomain.com

Step 5 — Restart Postfix

sudo systemctl restart postfix
sudo systemctl enable postfix

Step 6 — Open Required Ports

sudo ufw allow 25/tcp
sudo ufw allow 587/tcp
sudo ufw allow 465/tcp

Step 7 — Test Sending

echo "Test message body" | mail -s "Test Subject" [email protected]

If mail isn't installed:

sudo apt install mailutils -y

Step 8 — Check the Mail Queue

mailq

View Postfix logs:

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

Next Steps: Authentication

A working Postfix installation alone isn't enough for reliable delivery — configure SPF, DKIM, and DMARC (see How to Configure SPF, DKIM, and DMARC) and set up reverse DNS before sending real mail.

Common Errors

"Connection timed out" sending to port 25 — some networks/providers block outbound port 25 by default; contact your VPS provider to confirm it's open for legitimate mail sending.

Mail stuck in queue:

postqueue -p
sudo tail -50 /var/log/mail.log

Usually indicates a DNS resolution failure for the recipient domain, or the recipient server rejecting the connection.

Best Practices

  • Always enable TLS for both submission and relay
  • Set up authentication (SPF/DKIM/DMARC) before sending any real volume
  • Restrict relay access so your server can't be abused as an open relay for spam

FAQ

Is Postfix free?
Yes, Postfix is free and open source.

Related Articles

  • How to Install and Configure Dovecot for IMAP/POP3
  • How to Configure SPF, DKIM, and DMARC (Complete Guide)
  • How to Set Up Reverse DNS (PTR/rDNS) for Email Deliverability
  • postfix, mail transfer agent, mta, smtp server, self hosted email
  • 0 användare blev hjälpta av detta svar
Hjälpte svaret dig?

Relaterade artiklar

How to Set Up a VPS for Email Marketing (Complete Guide)

Running your own email marketing infrastructure on a VPS gives you full control over...

How to Configure SPF, DKIM, and DMARC (Complete Guide)

SPF, DKIM, and DMARC are the three DNS-based email authentication standards that Gmail, Outlook,...

How to Set Up Reverse DNS (PTR/rDNS) for Email Deliverability

Reverse DNS (PTR record) maps your server's IP address back to a hostname — the opposite of...

IP Warm-Up: A Safe 30-Day Schedule for New Sending IPs

Mailbox providers like Gmail and Outlook monitor sending behavior closely and treat sudden...

Best Email Marketing Software for a VPS (MailWizz, Mautic, Listmonk & More)

Choosing the right self-hosted email marketing platform affects deliverability, ease of...