How to Configure Email Forwarding from a Linux Server to External Address (e.g., Gmail) Using Postfix


1 views

When setting up email forwarding on a Linux server, Postfix is the most common MTA (Mail Transfer Agent) used for this purpose. The process involves modifying Postfix configuration to redirect incoming emails to an external address without local mailbox storage.

  • Ubuntu/Debian server with root access
  • Postfix installed (default in most Ubuntu installations)
  • Valid domain with properly configured MX records

First, let's install Postfix if it's not already present:

sudo apt-get update
sudo apt-get install postfix

During installation, select "Internet Site" when prompted, then enter your domain name.

The most reliable method is using virtual alias maps. Create or edit the virtual aliases file:

sudo nano /etc/postfix/virtual

Add your forwarding rule (one per line):

emailaddress@mydomain.com otheremail@gmail.com
@subdomain.mydomain.com another@gmail.com

Then compile the virtual aliases database:

sudo postmap /etc/postfix/virtual

Edit the main Postfix configuration file:

sudo nano /etc/postfix/main.cf

Add or modify these lines:

virtual_alias_domains = mydomain.com, subdomain.mydomain.com
virtual_alias_maps = hash:/etc/postfix/virtual

After making changes, restart Postfix:

sudo systemctl restart postfix

Test your configuration by sending an email to your domain address. Check mail logs for errors:

sudo tail -f /var/log/mail.log

To forward all emails for non-specified addresses to a single destination:

@mydomain.com catchall@gmail.com

Remember to update and compile the virtual file again and reload Postfix.

  • Regularly monitor your mail logs for suspicious activity
  • Consider implementing SPF, DKIM, and DMARC records
  • For production environments, add rate limiting

When setting up email forwarding on a Linux server, we're essentially creating a mail transfer agent (MTA) configuration that redirects incoming emails from a domain address to an external mailbox. For this tutorial, we'll use Postfix - the most common MTA for Linux systems.

Before proceeding, ensure you have:

- Ubuntu Server (10.04 or later)
- Root access
- A registered domain with proper MX records
- Basic terminal proficiency

First, install Postfix and required packages:

sudo apt-get update
sudo apt-get install postfix mailutils

During installation, select "Internet Site" when prompted, then enter your domain name (mydomain.com).

Edit the main Postfix configuration file:

sudo nano /etc/postfix/main.cf

Ensure these lines exist or add them:

myhostname = mail.mydomain.com
mydomain = mydomain.com
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost
relayhost = 
mynetworks = 127.0.0.0/8
local_recipient_maps =

Create a virtual aliases file to handle forwarding:

sudo nano /etc/postfix/virtual

Add your forwarding rule (one per line):

emailaddress@mydomain.com otheremail@gmail.com

Then create the virtual aliases database:

sudo postmap /etc/postfix/virtual

Edit main.cf again to enable virtual aliases:

sudo nano /etc/postfix/main.cf

Add these lines at the bottom:

virtual_alias_domains = mydomain.com
virtual_alias_maps = hash:/etc/postfix/virtual

Restart Postfix and test your configuration:

sudo service postfix restart
echo "Test email body" | mail -s "Test Subject" emailaddress@mydomain.com

Check your Gmail inbox for the forwarded message.

If emails aren't forwarding, check these logs:

tail -f /var/log/mail.log
postconf -n

Common issues include incorrect file permissions or missing Postfix components.

For simple cases, you can create a .forward file in the user's home directory:

echo "otheremail@gmail.com" > ~/.forward
chmod 644 ~/.forward

This method doesn't require Postfix virtual aliases but is less flexible for domain-wide forwarding.