How to Postfix SASL SMTP authentication

To configure Postfix with SASL SMTP authentication, you need to configure main.cf for global server settings, open the submission ports in master.cf, define your SASL framework backend, and run validations using openssl and swaks. Below is the complete blueprint using the default, stable Cyrus SASL framework on a Linux system.

1. Global Configuration (/etc/postfix/main.cf)

Add or modify the following directives in your main.cf file. This configures the server-side constraints, forcing security levels and restricting open-relaying to successfully authenticated users, while the server also acts as an Mail Transfer Agent to receive emails from other authoritative relays.

Core SASL Authentication Activation

smtpd_sasl_auth_enable = yes
smtpd_sasl_path = smtpd
smtpd_sasl_type = cyrus
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes

Mandatory TLS Configuration for Safe Authentication

smtpd_tls_security_level = may
smtpd_tls_auth_only = yes
smtpd_tls_cert_file = /etc/letsencrypt/live/yourdomain/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/yourdomain/privkey.pem

## Relay Restrictions Configuration
smtpd_relay_restrictions = 
    permit_mynetworks, 
    permit_sasl_authenticated, 
    defer_unauth_destination

Note. With the setting  smtpd_tls_auth_only = yes  only TLS encrypted authentication is accepted; therefore, smtpd will not accept unencrypted authentication. smtpd_tls_security_level = may  means that Postfix announces and offers encryption, but does not necessarily require it. This option is the default for common mail reception on port 25.

In global email communication, you can’t force every server in the world to support TLS. Using may ensures that you can receive emails from all servers, but automatically selects the secure option if the sender’s server supports it.

2. Service Definitions (/etc/postfix/master.cf)

Uncomment and customize the submission (Port 587) or smtps (Port 465) services to enforce SASL checks strictly on client-facing communication channels.

Submission service on Port 587 (STARTTLS)

submission inet n       -       y       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING

SMTPS service on Port 465 (Implicit TLS)

smtps     inet  n       -       y       -       -       smtpd
  -o syslog_name=postfix/smtps
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING

Note. Make sure that subsequent lines begin with a space or tab before -o is mandatory.

3. Deploying the ACME client and creating the certificate

To install the ACME client on Debian, the Electronic Frontier Foundation (EFF) Certbot get directly from the official Debian repository.

sudo apt install certbot

The Debian package automatically sets up a systemd timer to handle renewals. Verify it is active and testing that the renewal process works:

sudo systemctl status certbot.timer
sudo certbot renew --dry-run

Let’s generate your standalone certificate using Certbot.

sudo certbot --standalone certonly -d yourdomain

4. Backend Verification Framework (/etc/sasl2/smtpd.conf)

Create or update your Cyrus SASL application configuration block. If you want Postfix to authenticate against system accounts, link it to the saslauthd operational daemon.

pwcheck_method: saslauthd
mech_list: PLAIN LOGIN

Make sure to start and enable your system service via:

sudo systemctl enable --now saslauthd

5. Create system user

Create the system user with no shell login permissions.

sudo useradd -m -s /usr/sbin/nologin youruser
sudo passwd yourpassword

6. Testing Protocols

Restart your Postfix SASL SMTP authentication mail subsystem to commit the rules using.

sudo systemctl restart postfix  && systemctl status postfix

7. Testing Services

Verify that the services for Submission Port 587 and SMTPS Port 465 are listening.

sudo netstat -tlpn | egrep ":587|:465"

The output looks something like this.

tcp    0    0 0.0.0.0:587      0.0.0.0:*      LISTEN    19541/master
tcp    0    0 0.0.0.0:465      0.0.0.0:*      LISTEN    19541/master

Method A: Finalizing with OpenSSL

