Code

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:

vi /etc/mail/local-host-names
example.com
example2.com

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).

vi /etc/mail/virtusertable
robert@example.com	robert
sales@example.com	robert
carl@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:

vi /home/robert/.forward
forward_1@inkplant.com
forward_2@inkplant.com

Editing the aliases file instead is simpler since you can edit all the users in one spot instead of having to find different files:

vi /etc/aliases
#forwarding account(s)
carl: forward_3@inkplant.com
robert: forward_1@inkplant.com,forward_2@inkplant.com

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 robert@example.com now, it will forward to forward_1@inkplant.com and forward_2@inkplant.com. And, if you send to carl@example.com, it will go to forward_3@inkplant.com. Also, anything delivered to example2.com (sales@example2.com, test@example2.com, etc.) will also go to carl's forward_3@inkplant.com.



2 Comments

squarecandy
January 17, 2013, 12:25 pm
With this setup do the messages still get delivered to the mailbox on the server? They will pile up if the user is only checking the address they forward to. How can we make sure they are only being forwarded (no inbox)?
Alex G.
September 25, 2013, 12:02 pm
Thank you very much
It saved my day!

Leave a Comment

Name
Email
Website
Comment
Name and email are required. Your email will not be published.

This post was published on 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.

Copyright © 2014, Ink Plant. All rights reserved.