Enable SSH Login Notification on Linux

Is your Linux server accessed by multiple users? If so, consider adding an SSH login notification whenever someone logs in.

The examples below send an email when someone logs into your server. For this to work, your server must be able to send mail using the mail command. You may follow this tutorial to setup Exim with Gmail if your AKLWEB Host account is not verified to send mail.

Replace YOUR_EMAIL_ADDRESS with the email address that you want to receive login notifications.

CentOS

Open the file ~/.bash_profile in a text editor.

Append the following lines:

IP="$(echo $SSH_CONNECTION | cut -d " " -f 1)"
HOSTNAME=$(hostname)
NOW=$(date +"%e %b %Y, %a %r")

echo 'Someone from '$IP' logged into '$HOSTNAME' on '$NOW'.' | mail -s 'SSH Login Notification' YOUR_EMAIL_ADDRESS

Ubuntu/Debian

Open the file ~/.bashrc in a text editor.

Append the following lines:

IP="$(echo $SSH_CONNECTION | cut -d " " -f 1)"
HOSTNAME=$(hostname)
NOW=$(date +"%e %b %Y, %a %r")

echo 'Someone from '$IP' logged into '$HOSTNAME' on '$NOW'.' | mail -s 'SSH Login Notification' YOUR_EMAIL_ADDRESS
  • Enable SSH Login Notification on Linux
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Setting up a Name Server for IPv6 rDNS in CentOS

Managing your own IPv6 rDNS is a fairly simple task. After setting up the name server, please...

Reset the Root Password in CentOS

If you have forgotten the root password it is possible to set a new one by interrupting the boot...

How to Install and Configure vsftpd on CentOS 7

FTP (File Transfer Protocol) is one of the most popular methods to upload files to a server....

How to enable EPEL repository?

The EPEL repository is an additional package repository that provides easy access to install...

Find (View) Default Zone for Firewall on CentOS 7

Pre-Flight Check These instructions are intended for finding (viewing) the default zone in...