How to Forward Email on a Linux Server
This a compilation of notes that I keep around for when I need to manage email delivery through my Linux box. Depending on your install, they might or might not work for you, but hopefully they'll at least provide a good starting point.
First things first, you need to create a user (if you haven't done so already) for the email address:
/usr/sbin/useradd robert passwd robert
If you haven't already set up this domain to send emails through your server, you'll need to do that now. Point your MX Record for the domain to your server. You'll do this with your registrar: (GoDaddy, eNom, etc.) That change sometimes takes a few hours to propogate so you might want to do that step first. Understand that this will change where mail for your domain is delivered to – be careful if you already have email addresses set up on the domain! Changing this will likely cause them to stop working.
That got the mail traffic server, now you need to let your server know to expect mail from the new domain. A list of domain names can be found in a file called local-host-names (its location can vary). It contains all the email domains handled by your server, simply separated by line breaks. So, just add a new line with your domain:
Now, you need to tie together incoming email addresses with specific user accounts. To do this, you'll edit the virtusertable file. It is simply a text file with each incoming email address, followed by a tab, and then the username. To create a catch-all account, just include the at symbol and the domain (for example: @example.com).
email@example.com robert firstname.lastname@example.org robert email@example.com carl @example2.com carl
To forward all of a user's incoming email to a different email address (like Gmail, etc.), you can either create a .forward file in their home directory (that includes one email address to deliver to per line) or you can instead add a line to the bottom your aliases with the username followed by a colon, space, and the desired forwarding address. If you want to forward to multiple addresses, you can – just separate them with commas. Don't include spaces! They will keep it from working.
Here's an example of the .forward file method:
Editing the aliases file instead is simpler since you can edit all the users in one spot instead of having to find different files:
#forwarding account(s) carl: firstname.lastname@example.org robert: email@example.com,firstname.lastname@example.org
The final step is to update the aliases and then restart your mail program:
/usr/bin/newaliases service sendmail restart
That's it. If you send a test message to email@example.com now, it will forward to firstname.lastname@example.org and email@example.com. And, if you send to firstname.lastname@example.org, it will go to email@example.com. Also, anything delivered to example2.com (firstname.lastname@example.org, email@example.com, etc.) will also go to carl's firstname.lastname@example.org.
This post was published on Thursday, May 12th, 2011 by Robert James Reese in the following categories: Email, Linux, VI Editor. Before using any of the code or other content in this post, you must read and agree to our Terms & Conditions.