Linux newbie admin - send me an email

It seems to me that everybody has used a 'nix clone somewhere for something at some point in time. It can be a server hosting web sites, an application server, a home media server, you can put these machines to work and forget about them. But can you really?

No. If it works, it breaks. And if it breaks you want to know about it. The simplest method known to man kind at the moment is to send an email when you want to be notified of an event. Simple enough. But is it really?

No. Using Ubuntu server, it comes with sendmail installed, and ready to use, but not configured. So having entered your email address (i.e. you@gmail.com) during setup just connects the root user to your email address, so the server will happily pump event notifications towards you, and fail miserably. It usually looks like this in your syslog:

Jun  2 22:08:53 Linux1 sm-mta[54485]: s4SNT6tV018625: to=<you@gmail.com>, ctladdr=<www-data@Linux1.server.a5.internal.cloudapp.net> (33/33), delay=4+20:39:47, xdelay=00:00:00, mailer=relay, pri=63213048, relay=gmail-smtp-msa.l.google.com., dsn=4.0.0, stat=Deferred: Connection reset by gmail-smtp-msa.l.google.com.

These messages keeep piling up and never get to where they're supposed to. It litters your logs, not to mention that your server tries to resend them periodically. Something has to be done about it, right?

In this example we'll be using a Gmail account (everybody has one, right?). Note thet Google Apps account with custom domain name can be used as well.

Just in case, if your distribution does not have everything in place, you can run the following command (as root, of course).

apt-get install sendmail mailutils sendmail-bin

We need to store the authentication data on the server but we don't want to make it too easy to steal. Luckily, sendmail uses a hashed database file, which hides the sensitive data from prying eyes of any 13-years old wannabe hackers peering over your shouders.

First we need to create a directory for the config database.

mkdir -m 700 /etc/mail/authinfo/

Next, using your favorite editor, create a file that will be the source for our database. In this case it's called auth, but it can be anything you like.

nano /etc/mail/authinfo/auth

And now for the juicy part, the contents of auth file:

AuthInfo: "U:root" "I:you@gmail.com" "P:password"

Having done that, let's hash the contents:

makemap hash /etc/mail/authinfo/auth < /etc/mail/authinfo/auth

You can remove the /etc/mail/authinfo/auth file after this. Now, find your sendmail.mc:

nano /etc/mail/sendmail.mc

and enter the following, just under the line that says MAILER_DEFINITIONS:

define(`SMART_HOST',`[smtp.gmail.com]')dnl
define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl
define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl
define(`confAUTH_OPTIONS', `A p')dnl
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
FEATURE(`authinfo',`hash -o /etc/mail/authinfo/auth.db')dnl

Save it and run the following lines:

make -C /etc/mail
/etc/init.d/sendmail reload

Now all that remains is to test the configuration:

echo "I told you it was going to be easy..." | mail -s "Your server wants your attention" you@gmail.com

And that's it.