Because if you configured smtpd_tls_auth_only = yes standard unencrypted tests will mask your raw authentication verbs. Use openssl to negotiate a secure tunnel first.

  1. Generate your Base64 authorization string:
    Postfix expects raw auth entries inside a Base64 block (\0username\0password). Generate it locally:
    echo -ne "\0youruser\0yourpassword" | base64
    # Output will resemble: AHlvdXJ1c2VyAHlvdXJwYXNzd29yZA==
  2. Launch the OpenSSL connection:
    openssl s_client -connect 127.0.0.1:587 -starttls smtp
  3. Run the SMTP handshake conversation:
    EHLO localhost
    AUTH PLAIN AHlvdXJ1c2VyAHlvdXJwYXNzd29yZA==
    Expected Response: 235 2.7.0 Authentication successful

Method B: Testing with SWAKS

SWAKS (Swiss Army Knife for SMTP) automates text stream handshakes, internal Base64 handling, and transport mutations automatically. Run this targeting your explicit submission port:

swaks --server 127.0.0.1 --port 587 \
      --to external-recipient@example.com \
      --from local-sender@yourdomain.com --auth PLAIN \
      --auth-user youruser --auth-password yourpassword

Note: Do not enter the Base64 format for username and password here as above. The -tls flag instructs Swaks to initialize STARTTLS seamlessly before passing string arguments.

How to flash Raspberry Pi

To flash a Raspberry Pi, download the official Raspberry Pi Imager and use it to write an operating system directly to your microSD card or USB drive. This process will erase all existing data on the drive.

Prerequisites

  • A microSD card (Class 10, minimum 8 GB recommended).
  • A microSD-to-USB or SD card adapter for your computer.
  • An internet connection to download the OS.

Step-by-Step Guide

Download the Raspberry Pi Imager for Windows, Mac, or Linux.

Meanwhile, in the CLI it’s simply done in a Linux terminal with:

sudo apt install rpi-imager

If you prefer Windows then you can open a Windows terminal:

winget install rpi-imager

Launch the Raspberry Pi Imager and select Device. Select the exact Raspberry Pi model you are using to ensure optimal compatibility..

Raspberry Pi Imager to flash OS

Click OS and select your preferred operating system (e.g., Raspberry Pi OS, Debian).

Imager Choose operating system

Insert your microSD card into your computer, click Storage and carefully select your microSD card from the list.

Customisation (Highly Recommended): Here, you can pre-configure the hostname, a username and password, Wi-Fi credentials, your time zone, and enable SSH for remote access, more in the next section.

Flash: Click Next or Write, confirm the warning that all data will be erased, and wait for the software to write and verify the image.

Boot Up: Once complete, safely eject your microSD card, insert it into your Raspberry Pi, and power it on.


Configuring Wi-Fi or SSH without a monitor (headless setup)

if you don’t have a monitor connected. To set up a headless Raspberry Pi (without a monitor, keyboard, or mouse), use the Raspberry Pi Imager to pre-configure your network and access settings before flashing.

1. Configure OS Customization

When you click Next or Write in the Raspberry Pi Imager, a prompt will ask if you want to apply OS customization settings. Click Edit Settings.

2. General Tab (Network & User)

  • Set Hostname: Give your Pi a unique name (e.g., raspi.local).
  • Set Username and Password: Create a secure custom username and password. The default “pi/raspberry” credentials no longer exist.
  • Configure Wireless LAN: Check this box to enter your exact Wi-Fi SSID (name) and Password. Set your Wireless LAN country code (e.g., DE, US, GB) to enable the Wi-Fi chip.

3. Services Tab (Remote Access)

  • Enable SSH: Check this box.
  • Authentication: Choose Use password authentication for the easiest setup.

4. Connect to Your Pi

  1. Flash and Boot: Save the settings, finish flashing, insert the card into the Pi, and power it on. Wait 2 minutes for the initial boot.
  2. Find the IP Address: Open your computer’s terminal or command prompt and type:
ping raspi.local

(Replace raspi with your chosen hostname. Note the IP address it returns). 3. Log In via SSH: Connect from your computer’s terminal using your configured username:

ssh username@raspi.local
  1. Enter Password: Type your password when prompted (no characters will show as you type) and press Enter.

Troubleshooting:

if the Pi does not connect to your Wi-Fi

