Categories: CentOSLinuxShell

How to enable SSH login notifications by email in CentOS 8

In this tutorial, we will learn how to enable email SSH login notifications in CentOS 8.

Is your Linux Server used by multiple users and you want to know when a user is logging in by SSH? If yes, then you can enable SSH notifications in Bash profile to get notified. We will add a small script that will send you an email when somebody logs into the server.

Configuring SSH login notification

To configure SSH login notification open up the terminal and open the file ~/.bash_profile.

Add the following lines at the end of the file:

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>

Replace “your_email_address” with the email in which you want to receive an email. Save the file and quit.

Conclusion

In this tutorial, we learned how to enable notification in CentOS 8, when a user is accessing your Centos 8 Server over the SSH. It will send an email when the user accesses the system (Date and Time), and the IP address of the System from where the user accesses the system.

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn

Recent Posts

How to Install Magento 2 on AlmaLinux

Magento is a free and open-source e-commerce platform written in PHP. It is simple, easy…

1 year ago

How to Install ISPConfig Hosting Control Panel with Apache Web Server on Ubuntu 24.04

ISPConfig is an open-source control panel that allows users to manage multiple servers from a…

1 year ago

How to Test your Email Server (SMTP) Using the Telnet Command

As a Linux administrator, you may find it necessary to troubleshoot or test your Simple…

1 year ago

Managing Network Interfaces and Settings on Ubuntu 24.04 with nmcli

Ubuntu 24.04, like many modern Linux distributions, relies on the NetworkManager for managing network connections.…

2 years ago

Using Restic Backup on Ubuntu 24.04

Restic is a modern, open-source backup program designed for efficiency, security, and simplicity. It enables…

2 years ago

Installing phpMyAdmin on Rocky Linux 9 and Securing it with Let’s Encrypt SSL

phpMyAdmin is a popular free tool written in PHP intended to administer MySQL and MariaDB…

2 years ago