List active SSH sessions via the command-line in Linux
April 22, 2021 ‐ 1 min read
Your best bet to show the active SSH sessions on a system is probably to combine the netstat command with grep. The netstat command is used to show statistics on network connections, not just limited to SSH. The netstat command does require root privileges.
By piping the output of netstat to grep you can filter connections properly to just list active SSH sessions. The command looks as follows:
$ sudo netstat -tnpa | grep 'ESTABLISHED.*sshd'
More generally, you can use the commands users, who and w to list the currently logged in users on the system. Each displaying a different level of details.
The users commands just displays a list of usernames separated by spaces.
$ users
koen
The who command
$ who
koen pts/0 Apr 18 16:28 (80.113.224.189)
The w command
$ w
16:28:47 up 21 days, 5:05, 1 user, load average: 0.30, 0.13, 0.07
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
koen pts/0 80.113.224.189 16:28 7.00s 0.19s 0.01s w