Configure Postfix as mail forwarding on Ubuntu/Debian

Postfix is a popular mail transfer agent (MTA) used to handle email forwarding, among other tasks. To set up email forwarding using Postfix, you’ll need access to a Unix-like server where Postfix is installed and configured. Here are the steps to set up email forwarding using Postfix:

  1. Install Postfix (if not already installed):
    Ensure Postfix is installed on your server. You can install it using a package manager like apt on Ubuntu or yum on CentOS:

    # For Ubuntu/Debian
    sudo apt-get install postfix
    
    # For CentOS
    sudo yum install postfix
    
  2. Configure Postfix:
    Postfix’s configuration files are usually found in the /etc/postfix/ directory. You’ll need to modify the main.cf file to enable forwarding. Open the file in a text editor:

    sudo nano /etc/postfix/main.cf
    

    Add or modify the following lines to enable forwarding:

    virtual_alias_domains = example.com  # Replace with your domain
    virtual_alias_maps = hash:/etc/postfix/virtual
    

    Replace example.com with your actual domain name.

  3. Create a Virtual Alias Map:
    Create the /etc/postfix/virtual file to specify the forwarding rules. Each forwarding rule should be in the format:

    source@example.com    destination@example.com
    

    For example, to forward emails from user@example.com to forwarded@example.com, add this line to /etc/postfix/virtual:

    user@example.com    forwarded@example.com
    
  4. Generate the Virtual Alias Database:
    After adding your forwarding rules, you need to generate the database file:

    sudo postmap /etc/postfix/virtual
    
  5. Restart Postfix:
    Restart the Postfix service to apply the changes:

    sudo systemctl restart postfix
    
  6. Test the Forwarding:
    Send an email to the source email address (user@example.com in the example). It should be forwarded to the destination address (forwarded@example.com).

  7. Check with the postconf command that the domain aliases and alias file have been setup properly.

    $ postconf -n | grep virtual
    virtual_alias_domains = mydomain.com myanotherdomain.com
    virtual_alias_maps = hash:/etc/postfix/virtual
    root@localhost:~#
    

Remember to replace example.com, user@example.com, and forwarded@example.com with your actual domain and email addresses.

Additionally, make sure your server’s DNS records (MX and A/AAAA) are correctly set up to handle email for your domain. This setup assumes you have administrative access to the server and can modify Postfix configuration files.