<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Ssh on MangoDriod</title><link>https://md.eknath.dev/tags/ssh/</link><description>Recent content in Ssh on MangoDriod</description><generator>Hugo -- 0.141.0</generator><language>en-us</language><lastBuildDate>Sat, 13 Sep 2025 10:00:00 +0530</lastBuildDate><atom:link href="https://md.eknath.dev/tags/ssh/index.xml" rel="self" type="application/rss+xml"/><item><title>How to Set Up Telegram Alerts for SSH Logins on Debian</title><link>https://md.eknath.dev/posts/software-development/telegram-ssh-login-alerts/</link><pubDate>Sat, 13 Sep 2025 10:00:00 +0530</pubDate><guid>https://md.eknath.dev/posts/software-development/telegram-ssh-login-alerts/</guid><description>&lt;p>Securing your Virtual Private Server (VPS) is critical. A simple and effective way to monitor access is to receive real-time notifications for every successful SSH login. This guide provides a step-by-step walkthrough for setting up instant Telegram alerts on a Debian-based server, giving you immediate awareness of all shell access.&lt;/p>
&lt;h2 id="1-prerequisites">1. Prerequisites&lt;/h2>
&lt;p>Before you begin, ensure you have the following:&lt;/p>
&lt;ul>
&lt;li>A server running a Debian-based Linux distribution.&lt;/li>
&lt;li>&lt;code>sudo&lt;/code> or &lt;code>root&lt;/code> access to the server.&lt;/li>
&lt;li>A Telegram account.&lt;/li>
&lt;li>&lt;code>curl&lt;/code> and &lt;code>jq&lt;/code> installed on your server. If you don&amp;rsquo;t have them, install them now:
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>sudo apt-get update &lt;span style="color:#f92672">&amp;amp;&amp;amp;&lt;/span> sudo apt-get install -y curl jq
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;/ul>
&lt;h2 id="2-create-a-telegram-bot">2. Create a Telegram Bot&lt;/h2>
&lt;p>Your alerts will be sent by a Telegram Bot.&lt;/p></description><content:encoded><![CDATA[<p>Securing your Virtual Private Server (VPS) is critical. A simple and effective way to monitor access is to receive real-time notifications for every successful SSH login. This guide provides a step-by-step walkthrough for setting up instant Telegram alerts on a Debian-based server, giving you immediate awareness of all shell access.</p>
<h2 id="1-prerequisites">1. Prerequisites</h2>
<p>Before you begin, ensure you have the following:</p>
<ul>
<li>A server running a Debian-based Linux distribution.</li>
<li><code>sudo</code> or <code>root</code> access to the server.</li>
<li>A Telegram account.</li>
<li><code>curl</code> and <code>jq</code> installed on your server. If you don&rsquo;t have them, install them now:
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>sudo apt-get update <span style="color:#f92672">&amp;&amp;</span> sudo apt-get install -y curl jq
</span></span></code></pre></div></li>
</ul>
<h2 id="2-create-a-telegram-bot">2. Create a Telegram Bot</h2>
<p>Your alerts will be sent by a Telegram Bot.</p>
<h3 id="step-1-talk-to-botfather">Step 1: Talk to BotFather</h3>
<p>In your Telegram app, search for the verified <strong>BotFather</strong> account and start a chat.</p>
<h3 id="step-2-create-the-bot">Step 2: Create the Bot</h3>
<p>Send the <code>/newbot</code> command. Follow the prompts to give your bot a display name and a unique username, which must end in <code>bot</code>.</p>
<h3 id="step-3-save-the-api-token">Step 3: Save the API Token</h3>
<p>Once created, BotFather will provide a secret <strong>API token</strong>. Copy this token and keep it secure; you will need it in the next steps.</p>
<h2 id="3-get-your-personal-chat-id">3. Get Your Personal Chat ID</h2>
<p>The bot needs to know where to send the alerts. This will be your personal Telegram chat.</p>
<h3 id="step-1-start-a-chat-with-your-bot">Step 1: Start a Chat with Your Bot</h3>
<p>Search for your new bot in Telegram and send it any message (e.g., <code>/start</code>). This initializes the chat.</p>
<h3 id="step-2-retrieve-the-chat-id">Step 2: Retrieve the Chat ID</h3>
<p>In your server&rsquo;s terminal, run the following command. Replace <code>&lt;YOUR_BOT_TOKEN&gt;</code> with the token you saved from BotFather.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>curl -s <span style="color:#e6db74">&#34;https://api.telegram.org/bot&lt;YOUR_BOT_TOKEN&gt;/getUpdates&#34;</span> | jq <span style="color:#e6db74">&#39;.result[0].message.chat.id&#39;</span>
</span></span></code></pre></div><p>This command will output a long number, which is your <strong>Chat ID</strong>. Copy it.</p>
<h2 id="4-create-the-notification-script">4. Create the Notification Script</h2>
<p>This shell script will be triggered on login, gather information, and send the alert.</p>
<h3 id="step-1-create-the-script-file">Step 1: Create the Script File</h3>
<p>Using a text editor, create a new file for the script:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>sudo nano /usr/local/bin/ssh-login-alert.sh
</span></span></code></pre></div><h3 id="step-2-add-the-script-content">Step 2: Add the Script Content</h3>
<p>Paste the following code into the file. <strong>Remember to replace the placeholder values for <code>BOT_TOKEN</code> and <code>CHAT_ID</code> with your actual credentials.</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e">#!/bin/bash
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># --- Replace with your details ---</span>
</span></span><span style="display:flex;"><span>BOT_TOKEN<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;&lt;YOUR_BOT_TOKEN&gt;&#34;</span>
</span></span><span style="display:flex;"><span>CHAT_ID<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;&lt;YOUR_CHAT_ID&gt;&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#----------------------------------</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Do nothing if not an interactive session</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span> <span style="color:#f92672">[</span> -z <span style="color:#e6db74">&#34;</span>$PAM_TYPE<span style="color:#e6db74">&#34;</span> <span style="color:#f92672">]</span> <span style="color:#f92672">||</span> <span style="color:#f92672">[</span> <span style="color:#e6db74">&#34;</span>$PAM_TYPE<span style="color:#e6db74">&#34;</span> !<span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;open_session&#34;</span> <span style="color:#f92672">]</span>; <span style="color:#66d9ef">then</span>
</span></span><span style="display:flex;"><span>    exit <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">fi</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Gather login information</span>
</span></span><span style="display:flex;"><span>HOSTNAME<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>hostname<span style="color:#66d9ef">)</span>
</span></span><span style="display:flex;"><span>USER<span style="color:#f92672">=</span>$PAM_USER
</span></span><span style="display:flex;"><span>IP_ADDRESS<span style="color:#f92672">=</span>$PAM_RHOST
</span></span><span style="display:flex;"><span>TIMESTAMP<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>date +<span style="color:#e6db74">&#34;%Y-%m-%d %H:%M:%S&#34;</span><span style="color:#66d9ef">)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Format the message using Markdown</span>
</span></span><span style="display:flex;"><span>MESSAGE<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>cat <span style="color:#e6db74">&lt;&lt;EOF
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">🔔 *New SSH Login Detected* 🔔
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">*Server:* \`$HOSTNAME\`
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">*User:* \`$USER\`
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">*From IP:* \`$IP_ADDRESS\`
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">*Time:* \`$TIMESTAMP\`
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">EOF</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Send the message via the Telegram API</span>
</span></span><span style="display:flex;"><span>curl -s --max-time <span style="color:#ae81ff">10</span> -X POST <span style="color:#e6db74">&#34;https://api.telegram.org/bot</span>$BOT_TOKEN<span style="color:#e6db74">/sendMessage&#34;</span> <span style="color:#ae81ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#ae81ff"></span>     -d chat_id<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span>$CHAT_ID<span style="color:#e6db74">&#34;</span> <span style="color:#ae81ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#ae81ff"></span>     -d text<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span>$MESSAGE<span style="color:#e6db74">&#34;</span> <span style="color:#ae81ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#ae81ff"></span>     -d parse_mode<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;Markdown&#34;</span> &gt; /dev/null
</span></span></code></pre></div><h3 id="step-3-make-the-script-executable">Step 3: Make the Script Executable</h3>
<p>Save the file and make it executable so the system can run it.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>sudo chmod +x /usr/local/bin/ssh-login-alert.sh
</span></span></code></pre></div><h2 id="5-configure-pam-to-trigger-the-script">5. Configure PAM to Trigger the Script</h2>
<p>We&rsquo;ll use the Pluggable Authentication Module (PAM) framework to execute our script whenever a user opens a new SSH session.</p>
<h3 id="step-1-edit-the-sshd-pam-configuration">Step 1: Edit the SSHD PAM Configuration</h3>
<p>Open the PAM configuration file for the SSH daemon:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>sudo nano /etc/pam.d/sshd
</span></span></code></pre></div><h3 id="step-2-add-the-execution-rule">Step 2: Add the Execution Rule</h3>
<p>Add the following line at the very <strong>end</strong> of the file. This tells PAM to run our script for every session, but our script is smart enough to only act on SSH logins.</p>
<pre tabindex="0"><code># Run script for SSH login notification
session optional pam_exec.so /usr/local/bin/ssh-login-alert.sh
</code></pre><p>Save and close the file.</p>
<h2 id="6-test-the-setup">6. Test the Setup</h2>
<p>You&rsquo;re all set! To test the notification, log out of your server and log back in via SSH.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>exit
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>ssh your_user@your_server_ip
</span></span></code></pre></div><p>Within seconds, you should receive a neatly formatted notification on Telegram from your bot. If you don&rsquo;t, double-check that the <code>BOT_TOKEN</code> and <code>CHAT_ID</code> in your script are correct and that the script is executable.</p>
<pre tabindex="0"><code></code></pre>]]></content:encoded></item></channel></rss>