SSH Telegram Notify
Have you ever wanted to get a Telegram notification when someone logs into your server via SSH? I did! I also wanted to automate the entire setup with Ansible for faster deployment and updates.
So, I created an Ansible role that does exactly that. It automatically configures your servers to send login/logout notifications to Telegram. It currently supports Debian/Ubuntu, but can easily be extended to other Linux distributions.
Why Use Telegram for SSH Notifications?
Monitoring SSH logins is an important security measure. It helps you:
- Detect unauthorized access attempts.
- Track who logs in and when.
- Get real-time alerts without relying on email.
Telegram is ideal for pet projects or small teams because it’s fast, lightweight, and has a simple API that’s easy to integrate into scripts. However, for enterprise environments, there are more robust options available, such as Slack, PagerDuty, or Opsgenie, which provide centralized management, audit logging, and enterprise-level access control.
Step 1: Create a Telegram Bot
To send messages to Telegram, you’ll need a bot. Don’t worry, no coding required.
- Create a Telegram bot by messaging @BotFather
- Send
/newbotand follow the instructions - Save the bot token provided
- Add the bot to your chat or group
- Send any message to that chat, then find your chat ID by visiting:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates
That’s all you need, your bot is now ready to send messages.
Step 2: Automated Setup with Ansible
To make the process repeatable across multiple servers, I created an Ansible role , also published on Ansible Galaxy .
Installation
Install Ansible: Official Installation Guide
Install the role from Ansible Galaxy:
ansible-galaxy install wiseelf.ssh_telegram_notify
Example Playbook
Create a playbook file, for example: playbook-ssh-telegram.yml
- hosts: servers
become: true
gather_facts: true
roles:
- ssh-telegram-notify
Do not store sensitive data in plain text instead use ansible-vault.
Read the Ansible documentation to learn how to encrypt your secrets securely.
Then create inventory/group_vars/all with your bot credentials:
telegram_bot_token: "YOUR_BOT_TOKEN_HERE"
telegram_chat_id: "YOUR_CHAT_ID_HERE"
Add your servers to an inventory file, inventory/hosts:
[servers]
192.168.1.1
192.168.1.2
...
10.10.10.10
Finally, run the playbook:
ansible-playbook playbook-ssh-telegram.yml -i inventory
That’s it! Now you’ll receive a Telegram notification each time someone logs in (or out) via SSH.
Step 3: Manual Setup (If You Prefer Doing It by Hands)
If you don’t use Ansible, you can still install it manually. On your server:
Create a directory for your PAM scripts:
sudo mkdir -p /etc/pam.scripts/ sudo chmod 700 /etc/pam.scripts/Create the script
/etc/pam.scripts/login.sh:#!/usr/bin/env bash TOKEN="<YOUR_TELEGRAM_BOT_TOKEN>" ID="<YOUR_CHAT_ID>" HOSTNAME=$(hostname -f) DATE="$(date +"⏰ %H:%M:%S on %d %b %Y")" TIMEZONE="$(date +"%Z")" case "$PAM_TYPE" in "open_session") ACTION_EMOJI="🟢" ACTION_DESC="SSH Login" ;; "close_session") ACTION_EMOJI="🔴" ACTION_DESC="SSH Logout" ;; *) ACTION_EMOJI="🔵" ACTION_DESC="SSH $PAM_TYPE" ;; esac MESSAGE="$ACTION_EMOJI $ACTION_DESC: $PAM_USER@$HOSTNAME from $PAM_RHOST at $DATE ($TIMEZONE)" URL="https://api.telegram.org/bot$TOKEN/sendMessage" curl -s -X POST "$URL" \ -d "chat_id=$ID" \ -d "text=$MESSAGE" \ -d "parse_mode=HTML" \ -d "disable_notification=false" \ > /dev/null 2>&1 exit 0Since it contains bot token this sript should be accessible only by root:
chmod 700 /etc/pam.scripts/login.shUpdate PAM SSH configuration:
session optional pam_exec.so /etc/pam.scripts/login.sh
Step 4: Testing
Once everything is in place:
- Use
pamtesterto verify PAM configuration syntax - SSH into your server from another host.
- You should immediately receive a Telegram message with the login details.
- Log out and verify that you get a logout message too.
Conclusion
Using a few lines of Bash and an Ansible role, you can have a real-time Telegram alerting system for SSH logins. It’s simple, lightweight, and gives you instant visibility into who’s accessing your servers - whether it’s for auditing, security, or just peace of mind.
You can find the full Ansible role and source code here: https://github.com/wiseelf/ssh-telegram-notify
