Skip to content

Instantly share code, notes, and snippets.

@joshschmelzle
Last active August 4, 2023 11:23
Show Gist options
  • Save joshschmelzle/271ccc9fc0cbc7c6c3b1f1d8f911a43c to your computer and use it in GitHub Desktop.
Save joshschmelzle/271ccc9fc0cbc7c6c3b1f1d8f911a43c to your computer and use it in GitHub Desktop.
List all connected SSH sessions

How to list all connected SSH sessions

I was curious how to view sessions on a Linux box I had at my desk. Similar to the session table on an Aruba controller (show loginsessions). Here are some ways you can list active SSH sessions; some commands return more output than others. This applies to most modern Linux boxes or say a WLAN Pi.

All examples below are using 2 MobaXterm user sessions from a Windows machine to a Linux 4.14.42-sunxi64 aarch64 (NanoPi NEO2).

w

wlanpi@wlanpi:~$ w
 15:00:36 up  2:36,  2 users,  load average: 0.21, 0.20, 0.18
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
wlanpi   pts/0    192.168.1.68      14:51    0.00s  0.41s  0.01s w
wlanpi   pts/2    192.168.1.68      13:00    9:00   0.49s  0.49s -bash

who

wlanpi@wlanpi:~$ who
wlanpi   pts/0        2018-05-23 14:51 (192.168.1.68)
wlanpi   pts/2        2018-05-23 13:00 (192.168.1.68)

echo $SSH_CONNECTION

This one is for when you're in a pseudo shell and should return your IP and port and the IP you're connected to and port. This won't show other sessions.

wlanpi@wlanpi:~$ echo $SSH_CONNECTION
192.168.1.68 5875 192.168.1.94 22

netstat -tnpa | grep 'ESTABLISHED.*sshd'

wlanpi@wlanpi:~$ sudo netstat -tnpa | grep 'ESTABLISHED.*sshd'
tcp        0      0 192.168.1.94:22        192.168.1.68:19172       ESTABLISHED 927/sshd: wlanpi [p
tcp        0      0 192.168.1.94:22        192.168.1.68:5875        ESTABLISHED 17200/sshd: wlanpi
tcp        0      0 192.168.1.94:22        192.168.1.68:5876        ESTABLISHED 17222/sshd: wlanpi
tcp        0      0 192.168.1.94:22        192.168.1.68:19168       ESTABLISHED 886/sshd: wlanpi [p

ps auxwww | grep sshd:

wlanpi@wlanpi:~$ ps auxwww | grep sshd:
root       886  0.9  1.2  11872  6092 ?        Ss   14:51   0:00 sshd: wlanpi [priv]
root       927  0.8  1.2  11876  6076 ?        Ss   14:51   0:00 sshd: wlanpi [priv]
wlanpi     997  2.2  0.9  12060  4460 ?        S    14:51   0:00 sshd: wlanpi@pts/0
wlanpi    1070  0.0  0.9  11876  4508 ?        S    14:51   0:00 sshd: wlanpi@notty
wlanpi    1196  0.0  0.1   4308   580 pts/2    S+   14:51   0:00 grep sshd:
root     17200  0.0  1.2  11872  6120 ?        Ss   13:00   0:00 sshd: wlanpi [priv]
root     17222  0.0  1.2  11876  5992 ?        Ss   13:00   0:00 sshd: wlanpi [priv]
wlanpi   17294  0.0  0.9  12212  4700 ?        S    13:00   0:00 sshd: wlanpi@pts/2
wlanpi   17365  0.0  0.9  11876  4552 ?        S    13:00   0:00 sshd: wlanpi@notty

pgrep -ai sshd

wlanpi@wlanpi:~$ pgrep -ai sshd
777 /usr/sbin/sshd -D
886 sshd: wlanpi [priv]
927 sshd: wlanpi [priv]
997 sshd: wlanpi@pts/0
1070 sshd: wlanpi@notty
17200 sshd: wlanpi [priv]
17222 sshd: wlanpi [priv]
17294 sshd: wlanpi@pts/2
17365 sshd: wlanpi@notty
@gbrault
Copy link

gbrault commented Dec 21, 2022

ps axjf | grep [s]shd:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment