You can use netstat
for this. See the example (I grepped for ssh
):
netstat -putan | grep ssh
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1725/sshd
tcp 0 0 1.2.3.4:45734 1.2.3.5:22 ESTABLISHED 2491/ssh
tcp6 0 0 :::22 :::* LISTEN 1725/sshd
Explanation:
I often use the parameters -putan
(because they are simple to remember).
-p
: show the PIDs of the application/process
-u
: show udp ports/connections
-t
: show tcp ports/connections
-a
: show both listening and non-listening sockets
-n
: numeric output (don't do DNS lookups for hostnames etc.)
In the output above, you see that there is an ssh daemon process (sshd
) with PID 1725
listening at port 22
on all network interfaces (0.0.0.0
). Also there is an ssh client process (PID 2491
) connected to the IP-address 1.2.3.5
at port number 22
, my IP-address is 1.2.3.4
and my external port is 45734
. You see that the connection is established. Therefore I'm logged in via ssh
.