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

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:

vi /etc/mail/virtusertable	robert	robert	carl	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

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)

The final step is to update the aliases and then restart your mail program:

service sendmail restart

That's it. If you send a test message to now, it will forward to and And, if you send to, it will go to Also, anything delivered to (,, etc.) will also go to carl's



This post was published on May 12th, 2011 by Robert James Reese in the following categories: Email, Linux, and VI Editor Before using any of the code or other content in this post, you must read and agree to our terms of use.