How to get an email notification when someone logs into SSH

One neat trick, is to receive an email when someone logs into your VPS via SSH. Generally, only few people should have SSH access to the VPS. The rest should access your website / email / control panels /etc as needed.

To get an email when someone logs into SSH, do the following:

 

  1. Login to your VPS via SSH.
  2. Edit /etc/profile  in your favourate editor. vi is the most common Linux / UNIX editor.
  3. Navigate to the bottom of the file, and add the following
  4. Add the following: 
    echo 'SSH (localhost) on:' `date` `who` | mail -s "Alert: Access from `who | cut -d"(" -f2 | cut -d")" -f1`" you@yourdomain
  5. Now login to your VPS again, and you'll receive an email :)

 

P.S. Change you@yourdomain to your own email address

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How can I set a nice looking bash prompt on my Linux VPS?

Add the following lines to the end of /etc/profile PS1="\[\e]2;\u@\H...

How do I change my VPS's hostname

If you want to change your Virtual Private Server's hostname, you first need to login to your VPS...

What is a Virtual Private Server?

A Virtual Private Server, or VPS is simply put, a "slice" of a dedicated server. Dedicated...

How can I secure my VPS?

If you own, or run a Virtual Private Server, or Dedicated server on the internet, then you need...

how do I disable root logins?

Disabling direct root login to your VPS is almost a must!...