Skip to main content

Detailed guide to installing and configuring Postfix on CentOS 9 for mail forwarding

Step 1: Installing Postfix

  1. System update Before installing new software, it is important to make sure that all system packages are updated to current versions. To do this, run the command:
    sudo dnf update -y
    sudo apt update
    This command will update all packages on your server.
  2. Installing Postfix Now that the system has been upgraded, you can install Postfix, the mail server that will process and forward email. To install it, perform:
    sudo dnf install postfix -y
    sudo apt install postfix -y
    The -y flag automatically confirms the setting without requesting further confirmation.
  3. Starting and enabling Postfix at boot After installation, you should start Postfix and configure it to start automatically at every system startup:
    sudo systemctl start postfix
    sudo systemctl enable postfix
    The first command will start Postfix immediately, and the second command will configure it to start automatically every time the server is switched on.

Step 2: Configuring Postfix

  1. Editing the configuration file Now you need to configure Postfix. To do this, open the main configuration file:

    sudo nano /etc/postfix/main.cf

    Here we will specify the basic parameters of the mail server operation.

  2. Configuring Basic Settings In the configuration file, add or modify the following lines:

    • myhostname: specifies the domain name of your mail server. Replace mail.example.com with your actual domain name.
    • myorigin: this parameter specifies the domain to be added to outgoing messages.
    • mydestination: lists the host names for which this server will accept mail.
    • relayhost: specify the ip where the e-mails will be sent to

    Example:

    myhostname = mail.example.com
    myorigin = /etc/mailname
    mydestination = $myhostname, localhost.$mydomain, localhost
    relayhost =
    • smtpd_client_restrictions: configure restrictions on receiving mail. Here you can specify from which IP addresses the server will accept messages.
    • sender_bcc_maps: this setting allows you to send copies of all outgoing emails to a specified address.
    • transport_maps: defines rules for forwarding mail to other servers.

    Example:

    smtpd_client_restrictions = permit_mynetworks, reject
    sender_bcc_maps = hash:/etc/postfix/sender_bcc
    transport_maps = hash:/etc/postfix/transport
    • mynetworks: lists the IP addresses and networks from which mail is allowed to be sent through this server.

    Example:

    mynetworks = 10.1.99.35/32, 127.0.0.0/8
  3. Configuring Forwarding to Exchange Server To forward all emails to the Exchange server, you need to create a transport rules file:

    sudo nano /etc/postfix/transport

    Add a line to this file:

    * smtp:[10.1.80.35]

    Then update the transport table:

    sudo postmap /etc/postfix/transport
  4. Configuring BCC for the sandbox To configure the sandbox to forward copies of all messages to the sandbox, create or edit a file:

    sudo nano /etc/postfix/sender_bcc

    Add the following line:

    @example.com bcc@10.1.72.11

    Update the BCC table:

    sudo postmap /etc/postfix/sender_bcc
  5. Restart Postfix To apply any changes you have made, you must restart Postfix:

    sudo systemctl restart postfix

Step 3: Check the setting

  1. Check the status of the Postfix service Verify that the Postfix service is running correctly:
    sudo systemctl status postfix
  2. Checking Postfix logs To diagnose possible problems, you can view Postfix logs:
    sudo tail -f /var/log/maillog