18

I would like to forward the emails received by root to an external email on an Ubuntu node. I have seen this post, but it does not explain much about the procedure to follow. There are some other posts available online, but they are often incomplete or unclear.

Does anyone have a complete procedure to share? Should a mailserver be installed on my node? If yes, which one? What are the configurations steps on the node? I am working strictly with command line (the node is a server).

Jérôme Verstrynge
  • 4,747
  • 7
  • 23
  • 34

4 Answers4

19

If root is receiving email, then you probably already have a server installed.

$ sudo dpkg-reconfigure postfix

If this gets an error,

$ sudo apt-get install postfix

(Re)configure to either deliver directly or use a smarthost. Now add "root: user@example.com" (with the correct address, of course) to /etc/aliases and run newaliases. (I think the Ubuntu postfix package offers to do this automatically during dpkg-reconfigure).

geekosaur
  • 7,025
  • 1
  • 19
  • 19
13

You'll need a mail server installed on your node, yes. Postfix, exim, and sendmail are my preferences (in that order.)

Note that some mail servers are a bit picky about who they accept mail from. If it's your local mail server, the restrictions are often done by network so you might not have a problem. If it's gmail, for example, you'll have to make sure your domain you send as (/etc/mailname in postfix) matches the IP you send from. Check out DynDNS or other similar services if you need to get a domain which matches your IP (if you don't already have one.)

Check instructions online for relaying to other mail providers.

You can then do one of the following (as root) to get mail sent to root@localhost sent to your external email address:

echo "you@yourmail.com" > /root/.forward

or

echo "root: you@yourmail.com" >> /etc/aliases && newaliases
Cakemox
  • 24,141
  • 6
  • 41
  • 67
2

You also have to figure out if you can get out, or if your ISP does port 25 blocking. I made the below script to setup Ubuntu for smarthost email forwarding with username and password. So it will forward the root email to the ISPs SMTP server and not go direct.

apt-get install mailutils

P=/etc/postfix/password

echo "smtp.mailserver.com         username:password" >> $P
chown root:root $P
chmod 0600 $P
postmap hash:$P

echo "
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/password
smtp_sasl_security_options =
" >> /etc/postfix/main.cf


echo "
root:   emailtoforward2@domain.com" >> /etc/aliases

newaliases
/etc/init.d/postfix reload

echo "$HOSTNAME Email Ready" | mail -s 'Email test' root

tail /var/log/mail.log
Porch
  • 680
  • 5
  • 12
2

Another alternative would be to use ssmtp (a small send-only mail "server"). A complete howto is given in the official Ubuntu documentation.

stefanct
  • 131
  • 2