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:

1.   /usr/sbin/useradd robert
2.   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:

1.   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:

1.   vi /etc/mail/virtusertable
1.	robert
2.	robert
3.	carl
4.	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:

1.   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:

1.   vi /etc/aliases
1.   #forwarding account(s)
2.   carl:
3.   robert:,

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

1.   /usr/bin/newaliases
2.   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


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!
June 19, 2016, 2:46 pm
I cannot seem to get this to work. Where does the newaliases file come from? I have a newgrp file. The error message is `Returned mail: see transcript for details`

Leave a 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, and VI Editor. Before using any of the code or other content in this post, you must read and agree to our Terms & Conditions.