SEARCH

How-To Geek

How To Setup Email Alerts on Linux Using Gmail or SMTP


Linux machines may require administrative intervention in countless ways, but without manually logging into them how would you know about it? Here’s how to setup emails to get notified when your machines want some tender love and attention.

Of course, this technique is meant for real servers, but if you’ve got a Linux box sitting in your house acting as a home server, you can use it there as well. In fact, since many home ISPs block regular outbound email, you might find this technique a great way to ensure you still get administration emails, even from your home servers.

Overview

Configuring the Email sending ability for a machine immediately gives us the upshot that a lot of the system’s administration problems and critical alerts will be automatically sent to that system’s designated administrator. The most noteworthy example is that cron job execution errors fall into this category and therefore will notify the admin that there was a problem with scheduled jobs such as backups as soon as they happen including the standard error (stderr) output in the report. We will also be able to incorporate emailing from bash scripts.

Don’t worry—it usually doesn’t spam you with too much information and you can just de-configure it if it becomes to annoying.

On Debian based systems, other then a lot of very versatile emailing related programs there is also the ssmtp package, which is well suited if all you want to do is have an agent to send emails using another email server (MTA), without having to setup an entire postfix or sendmail configuration to do so.

Prerequisites and assumptions

Before we get started, you’ll want to make sure that all of these conditions are met:

  • You have an SMTP server that can receive the emails from your machines and send them to the recipient (i.e. your corporate exchange or Gmail).
  • You have the credentials for a user that is able to send Email on that server (i.e. a mailbox or a Gmail account).
  • This procedure has been used and tested on *Debian based systems (Lenny, Ubuntu & Mint), so if your not on one of those distributions your mileage may vary.
  • You will see me use VIM as the editor program, this is just because I’m used to it… you may use any other editor that you’d like.

*It is also used and tested on DD-WRT, but will only be covered in a future article (update: isn’t the future fun?) due to the setup and configuration variations necessary for such an embedded platform.

Setup

To install the ssmtp (Simple S.M.T.P) package, use the following command:

sudo aptitude install ssmtp

Then edit the configuration file:

sudo vim /etc/ssmtp/ssmtp.conf

Adjust and add as necessary the  following parameters:

  • root=username@gmail.com

    Change it from postmaster to the machines admin’s Email.

  • mailhub=smtp.gmail.com:587

    Your mail server in our case this is Gmail so we have to specify the port as 587, for regular SMTP servers this is usually not necessary.

  • hostname=username@gmail.com

    Usually the name of the machine is automatically filled by the package setup, if the machine has a mailbox this should be fine, but if it doesn’t or the name is not the same as the mailbox adjust accordingly.

  • UseSTARTTLS=YES

    Enable TLS for secure session communication.

  • AuthUser=username

    The username of the sending mailbox.

  • AuthPass=password

    The password of the sending mailbox..

  • FromLineOverride=yes

    Sends the hostname instead of root[root@hostname.FQDN].

In order to make the default (root) “from” field be the server name, edit the /etc/ssmtp/revaliases file:

sudo vim /etc/ssmtp/revaliases

And add into it the desired translation which in our Gmail examples case will be:

root:machine-name@some-domain.com:smtp.gmail.com

Incredibly this is all you have to do to enable the ability. From now on, the machine will Email you when something is up.

Confirming setup

Lets test that our ssmtp setup was correct by sending an Email:


echo "Test message from Linux server using ssmtp" | sudo ssmtp -vvv your-email@some-domain.com

The “-vvv” turns on verbosity output so don’t get alarmed… this is just in case you encounter any problems, you will have some sort of output to Google for.
If all goes well, you should be getting the Email in a couple of seconds.

We will show scripted examples of this setup in future articles.


May your Emails be of the non-bouncing variety :)

 

Aviad Raviv, is an agile IT implementer that has reached the level of "Bankai" and is now working on incorporating the "hollow" side.

  • Published 12/6/10

Comments (11)

  1. Slava

    Thank you, this is very useful article.

  2. dMk

    and, of course, you can use any(?) other mail service, not only gmail.

  3. Aviad (a.k.a Hotfortech)

    @dMk that’s exactly right… the Email server i used originally was a hosted exchange server with Mailstreet.

  4. Chris

    Works for the most part, except for the alias. Unable to make the “default (root) “from” field be the server name” and followed the instructions carefully.

  5. Nemesio

    Love the simplicity. Great article

    Can hardly wait for your scripts

    Thank you

  6. Aviad (a.k.a Hotfortech)

    @Nemesio
    your welcomed :)

  7. Aviad (a.k.a Hotfortech)

    @Chris
    ya.. that sometimes needs some tweaking :
    if anyone’s got the magic bullet i’d love to know about it.

  8. Thomas Kizito

    Thanks,
    It works like charm. I don’t know why every after running a command on ma command line, I have been presented with a ‘You have a new mail in /var/root’ message which was really nagging, being a cron job failure msg.
    With this now I have some where I have pushed them and it is doing wonders for me.
    The only problem is that in an event when I loose an Internet connection, I shall not know what to do because I might now be presented with another error msg.
    Let me pray things go on well. !!!!

  9. mzbcracker

    sorry.but why don’t you (the whole sysadmingeek team) update the site regularly to get them by mail subscribtion or …..like what how-to geek does.they are much more activer than this site.i like this site thats why i suggest this

  10. Learn how

    +1 for new stuff

  11. DynCoch

    Thank you! =)

Enter Your Email Here to Get Access for Free:

Go check your email!