OpenBSD Handbook

    Theme
    • Part I. Install & Configure
      • Introduction to OpenBSD
      • Installing OpenBSD
      • The X Window System
      • Networking on OpenBSD
      • System Configuration
      • OpenBSD Basics
      • Managing Software: Packages and Ports
    • Part II. Daily Operations
      • OpenBSD Desktop Environments
      • Multimedia
      • Printing
      • Linux Compatibility
      • Windows Compatibility
      • Games
    • Part III. System Administration
      • Virtualization
      • Storage and File Systems
      • Updating and Upgrading
      • Localization
      • The OpenBSD Boot Process
      • Security
    • Part IV. Networking & Daemons
      • Services
        • Database
          • MariaDB
          • PostgreSQL
          • Redis
          • memcached
        • Directory
          • YP (NIS)
          • LDAP
        • File
          • NFS
          • Samba
        • FTP Services
          • ftpd
          • ProFTPD
          • vsftpd
          • Pure-FTPd
          • TFTP
        • Mail
          • OpenSMTPD Mail Server
          • Dovecot IMAP and POP3 Server
          • Postfix
          • Exim
          • Rspamd
        • Name
          • Unbound DNS Resolver
          • NSD Authoritative DNS Server
          • BIND DNS Server
        • Networking
          • OpenBGPD
          • rad
          • DHCP on OpenBSD
          • slaacd
        • Web
          • Apache HTTP Server
          • nginx Web Server
          • OpenBSD httpd Web Server
          • relayd Reverse Proxy
        • Logging
          • syslogd
        • Monitoring
          • SNMP
        • Remote Access
          • Audit OpenSSH
          • OpenSSH Server
        • File Synchronization
          • rsync
        • Messaging
          • RabbitMQ Message Broker
        • Time
          • NTP Time Synchronization
        • Load Balancing
          • Build Redundant Load-Balanced Gateways
          • Load Balance Services with NAT or DSR
          • Build an HA HTTPS Load Balancer
      • PF
        • pfctl cheat sheet
        • PF Anchors
        • PF Filter Rules
        • PF Forwarding
        • PF Lists and Macros
        • PF Load Balancing
        • PF Logging
        • PF NAT
        • PF Options
        • PF Policies
        • PF Shortcuts
        • PF Tables
      • Advanced Networking
        • High Availability and State Replication
        • Multi-WAN and Policy-Based Routing
        • VPN and Cryptographic Tunneling
        • Classic and Lightweight Tunnels
        • IPv6 at Scale
        • QoS and Traffic Shaping
        • MPLS and Label Distribution
        • Network Services at Scale
        • Virtualization and Host Networking
        • Large-Scale L2 and L3 Design
        • Telemetry, Logging, and Flow Export
        • Hardening and Operational Safety
        • Reference Architectures
        • Troubleshooting Playbooks
      • Serial Communication
    • Part V. Miscellaneous
      • Virtualization Cheat Sheet
      • OpenBSD Cheatsheet
      • How-to Guides
        • Installation and First Boot
          • Complete the First 30 Minutes After Installation
          • Check Hardware Compatibility Before Installing
          • Bootstrap Wi-Fi Firmware Without Ethernet
          • Run OpenBSD as a Virtual Machine Guest
          • Install OpenBSD on a VPS or Cloud Server
          • Install OpenBSD with Full-Disk Encryption
          • Dual-Boot OpenBSD on a UEFI System
          • Automate OpenBSD Installation with Autoinstall and PXE
        • Desktops and Laptops
          • Set Up OpenBSD as a Laptop or Workstation
          • Configure Laptop Power Management
          • Troubleshoot Web Browsers on OpenBSD
          • Use Plan 9 Tools on OpenBSD
          • Build a Minimal Desktop with suckless Tools
          • Install Z shell (zsh)
        • Networking and Remote Access
          • Create an Administrator Account with doas and SSH Keys
          • Configure Secure Remote Access with OpenSSH
          • Use FIDO Security Keys with OpenSSH
          • Configure Wi-Fi Roaming and Wired Failover
          • Troubleshoot PF, NAT, and Routing
          • Route Multiple VLANs with PF
          • Configure a Road-Warrior WireGuard VPN
          • Route IPv6 Networks without NAT
          • Test OpenBGPD with Two Providers
          • Unlock an Internal Service with an SSH Key
          • Build a Simple Router and Firewall
        • Servers and Services
          • Set Up WordPress on OpenBSD
          • Build a Mail Server with OpenSMTPD, Dovecot, and Rspamd
        • Security, Maintenance and Recovery
          • Establish a Conservative Security Baseline
          • Recover Access and Reset the Root Password
          • Collect OpenBSD Diagnostic Evidence
          • Troubleshoot Package Installation and Update Failures
          • Schedule Recurring Maintenance
          • Monitor an OpenBSD System
          • Back Up and Restore an OpenBSD System
          • Configure softraid RAID1
          • Recover an OpenBSD System That Does Not Boot
        • Building, Automation and Virtual Machines
          • Manage OpenBSD with Ansible
          • Build Third-Party Software from Ports
          • Build OpenBSD from Source
          • Plan a Docker Workload Migration to OpenBSD
      • OpenBSD for Linux Users
      • OpenBSD for FreeBSD Users
      • OpenBSD for macOS Users
    • OpenBSD FAQ
    • OpenBSD Architectures
      • OpenBSD Package Search
        • Synopsis
        • Features
        • Enabling smtpd
        • Basic Configuration
        • Accepting Mail from the Internet
        • Outgoing Mail via Smarthost
        • Virtual Domains and Users
          • Define Table for Virtual Mappings
          • Update smtpd.conf
        • Filtering Mail
          • Greylisting
          • Blocking Specific Senders or Addresses
          • External Filters
        • Local Delivery Options
        • Administrative Commands
        • Logging and Troubleshooting
        • Summary of Key Configuration Files

        OpenSMTPD Mail Server

        Synopsis #

        OpenSMTPD is the default mail transfer agent (MTA) in OpenBSD. Its smtpd(8) daemon handles local delivery, outgoing relays, authenticated submission, and mail for virtual domains. The examples below configure these roles, TLS, and separately packaged mail filters.

        Features #

        • Secure by default, with privilege separation and sane defaults
        • Local delivery to system users
        • Relay via authenticated submission or smarthost
        • Full IPv6 and TLS support
        • Filtering and address rewriting
        • Virtual user/domain support
        • Integration with filter-* plugins and extras packages

        Enabling smtpd #

        To enable and start the smtpd service at boot:

        doas rcctl enable smtpd
        doas rcctl start smtpd
        

        The daemon will read its configuration from /etc/mail/smtpd.conf.

        Basic Configuration #

        A minimal smtpd.conf(5) file enabling local delivery and outgoing mail might look like this:

        listen on lo0
        
        action "local" mbox
        action "outbound" relay
        
        match from local for local action "local"
        match from local for any action "outbound"
        

        This configuration:

        • Listens only on the loopback interface
        • Defines one action for local mailbox delivery and one action for outbound relay
        • Allows local users to send mail to other local users
        • Relays outbound mail from local users to other systems using DNS

        To apply changes:

        doas smtpd -n && doas rcctl restart smtpd
        

        Accepting Mail from the Internet #

        To receive mail from the outside world:

        pki example.org cert "/etc/mail/certs/example.org.crt"
        pki example.org key "/etc/mail/certs/example.org.key"
        
        listen on egress tls pki example.org
        
        action "inbound" maildir
        match from any for domain example.org action "inbound"
        

        This example:

        • Defines the certificate and key used by the listener
        • Listens on the external interface with TLS enabled
        • Accepts mail from the internet for the domain example.org
        • Delivers it to ~/Maildir in the recipient’s home directory

        To use TLS, a valid certificate must be placed under /etc/mail/certs/:

        doas mkdir -p /etc/mail/certs
        doas cp fullchain.pem /etc/mail/certs/example.org.crt
        doas cp privkey.pem /etc/mail/certs/example.org.key
        

        Ensure permissions are correct:

        doas sh -c 'chown root:_smtpd /etc/mail/certs/*'
        doas sh -c 'chmod 640 /etc/mail/certs/*'
        

        Then define the certificate using the pki block in smtpd.conf:

        pki example.org cert "/etc/mail/certs/example.org.crt"
        pki example.org key "/etc/mail/certs/example.org.key"
        

        Outgoing Mail via Smarthost #

        To relay all outgoing mail through a remote mail server (e.g., ISP or upstream relay):

        table secrets file:/etc/mail/secrets
        action "relay" relay host smtp+tls://upstream@smtp.example.net:587 auth <secrets>
        match from local for any action "relay"
        

        Define the authentication secret in /etc/mail/secrets:

        upstream username:replace-with-provider-password
        

        Secure the file:

        doas chmod 600 /etc/mail/secrets
        doas chown root:_smtpd /etc/mail/secrets
        

        Virtual Domains and Users #

        To receive mail for domains not mapped to system users, use virtual aliases:

        Define Table for Virtual Mappings #

        Create /etc/mail/virtuals:

        info@example.org      alice
        admin@example.org     bob
        contact@example.org   contactuser
        

        Use this text file directly as a file: table. The destinations must be existing local accounts.

        Update smtpd.conf #

        table aliases file:/etc/mail/virtuals
        
        action "virtuals" mbox virtual <aliases>
        match from any for domain example.org action "virtuals"
        

        Filtering Mail #

        Use filter blocks to reject selected senders or attach an external mail filter.

        Greylisting #

        Greylisting temporarily rejects messages and accepts later retries. Configure an external filter that tracks these retries, following its package documentation, then attach it to the listener.

        Blocking Specific Senders or Addresses #

        Create a table:

        table badsenders file:/etc/mail/badsenders.txt
        

        With contents:

        spammer@example.com
        junk@example.net
        

        Then:

        filter "blocklist" phase mail-from match mail-from <badsenders> reject "550 Sender blocked"
        listen on egress filter "blocklist"
        

        External Filters #

        Install the individual integration required by the deployment. For Rspamd:

        doas pkg_add opensmtpd-filter-rspamd
        

        Follow its installed package README and the Rspamd chapter . Filter declarations must precede the listener that uses them. Merge listener options such as TLS and filtering into one listener definition; the fragments above are alternatives, not a complete combined mail-server configuration.

        Local Delivery Options #

        OpenSMTPD can deliver to either mbox (traditional mailbox format) or maildir (per-message file format). Set the method in an action and select it from a match rule:

        action "inbound" maildir
        match from any for domain example.org action "inbound"
        

        The default Maildir is ~/Maildir; mbox delivery uses /var/mail/user. Verify recipient accounts, directory permissions, and actual delivery before accepting external mail.

        Administrative Commands #

        Use smtpctl(8) to manage the mail system:

        doas smtpctl show stats
        

        Show statistics for messages processed, sessions handled, and more.

        doas smtpctl show queue
        

        List the mail queue.

        doas smtpctl schedule all
        

        Immediately attempt to send all queued messages.

        doas smtpctl remove <msg-id>
        

        Remove a specific message from the queue.

        doas smtpctl log verbose
        

        Enable verbose logging for debugging.

        Logging and Troubleshooting #

        Logs are written to /var/log/maillog. Tail the log for real-time updates:

        doas tail -f /var/log/maillog
        

        For additional diagnostics:

        doas rcctl stop smtpd
        doas smtpd -dv
        

        Runs smtpd in the foreground with verbose debugging after stopping the regular service. When finished, exit the debug process and use doas rcctl start smtpd.

        Summary of Key Configuration Files #

        FilePurpose
        /etc/mail/smtpd.confMain configuration file for smtpd
        /etc/mail/secretsAuthentication credentials for relay hosts
        /etc/mail/virtualsVirtual user mappings (if used)
        /etc/mail/certs/TLS certificate and key storage
        /etc/mail/aliasesLocal user aliases (via newaliases)

        All changes to smtpd.conf or related tables require validation and a restart:

        doas smtpd -n && doas rcctl restart smtpd
        
        Report a bug
        • Synopsis
        • Features
        • Enabling smtpd
        • Basic Configuration
        • Accepting Mail from the Internet
        • Outgoing Mail via Smarthost
        • Virtual Domains and Users
          • Define Table for Virtual Mappings
          • Update smtpd.conf
        • Filtering Mail
          • Greylisting
          • Blocking Specific Senders or Addresses
          • External Filters
        • Local Delivery Options
        • Administrative Commands
        • Logging and Troubleshooting
        • Summary of Key Configuration Files