I am looking to implement this feature where an LED connected to Pi GPIO should turn on whenever the Pi is successfully connected to a PC by SSH and turn off otherwise.

Appreciate any help in implementing this


2 Answers 2


Add this to your ~/.profile file, where <pin> refers to the pin number where the LED is connected:

if [[ -n $SSH_TTY ]] ; then
    gpio mode <pin> out
    trap 'gpio write <pin> 0' EXIT
    gpio write <pin> 1

The $SSH_TTY is set only in SSH sessions, so the whole block will be executed when you login via SSH, but not locally. trap executes a command when the session ends. Assuming that setting the GPIO pin to 1 turns the LED on and 0 turns it off, it should remain lit while the SSH session is active.

Note that this will not work with several simultaneous sessions. If you need that, you could write a script which lights the LED up repeatedly in a loop so that it would remain lit if one of multiple session ends.

  • Thanks for ur response. I added the above lines to /home/pi/.profile as follows: if [[ -n $SSH_TTY ]] ; then gpio mode 16 out trap 'gpio write 16 0' EXIT gpio write 16 1 fi LED is connected to Pi Pin no. 16 (physical pin). Sending 1 to the pin turns ON the LED. My Pi is connected to Putty through SSH. However, the LED didn't get turned ON. Pl. let me know if any further settings are to be made to implement the above feature. Thank u.
    – Raghavan K
    Mar 4, 2022 at 7:49
  • @RaghavanK I would start by keeping a single line, gpio write 16 1, and progressively adding code to see what is not working. If even this line doesn't work, it would mean you either didn't test it on the command line (it could be that you tested it with sudo which you forgot to add to the code), or your system is set up to use a different startup file, not /home/pi/.profile. Mar 4, 2022 at 10:40
  • I'm curious: The OP seems to be asking for a solution that will illuminate an LED when any user makes an SSH connection to the Pi. If your code is in ~/.profile, doesn't that limit it to working only for that one user?
    – Seamus
    Mar 5, 2022 at 22:53

Here's one way to do it:

Your question is a bit vague in some respects, but it seems to me that you'll want to know if anyone has an open SSH connection - not necessarily one specific user. The solution proposed here allows you to filter for certain users if that's what you want - or if any user has an ssh connection.

We'll develop a script to get the data we need, and turn the LED ON or OFF depending upon that data. The data on SSH connections will be provided by the command w. The LED will be controlled using the gpio-led overlay. Finally, this script will need to be a daemon of sorts - i.e. it will run in the background, even if no one is logged on. Otherwise, the LED would always be ON!

The script:

#!/usr/bin/env bash
# WTF?: Consult w for active SSH sessions - indicated by use of a pseudo-terminal pts
#       pipe to awk w/ patterns finds the number of pts for user pi
#       turn LED ON or OFF depending on number of pts

# put w in infinite loop REFCYCLE interval

   PTS_CT=$(w | awk '/pi/ && /pts/ {count++} END{print count}')
# until PTS_CT has a value assigned, set it to 0; it's a cron thing
   if ! [[ $PTS_CT =~ ^[0-9]+$ ]]


   if [ $PTS_CT -gt 0 ]
      echo 1 > /sys/class/leds/mysshled/brightness
      echo 0 > /sys/class/leds/mysshled/brightness


   sleep $REFCYCLE

Additional tasks:

  1. Open the file /boot/config.txt in your editor & add one line:


  1. Connect your LED & a 330 ohm resistor to your RPi - see pinout ref

Ground at Pin 20 --> 330𝝮 R --> Cathode|LED|Anode --> GPIO25 at pin 22

anode and cathode

Moment of truth

With all of the pieces above in place, start the script as follows:

$ sudo su
# nohup ./ssh-stats-w.sh &
[1] 1406

The script must be run as the root user as it needs permissions to write to sysfs, but running nohup from sudo is a bit tetchy. Therefore we use sudo su to become root & run nohup from a root shell. The output 1406 is the PID assigned to ssh-stats-w.sh. Also note that the process is run in the background & so that our terminal is freed for other activity.

Your LED should illuminate since at least you are logged on as user pi. With ssh-stats-w.sh running in the background & independent of your shell, you can now logout. Before you do, run w from the command prompt & verify you're the only user connected via SSH. If you're alone, logout, and the LED will be turned OFF until you log in again.

Other stuff potentially of interest

  • Streaming journalctl through awk could get us to the same place as w, but it becomes a bookkeeping task to keep up with all of the connections and disconnections. One advantage of journalctl is that it can also catch failed logins.

  • The script can be started "manually" as shown here, or it could be started by cron (use the root crontab), or included with a service file to start under systemd.

  • For cron:

$ sudo crontab -e

# Add 1 line: 
@reboot /home/pi/ssh-stats-w.sh >> /home/pi/ssh-stats-w.log 2>&1

# save & exit editor

$ sudo reboot
  • I don't like the GPIO control used here! It works, but uses the now-deprecated sysfs interface. The "new" libgpiod char-based API seems to be broken on my RPi 3B+. This may be related to a recent bug discovery in the kernel? I'm sure it'll be resolved eventually, and there are other alternatives for GPIO control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.