SSH Login Alerts to Telegram Without a Bot Token
push.tgWant a Telegram message every time someone logs in to your server over SSH? Linux already has the hook for it: PAM's pam_exec module runs a program at the start of every session. Point it at a two-line script with curl and you're done. No BotFather, no bot token, no chat ID.
Step 1: get your endpoint (30 seconds)
Open @trafficRobot in Telegram and press Start. You get an address like k3x9q@push.tg. Your endpoint is the part before the @: https://push.tg/k3x9q. Every example below uses k3x9q, so swap in your own key.
There's no bot to create, no chat_id to look up and no token to keep safe. Add @trafficRobot to a group and the group gets its own address, so everyone on call sees the messages.
Step 2: the alert script
Save this as /usr/local/bin/pushtg-ssh and make it executable (sudo chmod +x /usr/local/bin/pushtg-ssh):
#!/bin/sh
[ "$PAM_TYPE" = "open_session" ] || exit 0
curl -s -o /dev/null -m 10 --data-urlencode "data=SSH login on $(hostname): $PAM_USER from ${PAM_RHOST:-local}" https://push.tg/k3x9q &
exit 0pam_exec puts the details in the environment: PAM_USER (who logged in), PAM_RHOST (where from), PAM_SERVICE and PAM_TYPE. The first line makes sure you get one message per login, not a second one at logout. The & sends the curl to the background, so a slow network never holds up the login. --data-urlencode "data=..." puts the text in the form field push.tg reads, safely encoded.
Step 3: hook it into sshd's PAM config
Add this line at the end of /etc/pam.d/sshd:
session optional pam_exec.so type=open_session /usr/local/bin/pushtg-ssh
optional matters: if the script ever fails, the login still goes through, so you can't lock yourself out. type=open_session runs it only when a session starts. Keep your current SSH session open while you try this, just in case.
This needs UsePAM yes in /etc/ssh/sshd_config. Debian and Ubuntu ship with it on. With PAM on, sshd runs the session modules for every kind of login, so key-based logins are reported too, not only passwords. The PAM files are read for each new login, so there's nothing to restart.
Step 4: test it
From another terminal, log in again:
ssh you@your-server
You should get something like SSH login on web1: you from 203.0.113.7. If nothing arrives, run the curl by hand to rule out the network:
curl -d "test from $(hostname)" https://push.tg/k3x9q
Good to know
The message text comes from the login itself, so treat it as a heads-up, not an audit log; the pam_exec man page warns that the user can influence the environment. It's still a quick way to notice a login you didn't expect.
Want the same alert for su or sudo -i? Add the same line to /etc/pam.d/su and include $PAM_SERVICE in the message so you can tell them apart.
Tip: point this at a group. Each Telegram chat or group gets its own push.tg address, so everyone who looks after the server sees the logins.
Check that the message really arrived. push.tg answers HTTP 200 even when the key is wrong; the body then says {"error":"no such user"}. Send one test by hand and look at Telegram before you trust it:
Official references: pam_exec(8) (exported PAM_* variables, type=) and sshd_config(5) (UsePAM).