Before You Begin

This tutorial shows you how to install and set up the Postfix email server software on an Oracle Linux 8 system to enable you to send messages within your network.

Background

Postfix is a Mail Transfer Agent (MTA) server that was developed as a replacement for the sendmail server, the default MTA on many older Linux systems. Because of its modular pipeline-based architecture, Postfix is vesatile and integrates easily with many other services, such as spam and anti-virus processing, as well as with message store software, such as the Dovecot IMAP and POP server.

This tutorial describes how to set up and configure Postfix to function primarily as a Simple Mail Transfer Protocol (SMTP) server.

What Do You Need?

Any system with Oracle Linux 8 installed.

Set the server hostname

Set the full server domain and IP address in the /etc/hosts file, for example:

192.168.1.2   server1.example.com

You can also change your hostname by using the hostnamectl command:

$ sudo hostnamectl set-hostname server1.example.com

Note that if you change the system's hostname, you should revise the /etc/hosts file accordingly.


Install Postfix

  1. Install the postfix package.

    $ sudo dnf install postfix
  2. Open TCP port 25 for the firewall to enable emails to be sent over SMTP.

    $ sudo firewall-cmd --zone=public --add-service=smtp --permanent
    $ sudo firewall-cmd --reload
  3. If the sendmail package is installed on the system, remove it to prevent any interference with the normal operation of Postfix.

    $ sudo dnf remove sendmail
    $ sudo alternatives --set mta /usr/sbin/sendmail.postfix
  4. Enable and start the postfix service.

    $ sudo systemctl enable --now postfix

Configure Postfix

  1. Set your configuration in the /etc/postfix/main.cf file, for example:

    myhostname = server1.example.com
    mydomain = example.com
    myorigin = $mydomain
    inet_interfaces = all
    inet_protocols = all
    mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
    mynetworks = 192.168.1.0/24, 127.0.0.0/8
    home_mailbox = mail/
  2. Restart the postfix service.

    $ sudo systemctl restart postfix

Configure Postfix for STARTTLS

As a bare minimum to secure the service, configure Postfix to support STARTTLS to perform TLS/SSL verification and encryption over an SMTP connection. Using STARTTLS helps to protect the integrity of your communications.

Oracle strongly recommends using a TLS/SSL certificate that has been signed by an external Certficate Authority (CA). See https://docs.oracle.com/en/operating-systems/oracle-linux/certmanage/ for more information.

  1. If you already have a working SSL certificate, update your configuration in the /etc/postfix/main.cf file, for example:

    tls_random_source=dev:/dev/urandom
                                        
    # SMTPD TLS configuration for incoming connections
    smtpd_use_tls = yes
    smtpd_tls_cert_file = /etc/pki/tls/certs/mydomain.example.com.crt
    smtpd_tls_key_file = /etc/pki/tls/private/mydomain.example.com.key
    smtpd_tls_security_level = yes
    
    # SMTP TLS configuration for outgoing connections
    smtp_use_tls = yes
    smtp_tls_cert_file = /etc/pki/tls/certs/mydomain.example.com.crt
    smtp_tls_key_file = /etc/pki/tls/private/mydomain.example.com.key
    smtp_tls_security_level = yes
  2. Restart the postfix service to apply your changes:

    $ sudo systemctl restart postfix

Configure Postfix as a Mail Submission Agent

A Mail Submission Agent (MSA) accepts mail from a Mail User Agent (MUA) or an email client application. Although most client applications can use a standard SMTP service directly, many SMTP servers are configured to perform validation, verification, and other anti-spam functions on this interface. An MSA is usually configured to perform authentication and reject non-authenticated connections that are not originating from a permitted network. Therefore, MSAs are nearly always also configured to use Transport Layer Security (TLS) to protect communications between client and server. This separation of functions can limit the amount of intensive processing of email for authenticated clients, as well as control who is able to send mail from of the Simple Mail Transfer Protocol (SMTP) server.

In this task, a legacy SMTPS service is also configured to handle mail connections from much older MUAs. Configuring this interface is optional and most modern mail clients no longer use SMTPS. Also, note that the default Cyrus SASL library is used for authentication, as this library is already linked with Postfix. Many implementations prefer to configure the Dovecot SASL implementation because it offers more flexibility and also comes with IMAP and POP3 services.

  1. Install the Cyrus SASL authentication daemon.

    $ sudo dnf install cyrus-sasl
  2. Enable and start the SASL authentication service.

    $ sudo systemctl enable --now saslauthd
  3. Edit the /etc/postfix/main.cf file to configure some of the restriction parameters that you would apply for a typical MSA. Usually, this configuration would limit traffic to particular networks or force authentication. For example, add the following lines to the end of the file.

    smtpd_restriction_classes = mua_sender_restrictions, mua_client_restrictions, mua_helo_restrictions
    mua_client_restrictions = permit_sasl_authenticated, reject
    mua_sender_restrictions = permit_sasl_authenticated, reject
    mua_helo_restrictions = permit_mynetworks, reject_non_fqdn_hostname, reject_invalid_hostname, permit
  4. Edit the /etc/postfix/master.cf file to enable the smtps and submission services. The default configuration provides commented out entries for these services; it is sufficient to enable the lines for these services so that the entries read as follows:

    submission inet n       -       n       -       -       smtpd
      -o syslog_name=postfix/submission
      -o smtpd_tls_security_level=encrypt
      -o smtpd_sasl_auth_enable=yes
      -o smtpd_tls_auth_only=yes
      -o smtpd_reject_unlisted_recipient=no
      -o smtpd_client_restrictions=$mua_client_restrictions
      -o smtpd_helo_restrictions=$mua_helo_restrictions
      -o smtpd_sender_restrictions=$mua_sender_restrictions
      -o smtpd_recipient_restrictions=
      -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
      -o milter_macro_daemon_name=ORIGINATING
    smtps     inet  n       -       n       -       -       smtpd
      -o syslog_name=postfix/smtps
      -o smtpd_tls_wrappermode=yes
      -o smtpd_sasl_auth_enable=yes
      -o smtpd_reject_unlisted_recipient=no
      -o smtpd_client_restrictions=$mua_client_restrictions
      -o smtpd_helo_restrictions=$mua_helo_restrictions
      -o smtpd_sender_restrictions=$mua_sender_restrictions
      -o smtpd_recipient_restrictions=
      -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
      -o milter_macro_daemon_name=ORIGINATING
  5. Create additional firewall rules for SMTPS and SMTP-Submission. Note that SMTPS listens on TCP port 465, while SMTP-Submission listens on TCP port 587.

    $ sudo firewall-cmd --permanent --add-service=smtps
    $ sudo firewall-cmd --permanent --add-service=smtp-submission
    $ sudo firewall-cmd --reload
  6. Restart the postfix service to apply your changes.

    $ sudo systemctl restart postfix

Send emails with Postfix

  1. Install the mailx command-line email client.

    $ sudo dnf install mailx
  2. Send a test email to an administrator address.

    $ echo "External email" | mailx -r root@example.com -s "Test email subject" admin@example.com

You can now configure other services that are running on Oracle Linux 8 to send notification emails.


Troubleshoot Postfix

If Postfix emails are not being received, troubleshoot the service as follows:

$ sudo mailq
  • Check the log for errors in the Postfix service and the sending process:
  • $ sudo tail -f /var/log/maillog

    Want to Learn More?