LLI Wiki — My Digital Knowledge Base

List connected SSH sessions on Linux

List connected SSH sessions with who, w, last and ss.

Several Linux commands can list the SSH sessions connected to a server. The examples below use generic accounts and addresses from the documentation range 192.0.2.0/24.

who

who shows logged-in users, their terminal, the login time and, when known, the remote host. The -a option enables all the standard columns (boot time, login processes, and so on).

who -a
root     pts/0        2020-12-19 14:20 (192.0.2.10)
user1    pts/1        2020-12-19 15:25 (192.0.2.11)
user2    pts/2        2020-12-19 14:30 (192.0.2.12)

The -u option adds the idle time column.

w

w shows logged-in users and the process each one is running. The -h option removes the header.

w -h
root     pts/0        14:20   2.00s  0.00s  ssh 192.0.2.10
user1    pts/1        14:25   1.00s  0.00s  ssh 192.0.2.11
user2    pts/2        14:30   1.00s  0.00s  ssh 192.0.2.12

last

last shows the login history. The -i option displays the IP address instead of the host name. An entry with no end time ("still logged in") is an open session.

last -i
root     pts/0        192.0.2.10       Sun Dec 19 14:20 - 14:25  (00:05)
user1    pts/1        192.0.2.11       Sun Dec 19 14:25 - 14:30  (00:05)
user2    pts/2        192.0.2.12       Sun Dec 19 14:30 - 14:35  (00:05)

ss

ss reads the socket table from the kernel. To filter SSH connections (port 22 by default, shown as ssh):

ss -t -a | grep ssh
tcp    ESTAB      0      0      192.0.2.10:ssh      192.0.2.10:46754        users:(("sshd",pid=9987,fd=3))
tcp    ESTAB      0      0      192.0.2.11:ssh      192.0.2.11:47754        users:(("sshd",pid=9987,fd=3))
tcp    ESTAB      0      0      192.0.2.12:ssh      192.0.2.12:48754        users:(("sshd",pid=9987,fd=3))

-t limits the output to TCP sockets, -a shows all sockets (listening and established), and grep keeps only the SSH lines.

Notes

Mastodon LLIMastodon SocialFosstodon