Using Monit to get email alert on unauthorized login
TL;DR
For a long time, we had our own custom written perl script to alert us whenever someone logged into our production servers from an ip address we do not recognize (not whitelisted). The script looked somewhat like this…
All we needed to do was to run this script in the background as a daemon, and it would send us an email alert whenever someone logged in successfully. As root user start the script like this:
# (perl alert_on_login.pl /var/log/auth.log &)
Ever since we started using monit for the usual purpose (monitoring processes), we have also entrusted monit to do the job of the above perl script. Monit makes this super simple…
Monit is a popular opensource process monitoring tool. It is used mostly for monitoring health of any linux process and take necessary action if any of the set parameters are breached. Monit can restart a process if the process failed for some reason. Monit can also notify you of incidents and actions taken.
See this to learn more about monit’s alert capabilities.
Monit’s global configuration file is usually /etc/monit/monitrc. Here is what monit needs to be told about how to send email alerts:
And then we add this config file ssh_logins.conf
specific to sshd related stuff:
Notice how we tell monit to ignore logins from known ip addresses. We can now store all whitelist ip addresses in a separate file /etc/monit/whitelist_ips.regex
, one address per line.
Note: We have disabled password based login and hence do not monitor for passworded logins. If you use passworded login, you should change "Accepted publickey"
to "Accepted password"
Happy monitoring!