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.
source from http://www.nixtutor.com/

No comments :

Post a Comment