If your Raspberry Pi fails to connect to Wi-Fi after a headless setup, the issue usually stems from typo errors, frequency band mismatches, or OS-specific network behaviors.

Follow these targeted troubleshooting steps to get your Pi online.

1. Verify Common Network Pitfalls

  • Check the Frequency Band (Pi Zero / Pi 3): The Raspberry Pi Zero W, Zero 2 W, and older Pi 3 models only support 2.4GHz Wi-Fi. If your home router combines 2.4GHz and 5GHz into a single network name (Smart Steering), the Pi may fail to connect. Temporarily split bands in your router settings or use a dedicated 2.4GHz guest network.
  • Double-Check for Typos: Wi-Fi network names (SSIDs) and passwords are strictly case-sensitive. Re-flash the SD card using the Raspberry Pi Imager to ensure no hidden spaces or wrong characters were entered.
  • Verify the Wi-Fi Country Code: Raspberry Pi OS disables the Wi-Fi chip by default until a valid Wi-Fi country code (like US, GB, or DE) is set. Make sure this was selected in the Imager’s advanced settings.
  • Check Encryption Types: Most Raspberry Pis struggle with modern WPA3 encryption. Ensure your router set to WPA2 Personal (AES).

2. Isolate the Issue Using a Mobile Hotspot

To determine if your home router is blocking the Pi, configure a secondary test network:

  1. Turn on the Mobile Hotspot feature on your smartphone.
  2. Set the hotspot frequency band to 2.4 GHz (if your phone allows it).
  3. Re-flash your SD card with the Raspberry Pi Imager, inputting your phone’s exact hotspot name and password.
  4. If the Pi successfully connects to your phone, your home router’s security configuration or band layout is preventing the connection.

3. Bypass Wi-Fi Temporarily (Ethernet / USB Gadget)

If the Wi-Fi refuse to connect, you can bypass wireless setup entirely to log in and diagnose the problem:

  • Use an Ethernet Cable: If your Pi model has an Ethernet port (like the Pi 4 or Pi 5), plug it directly into your router. It will bypass Wi-Fi and pull an IP address instantly. You can then log in SSH and manually fix the Wi-Fi config.
  • USB Gadget Mode (Pi Zero Only): You can power and data-link a Pi Zero directly to your computer using a regular micro-USB/USB-C data cable. By adding dtoverlay=dwc2 to the config.txt file on your flashed SD card, you can SSH over USB via ssh username@raspi.local.
  • Depending on your operating system version, this file is located at either /boot/firmware/config.txt (for newer versions like Raspberry Pi OS Bookworm) or /boot/config.txt (for older legacy systems).
sudo nano /boot/firmware/config.txt

(Note: If the file is empty, press Ctrl+X to exit and try sudo nano /boot/config.txt instead).

Scroll to the very bottom of the file and add the line:

dtoverlay=dwc2

Save and exit by pressing Ctrl+O, Enter, and then Ctrl+X, then reboot your Raspberry Pi.

Note: If an error occurs when trying to log in to the Raspberry Pi via SSH, you can use SSH with the verbose switch.

ssh -v username@192.168.1.1

Replace the placeholder IP with your Raspberry Pi’s actual IP address.

4. Find Raspberry Pi IP address in local subnet

This is where the execution of ping in a for-loop help us. This example send ping to all IPv4 addresses of a subnet from the command prompt. Run in a one-liner to find the IP addresses that are currently online.

for /l %i in (1,1,255) do @ping 192.168.1.%i -w 1 -n 1 | find /i "ttl="

5. Important Note for Recent OS Releases (Bookworm)

If you are using the latest Raspberry Pi OS (Bookworm), be aware that the legacy wpa_supplicant.conf trick no longer works. Network configurations are now handled strictly via NetworkManager. Do not manually create a wpa_supplicant.conf file on the boot partition, as the OS will completely ignore it. Always rely on the OS Customization settings inside the official Raspberry Pi flash imager. If you are still stuck, let me know.

Link Sources