Linux: Send email to another address instead of /var/spool/mail/root
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 [email protected]) instead?
sed s/^root.*/root:\ [email protected]/ -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: [email protected] to have root's e-mails automatically forwarded to that address. For more information on the /etc/aliases file, man aliases
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.
Another way is for forward root's mail.
echo "[email protected]" > /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