6

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?

Zoredache
  • 130,897
  • 41
  • 276
  • 420
Dan Esparza
  • 226
  • 2
  • 4
  • 13

3 Answers3

6
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

O G
  • 874
  • 4
  • 6
  • 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
  • Correct me if I'm wrong, but this has the effect of setting the line root: someuser@gmail.com in the /etc/aliases file and then running 'newaliases'. – Dan Esparza Aug 16 '11 at 15:18
  • 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](http://en.wikipedia.org/wiki/Mail_delivery_agent) which can only deliver to local mailboxes. Or a "real" [MTA](http://en.wikipedia.org/wiki/Message_transfer_agent) 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
3

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
2

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

Aaron Copley
  • 12,525
  • 5
  • 47
  • 68