I manage a micro linux server on Amazon EC2. Occasionally I get email sent to the root account and if I'm logged in, I'll get the notice:

You have new mail in /var/spool/mail/root

How do I get these emails sent to another email address (like user@gmail.com) instead?

sed s/^root.*/root:\ someuser@gmail.com/ -i /etc/aliases && newaliases

Run as root. Will alias the delivery address for root to the email address of your choice. Otherwise, manually edit /etc/aliases and modify the root's alias in the form of: root: someuser@some.mail.host to have root's e-mails automatically forwarded to that address. For more information on the /etc/aliases file, man aliases

  • 1
    And you need an MTA which is able to send mail to "the outside world". This does not work with only a MDA. – mailq Aug 16 '11 at 15:09
  • 1
    @mailq: Please elaborate – Dan Esparza Aug 16 '11 at 15:13
  • 3
    @Dan That is correct. \@OG: One should be nice provide clear instructions rather than shell golf scripts. – Jeff Ferland Aug 16 '11 at 15:24
  • 1
    On a stripped down server one could have only a MDA which can only deliver to local mailboxes. Or a "real" MTA which acts as SMTP client, SMTP server, MDA and maybe even more. – mailq Aug 16 '11 at 15:24
  • 1
    @Jeff: thanks for the comment, I've edited the answer. The previous one was posted in a bit of a rush but required very basic regexp knowledge to figure out and Dan managed to figure it out correctly, so no 'macchiavellic' intention was meant. – O G Aug 16 '11 at 16:43

The difference between having your mail address in .forward and in /etc/aliases is that, if you use .forward, the mail will first land in your local mailbox (possibly /var/spool/mail/root by default) and then a copy will get forwarded to you. In the /etc/aliases way, however, it will get sent directly to you, without any local delivery. You might want to consider this while choosing the method, whether you want a local copy to remain on server or not.

  • This is of course valid in case you have a proper MTA, otherwise, the other answers are more relevant as /etc/aliasas wont work for remote delivery. – Tuncay Göncüoğlu Nov 30 '12 at 18:08

Another way is for forward root's mail.

echo "youraddress@domain.com" > /root/.forward

You'll need to make sure that sendmail/postfix is installed and running and that your host can connect to the SMTP servers for the receiving domain. Otherwise, you'll need to configure some host as a "smart host" to relay all of your mail.

To get the SMTP servers: nslookup -type=mx domain.com

To test connectivity: telnet <hostname> 25 or nc -z <hostname> 25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.