Configure Postfix SMTP Authentication on Ubuntu VPS Server

By Raman Kumar

Share:

Updated on Apr 29, 2026

Configure Postfix SMTP Authentication on Ubuntu VPS Server

Installing and Preparing Postfix on Ubuntu VPS

Your VPS needs secure email delivery to prevent relay abuse and keep messages out of spam folders. This guide shows you how to configure Postfix SMTP authentication on Ubuntu with SSL/TLS encryption.

Start by updating your system and installing the necessary packages:

sudo apt update
sudo apt install postfix sasl2-bin libsasl2-modules -y

During installation, select "Internet Site" and enter your server's fully qualified domain name (FQDN). Miss this step? Fix it later:

sudo dpkg-reconfigure postfix

Your Hostperl VPS comes with full root access, making these configuration changes straightforward.

Installation creates the main config file at /etc/postfix/main.cf.

Creating SASL Authentication Database

SMTP authentication uses SASL (Simple Authentication and Security Layer).

Create a password database for authenticated users:

sudo nano /etc/postfix/sasl_passwd

Add your email credentials in this format:

[smtp.gmail.com]:587 username@gmail.com:password
[smtp.mailgun.org]:587 postmaster@yourdomain.com:your-api-key

Replace these with your actual SMTP provider details. Many customers use Gmail's SMTP for reliability. Others prefer dedicated services like Mailgun or SendGrid.

Secure the password file and create the database:

sudo chmod 600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

This creates a sasl_passwd.db file that Postfix reads efficiently.

Configure Postfix SMTP Authentication Settings

Edit the main configuration file:

sudo nano /etc/postfix/main.cf

Add these lines at the end:

# SMTP Authentication
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_use_tls = yes
smtp_tls_security_level = encrypt
smtp_tls_note_starttls_offer = yes

For Gmail specifically, add these settings:

# Gmail-specific settings
relayhost = [smtp.gmail.com]:587
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

The relayhost tells Postfix where to send outgoing emails. This works whether you're running a single WordPress site or managing multiple client sites.

Setting Up SSL/TLS Certificates

Secure email transmission requires proper SSL/TLS configuration. Ubuntu includes a comprehensive certificate bundle. You can also use Let's Encrypt certificates if you've already set them up.

Add TLS settings to main.cf:

# TLS Configuration
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtp_tls_loglevel = 1

For servers handling sensitive business email, consider using dedicated certificates. The smtp_tls_loglevel = 1 provides useful debugging without overwhelming your logs.

Using a custom certificate? Update the CA file path:

smtp_tls_CAfile = /path/to/your/ca-bundle.crt

Many managed VPS hosting customers find this more reliable than system-wide certificate stores.

Configuring SASL Authentication for Local Submission

To accept authenticated submissions from mail clients, configure the submission service:

sudo nano /etc/postfix/master.cf

Uncomment and modify the submission line:

submission inet n       -       y       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_sasl_type=dovecot
  -o smtpd_sasl_path=private/auth
  -o smtpd_reject_unlisted_recipient=no
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject

This assumes you're also running Dovecot for IMAP/POP3 services. Using Postfix only for outgoing mail? Use the built-in SASL mechanism instead.

Testing SMTP Authentication

Test your configuration before production.

Check the Postfix setup:

sudo postfix check

No errors? Reload the configuration:

sudo systemctl reload postfix

Test authentication with a simple mail command:

echo "Test email body" | mail -s "Test Subject" recipient@example.com

Monitor the mail logs for authentication attempts:

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

Look for "authentication successful" or "SASL" to confirm everything works. Failed attempts show specific error messages for troubleshooting.

Need comprehensive email testing? Check our complete email server setup guide covering both incoming and outgoing mail.

Security Hardening and Best Practices

Prevent your server from becoming an open relay.

Add these restrictions to main.cf:

# Security restrictions
smtpd_recipient_restrictions = permit_mynetworks,
    permit_sasl_authenticated,
    reject_unauth_destination
    
