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
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
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
w to list the currently logged in users on the system. Each displaying a different level of details.
users commands just displays a list of usernames separated by spaces.
$ users koen
$ who koen pts/0 Apr 18 16:28 (22.214.171.124)
$ 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 126.96.36.199 16:28 7.00s 0.19s 0.01s w