Redirect Email Using Postfix

The other day my mentor taught me a nifty trick for re-routing email using Postfix from a public facing Linux Server (we use Ubuntu Server LTS), as long as you have control over the DNS for the domain’s email you want to re-route.  This can be used for a number of reasons, but in our case we just use it to verify our domains for SSL Certs.

Since DNS most recently came up, let’s start there.  All you have to do here is login to whoever is controlling the DNS for the domain you wish to re-route (GoDaddy, AWS, CloudFlare, etc.) and point the MX record to your Postfix public ip/domain name.  Simple, now let’s move on to the Postfix box.

I’m just going to assume that you already have the Linux box all set up correctly with Postfix installed for the sake of my fingers.  The initial Postfix install prompts you for a couple pieces of information, but they are pretty self explanatory.  One thing I will mention is that you will need to add an allow rule to the ufw firewall if you are using that to lock everything down.  You can do this by running the following:

Now that that is handled we can dig into the meat of this operation.  First navigate to /etc/postfix/ directory and edit the main.cf file found there.  Make the following changes to the file:

Notice that we pointed the alias maps to a file called virtual.  A quick ls will reveal that does not yet exist, so let’s make it!

Next we run the following commands to set up the lookup and restart the postfix service:

Notice this creates a virtual.db file in the same directory as virtual.  Anywho, with that we are ready to rock and roll!  If you want to see the traffic move through your postfix box you can run the following command to see the traffic as it comes and goes: