Home How ToHow to Install and Configure Postfix SMTP Server on Ubuntu

How to Install and Configure Postfix SMTP Server on Ubuntu

by Knowledge Base Linux
How to Install and Configure Postfix — How to Install and Configure Postfix SMTP Server on Ubuntu

Setting up your own Postfix SMTP server on Ubuntu gives you full control over outgoing mail for your applications, monitoring alerts and transactional messages, without depending on a third-party relay. While full mail suites exist, many sysadmins only need a lightweight, reliable server that can send email from a VPS. In this guide you will install and configure a Postfix SMTP server step by step, secure it with TLS, and verify that messages actually leave the server.

Prerequisites – How to Install and Configure Postfix

Before you begin, make sure you have the following:

  • An Ubuntu 22.04 or 24.04 server with sudo access
  • A fully qualified domain name (FQDN) such as mail.example.com pointing to your server IP
  • Port 25 open in your firewall and allowed by your hosting provider
  • Access to your DNS zone so you can add SPF, DKIM and DMARC records later

If your firewall is not configured yet, follow our guide to install and configure UFW on Ubuntu first, because a mail server with the wrong ports open will not send or receive anything.

Step 1: Install the Postfix SMTP Server – How to Install and Configure Postfix

Update the package index and install Postfix together with the mailutils package, which provides the mail command for testing:

sudo apt update
sudo DEBIAN_FRONTEND=noninteractive apt install -y postfix mailutils

Installing with DEBIAN_FRONTEND=noninteractive skips the interactive configuration screen. If you run the installer interactively instead, select Internet Site and enter your domain name when prompted.

Step 2: Configure the Postfix SMTP Server Basics

The main configuration lives in /etc/postfix/main.cf. Open it and set the core identity parameters:

sudo nano /etc/postfix/main.cf
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
inet_interfaces = all
inet_protocols = ipv4
home_mailbox = Maildir/
smtpd_banner = $myhostname ESMTP

Next, restrict which networks may relay mail through your server. For a send-only server on a single VPS, limiting relay to localhost and your own network prevents abuse as an open relay:

mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, defer_unauth_destination

Apply the changes and restart the service:

sudo postfix check
sudo systemctl restart postfix
sudo systemctl enable postfix

The postfix check command validates the configuration and warns about permission or syntax problems before you rely on it.

Configuring a Postfix SMTP server from the Linux terminal

Step 3: Open the Firewall and Test Sending

Allow SMTP traffic through UFW:

sudo ufw allow Postfix
sudo ufw allow 25/tcp

Now send a test email to an external address you control:

echo "This is a test from my Postfix server" | mail -s "Postfix test" [email protected]

Follow the mail queue and log in real time to confirm delivery:

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

A line containing status=sent means the receiving server accepted the message. If the message lands in the spam folder, do not panic: deliverability depends on the DNS records in the next step, not only on Postfix itself.

Step 4: Add DNS Records for Deliverability

A Postfix SMTP server without correct DNS records will be treated as spam by Gmail, Outlook and most providers. Add these records for example.com:

  • Reverse DNS (PTR): set in your hosting panel so the server IP resolves back to mail.example.com
  • SPF (TXT): v=spf1 ip4:YOUR_SERVER_IP -all
  • DKIM: install OpenDKIM, generate a key pair and publish the public key in DNS:
sudo apt install -y opendkim opendkim-tools
sudo opendkim-genkey -b 2048 -d example.com -s default -v
  • DMARC (TXT) on _dmarc.example.com: v=DMARC1; p=none; rua=mailto:[email protected]
  • Start with p=none to monitor reports, then tighten the policy to quarantine or reject once legitimate mail passes both SPF and DKIM checks. The official Postfix documentation describes every configuration parameter used here in detail.

    Step 5: Secure the Server with TLS

    If users or applications authenticate to submit mail on port 587, encrypt the connection with a free Let’s Encrypt certificate:

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

    Then point Postfix to the certificate and enable the submission service in /etc/postfix/master.cf by uncommenting the submission block:

    smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example.com/fullchain.pem
    smtpd_tls_key_file = /etc/letsencrypt/live/mail.example.com/privkey.pem
    smtpd_tls_security_level = may
    smtp_tls_security_level = may
    sudo systemctl restart postfix

    Troubleshooting

    Messages stuck in the queue: run postqueue -f to force a flush and read the reason in /var/log/mail.log. A common cause is the provider blocking outbound port 25; open a support ticket or use a relay host on port 587.

    “Relay access denied”: the client IP is not in mynetworks and is not authenticated. Either add the trusted network or configure SASL authentication instead of widening mynetworks to the world.

    Postfix will not start: run sudo postfix check and systemctl status postfix. Most failures are typos in main.cf or another process already listening on port 25, which you can find with sudo ss -tlnp | grep :25.

    Conclusion

    You now have a working Postfix SMTP server on Ubuntu: it sends mail with a correct hostname, encrypted submission, and the SPF, DKIM and DMARC records that modern providers require. Keep the server updated, watch /var/log/mail.log for abuse, and monitor your IP reputation regularly. If you later need webmail, quotas and a full administration panel, you can graduate to a complete suite, but for reliable sending from your own infrastructure, Postfix alone is hard to beat.

    You may also like

    KbLinux

    Typically replies within a day

    Hello, Welcome to the site. Please click below button for chatting me through Telegram.