How to Get Instant Telegram Notifications for Every Successful SSH Login on Your Ubuntu Server (2026 Guide)

s
sfweb
Jan 12, 2026
4 min read

Hey there! If you're running an Ubuntu VPS (like many of our clients at SFWeb do), you already know how important it is to keep an eye on who's accessing your server. You've probably secured it with iptables and Fail2Ban, great start! But what if someone legitimately logs in... or worse, what if it's not so legitimate?

Wouldn't it be amazing to get a friendly ping on Telegram the second a successful SSH login happens? Instant peace of mind, whether you're at your desk in Canada or traveling. In this quick 2026-updated guide, we'll show you the cleanest, most reliable way using PAM (Pluggable Authentication Modules) and yes, it works perfectly for both password-based and SSH key-based logins.

Why Telegram Alerts Beat Email or Log Watching

Old-school methods like tailing logs or email alerts can be slow or buried. Telegram is instant, mobile-first, and you can even set up group notifications for your team. Plus:

  • Zero delay, notification hits your phone in seconds
  • Works even if you're not checking email
  • Customizable with emojis, host info, IP, and more
  • Doesn't break tools like VS Code Remote-SSH, SFTP, or scp

Ready? Let's set it up safely and cleanly.

Step 1: Create Your Telegram Bot (One-Time Setup)

  1. Open Telegram and search for @BotFather
  2. Type /newbot and follow the prompts to name it (e.g., "MyServerAlertBot")
  3. Copy the Bot Token it gives you (looks like 7123456789:AAHxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
  4. Start a chat with your new bot by sending it any message
  5. To get your Chat ID:
    • Forward a message from the bot chat to @getmyid_bot or @RawDataBot, or
    • Open in your browser: https://api.telegram.org/bot<YOUR_TOKEN>/getUpdates and look for "chat":{"id":...

Step 2: Create the Notification Script

SSH into your Ubuntu server and run:

sudo mkdir -p /etc/pam.scripts
sudo nano /etc/pam.scripts/ssh-telegram-notify.sh

Paste this modern, clean script (replace the two values in ALL CAPS):

#!/usr/bin/env bash

# Configuration - CHANGE THESE
TELEGRAM_TOKEN="YOUR_BOT_TOKEN_HERE"
TELEGRAM_CHAT_ID="YOUR_CHAT_ID_HERE"

HOSTNAME=$(hostname -f)
DATE=$(date '+%Y-%m-%d %H:%M:%S %Z')
IP=${PAM_RHOST:-unknown}
TTY=${PAM_TTY:-console}

if [[ "$PAM_TYPE" == "open_session" ]]; then
    EMOJI="🟢"
    ACTION="LOGIN SUCCESS"
elif [[ "$PAM_TYPE" == "close_session" ]]; then
    EMOJI="🔴"
    ACTION="LOGOUT"
else
    exit 0
fi

MESSAGE="${EMOJI} *${ACTION}* on \`${HOSTNAME}\`
👤 User: ${PAM_USER}
🌐 From IP: \`${IP}\`
🕒 Time: ${DATE}
📟 TTY: ${TTY}"

curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage" \
     -d chat_id="${TELEGRAM_CHAT_ID}" \
     -d text="${MESSAGE}" \
     -d parse_mode="Markdown" >/dev/null 2>&1

exit 0

Save and secure it:

sudo chmod 700 /etc/pam.scripts/ssh-telegram-notify.sh
sudo chown root:root /etc/pam.scripts/ssh-telegram-notify.sh

Step 3: Hook It Into PAM (The Magic Part)

Open the SSH PAM config:

sudo nano /etc/pam.d/sshd

Add this single line at the very bottom:

session    optional     pam_exec.so     /etc/pam.scripts/ssh-telegram-notify.sh

That's it! The optional flag ensures that even if Telegram is down or the token is wrong, your SSH login still works normally.

You're All Set! Test It!

Simply log out and SSH back into your server from another device or terminal. Within seconds, you should see a nice Telegram message pop up with all the details.

This setup reliably catches successful logins whether using password authentication or SSH keys, no extra configuration needed.

Need Help Securing or Optimizing Your Servers?

At SFWeb, we help businesses just like yours harden Ubuntu servers, set up monitoring, automate alerts, and build fast, secure websites that actually convert visitors.

If this feels a bit technical or you'd rather have a pro handle your server security, VPS hardening, web hosting setup, or full website development, reach out via our contact page. We'd love to help!

While you're here, check out our growing collection of free scripts and tools we share with the community.