smtpd_sender_restrictions = permit_mynetworks,
    permit_sasl_authenticated,
    reject_unknown_sender_domain

Configure rate limiting to prevent abuse:

# Rate limiting
anvil_rate_time_unit = 60s
anvil_status_update_time = 600s
smtpd_client_connection_rate_limit = 10

These settings allow 10 connections per minute per IP. Adjust based on your legitimate sending patterns.

Monitor authentication failures regularly. Repeated failures from the same IP often indicate brute-force attempts:

sudo grep "authentication failed" /var/log/mail.log | tail -20

Consider implementing fail2ban rules for Postfix authentication failures. Our server security guide covers fail2ban configuration in detail.

Troubleshooting Common Authentication Issues

Authentication problems usually stem from configuration errors or credential issues.

Here are the most common problems and solutions:

"Authentication failed" errors: Check your credentials in /etc/postfix/sasl_passwd.

Regenerate the database after any changes:

sudo postmap /etc/postfix/sasl_passwd
sudo systemctl restart postfix

TLS connection failures: Verify your CA certificate bundle is current:

sudo apt update
sudo apt install --reinstall ca-certificates

Gmail-specific issues: Enable "Less secure app access" or use App Passwords for two-factor authentication accounts. Google's security settings often block VPS connections.

Port blocking: Some ISPs block outbound port 25.

Test alternative ports:

telnet smtp.gmail.com 587

Connection times out? Your ISP likely blocks SMTP traffic. Contact your hosting provider for relay options or try port 465 (SMTPS).

For persistent issues, our email troubleshooting checklist provides systematic debugging steps.

Monitoring and Maintenance

Regular monitoring ensures your SMTP authentication keeps working.

Set up log rotation to prevent mail logs from consuming excessive disk space:

sudo nano /etc/logrotate.d/postfix

Add this configuration:

/var/log/mail.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
    postrotate
        systemctl reload postfix
    endscript
}

Monitor these metrics weekly:

  • Authentication success rates
  • Failed authentication attempts
  • Queue sizes and delivery delays
  • SSL/TLS handshake failures

Create a simple monitoring script:

#!/bin/bash
echo "=== Postfix Queue Status ==="
postqueue -p
echo "=== Recent Auth Failures ==="
grep "authentication failed" /var/log/mail.log | tail -5

Save this as /usr/local/bin/postfix-status.sh and make it executable. Run it weekly to catch issues early.

Ready to deploy your own mail server? Our VPS hosting plans include full root access and dedicated IPv4 addresses, perfect for running Postfix with SMTP authentication. Get started with reliable infrastructure that supports your email delivery needs.

Frequently Asked Questions

Can I use multiple SMTP providers with Postfix authentication?

Yes, but it requires transport maps to route different domains through different providers. Add multiple entries to your sasl_passwd file and configure transport maps in main.cf to specify which provider handles each domain.

Why do my emails still go to spam with SMTP authentication?

SMTP authentication alone doesn't guarantee inbox delivery. You also need proper SPF, DKIM, and DMARC records configured for your domain. Many customers benefit from using established SMTP services like Mailgun or SendGrid for better reputation.

How do I backup my Postfix authentication configuration?

Copy your main.cf, master.cf, and sasl_passwd files to a secure location. Include any custom certificate files and transport maps. Regular configuration backups prevent lengthy reconfiguration after server migrations or failures.

Can I use OAuth2 instead of password authentication?

Postfix doesn't natively support OAuth2 for SMTP authentication. You'll need to use App Passwords for Gmail or API keys for services like SendGrid. Some third-party patches exist, but they're not recommended for production environments.

How do I enable SMTP authentication logging for debugging?

Set smtp_tls_loglevel = 2 and smtpd_sasl_loglevel = 2 in main.cf for detailed authentication logging. Remember to reduce these levels after debugging to prevent log file bloat in production environments.