Postfix Setup for Aliases/Forwarding Across Multiple Domains
Managing email for multiple domains on a single server, especially when you only need to forward or alias incoming mail to existing users or external addresses, is a common requirement. Postfix, a powerful Mail Transfer Agent (MTA), provides a robust mechanism for this using what it terms "virtual alias domains" and "virtual alias maps."
While the term "virtual" might suggest complex setups like virtual machines or dedicated mailboxes, in this context, it simply refers to Postfix's ability to handle email for domains that are not directly your server's primary hostname. This guide will walk you through configuring Postfix specifically for this alias and forwarding functionality.
Goal of This Configuration
Our objective is to configure a single Postfix instance to accept email for multiple distinct domains (e.g., domain1.com
, domain2.net
, domain3.org
) and then, based on the recipient's address, forward that email to either:
- A local system user on your server.
- An external email address.
- Another alias defined within your Postfix configuration.
This setup does not involve creating individual mailboxes on your server for each of the virtual email addresses.
Prerequisites
Before you begin, ensure the following:
- Debian-based server: This guide assumes you are running a Debian, Ubuntu, or similar Linux distribution.
- Root or
sudo
privileges: You'll need elevated permissions to modify system configuration files. - Postfix installed: If not, install it using
sudo apt update && sudo apt install postfix
. During installation, choose "Internet Site" and set your server's fully qualified domain name (FQDN), e.g.,mail.yourserver.com
. - Domains point to your server: Your domains (
domain1.com
,domain2.net
, etc.) must have their MX (Mail Exchanger) DNS records configured to point to your server's public IP address. We'll revisit this in Step 4. - Firewall configured: Ensure that TCP port 25 (SMTP) is open on your server's firewall to allow incoming mail.
Step-by-Step Configuration
Step 1: Configure Postfix's main.cf
The main.cf
file is the core configuration file for Postfix. You'll specify your server's identity and declare which domains Postfix will handle aliases for.
Open the file for editing:
sudo nano /etc/postfix/main.cf
Add or modify the following lines. Ensure that your actual domains are NOT listed in mydestination, as that would cause Postfix to attempt local delivery rather than using the alias maps.
# Basic server identity: # myhostname: The fully qualified domain name of your mail server. # This is often a subdomain, e.g., mail.yourserver.com. myhostname = mail.your_server_fqdn.com # mydomain: Your primary domain, derived from myhostname. mydomain = your_primary_domain.com # mydestination: Domains for which this server is the final delivery destination # for local UNIX users. Crucially, DO NOT list your virtual alias domains here. mydestination = $myhostname, localhost.$mydomain, localhost # virtual_alias_domains: Declare all domains for which Postfix will accept mail # and process it through the virtual_alias_maps. # List all domains you want to manage aliases for, separated by spaces. virtual_alias_domains = domain1.com domain2.net domain3.org # virtual_alias_maps: Specifies the file that contains your alias mappings. # 'hash:' indicates that Postfix will use a DBM database # derived from the plain text file. virtual_alias_maps = hash:/etc/postfix/virtual # Optional: Ensure no virtual mailbox related settings are active if you are # not using local mailboxes for virtual users. # # virtual_mailbox_domains = # # virtual_mailbox_maps = # # virtual_mailbox_base = # # virtual_uid_maps = # # virtual_gid_maps =
- myhostname: Replace mail.your_server_fqdn.com with your actual mail server's hostname (e.g., mail.yourdomain.com).
- mydomain: Replace your_primary_domain.com with your primary domain.
- virtual_alias_domains: Replace domain1.com domain2.net domain3.org with all the domains for which you want to create aliases/forwarding rules.
Save the file and exit the editor.
Step 2: Create the Virtual Alias Map File (/etc/postfix/virtual)
This file contains the specific rules for how email addresses within your virtual_alias_domains should be handled.
Create and open this file for editing:
sudo nano /etc/postfix/virtual
Add your alias entries. The format is original_email_address followed by one or more destination_address(es), separated by whitespace.
# Format: virtual_email_address destination_address(es) # --- Aliases for domain1.com --- info@domain1.com your_local_system_user_one # Deliver to a local UNIX user (e.g., 'ubuntu', 'admin') sales@domain1.com another_local_system_user # Deliver to a different local UNIX user support@domain1.com external_email@outlook.com # Forward to an external email address webmaster@domain1.com root, other_admin@external.com # Forward to multiple destinations (comma-separated) contact@domain1.com info@domain1.com # Forward to another virtual alias within Postfix # --- Aliases for domain2.net --- hello@domain2.net your_local_system_user_one # Can go to the same local system user admin@domain2.net external_admin@another-company.com # Forward externally feedback@domain2.net /dev/null # Silently discard incoming mail for this address press@domain2.net contact@domain1.com # Forward to an alias in another virtual domain # --- Catch-all aliases (use with caution, can attract a lot of spam) --- # Any email to domain1.com not explicitly listed above will go to catchall_for_d1@example.com @domain1.com catchall_for_d1@example.com # Any email to domain2.net not explicitly listed above will go to some_local_user_for_d2 @domain2.net some_local_user_for_d2
Destinations can be: * Local system user: A valid username on your server (e.g., root, ubuntu). Mail will be delivered to /var/mail/username. * External email address: A full email address outside your server. * Another virtual alias: An address defined elsewhere in this same virtual file. /dev/null: To discard incoming mail silently without bounce. Multiple destinations: Separate multiple destinations with a comma and space.
Save the file and exit.
Step 3: Apply Changes and Reload Postfix
After making changes to the virtual file, you must convert it into a database format that Postfix can quickly look up. Then, reload the Postfix service to apply the new configuration.
sudo postmap /etc/postfix/virtual
sudo systemctl reload postfix
Step 4: Configure DNS MX Records for Each Domain
This is a critical step that happens outside your server, at your domain registrar or DNS hosting provider. For other mail servers on the internet to know where to send mail for your domains, you must set up MX (Mail Exchanger) records.
For each of your domains (domain1.com, domain2.net, domain3.org), add an MX record pointing to your server's FQDN (the value you set in myhostname in main.cf).
Example DNS Configuration (at your domain registrar's panel):
# DNS for domain1.com: # Type Name Value Priority MX @ mail.your_server_fqdn.com. 10 # This tells mail servers to send mail to mail.your_server_fqdn.com A @ YOUR_SERVER_PUBLIC_IP # A record for the main domain A www YOUR_SERVER_PUBLIC_IP # A record for the www subdomain # DNS for domain2.net: # Type Name Value Priority MX @ mail.your_server_fqdn.com. 10 A @ YOUR_SERVER_PUBLIC_IP A www YOUR_SERVER_PUBLIC_IP # DNS for domain3.org: # Type Name Value Priority MX @ mail.your_server_fqdn.com. 10 A @ YOUR_SERVER_PUBLIC_IP A www YOUR_SERVER_PUBLIC_IP # Also, ensure your mail server's hostname resolves to your server's IP: A mail YOUR_SERVER_PUBLIC_IP
- Replace mail.your_server_fqdn.com with the actual FQDN of your mail server (e.g., mail.yourdomain.com).
- Replace YOUR_SERVER_PUBLIC_IP with your server's actual public IP address.
- DNS changes can take some time to propagate across the internet (up to 48 hours, though often faster).