Active SSH connections on a Linux server include terminal logins and background activity. Comparing login sessions, live sockets, and their owning processes shows what is connected now. The logged-in user view covers terminal sessions, while login history covers past sessions. Live inspection does not retain history, record session contents, or cover Windows.

who and w show the logged-in sessions and what each one is running. ss shows the live TCP connections behind them, including background connections such as port forwards and file copies, which can appear in the login view with a blank terminal or not at all.

Listing connections changes nothing on the server, but ss needs sudo to show process owners. The address shown can be a jump host, VPN gateway, or NAT device instead of the person's own computer.

Steps to monitor active SSH connections on Linux:

  1. Open a terminal on the Linux server with an account that can use sudo.
  2. List the current logins and their client addresses.
    $ who
    user     sshd pts/0   Nov  6 15:03 (127.0.0.1)
    user     sshd pts/0   Nov  6 15:03 (127.0.0.1)

    A terminal label such as pts/0 can repeat or appear for a session with no terminal, so use the last step for the real connection count.

  3. Check what each session is running and how long it has been idle.
    $ w
     15:03:17 up  1:02,  2 users,  load average: 26.96, 28.26, 34.10
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU  WHAT
    user              127.0.0.1        15:03           0.00s  0.02s sshd-session: user [priv]
    user     pts/0    127.0.0.1        15:03    4.00s  0.01s  0.01s -bash

    A connection with no terminal, such as a remote command, can show a blank TTY. Some builds hide FROM, so use the address from who instead.

  4. Show the live SSH connections and the processes that own them.
    $ sudo ss -tnp sport = :ssh
    State Recv-Q Send-Q Local Address:Port Peer Address:Port Process                                                               
    ESTAB 0      0          127.0.0.1:22      127.0.0.1:46262 users:(("sshd-session",pid=1091,fd=7),("sshd-session",pid=1068,fd=7))
    ESTAB 0      0          127.0.0.1:22      127.0.0.1:46246 users:(("sshd-session",pid=1093,fd=7),("sshd-session",pid=1067,fd=7))

    Each row is one connection open right now, and Peer Address is where it came from. Replace :ssh with the port number, such as :2222, when the server listens on another port. No rows means nothing is connected on that port.

    A connection with no matching login row, or with an unexpected terminal label, is not suspicious on its own, because background connections and multiplexed connections look that way. Check it against the work expected on the server before ending it.

Share this guide