How to check port status and test connections

How to check if a port is listening on Linux

lsof (List of Open Files)

Do not let the name fool you. lsof is one of the best ways to get a list of TCP/UDP ports that are listening or have active sessions.

sudo lsof -i -P -n

Will produce output similar to below.

systemd-n  747 systemd-network   19u  IPv4  66150      0t0  UDP
systemd-r  749 systemd-resolve   12u  IPv4  20789      0t0  UDP
systemd-r  749 systemd-resolve   13u  IPv4  20790      0t0  TCP (LISTEN)
sshd       820            root    3u  IPv4  22075      0t0  TCP *:22 (LISTEN)
sshd       820            root    4u  IPv6  22077      0t0  TCP *:22 (LISTEN)
docker-pr 2586            root    4u  IPv6  36785      0t0  TCP *:1337 (LISTEN)
sshd      6630            root    4u  IPv4  69954      0t0  TCP> (ESTABLISHED)
sshd      6764            brad    4u  IPv4  69954      0t0  TCP> (ESTABLISHED)

Here we can see the server is listening on several ports and has active SSH connections to it.

If you want to just see ports the server is listening on, grep for LISTEN

sudo lsof -i -P -n | grep -i LISTEN

Fun Fact: lsof stands for "List of Open Files", because as you may know, everything in Linux is a file. You can do a lot more then check for open ports with this powerful command.

SS (Socket Status)

The SS command is another modern way to check the status of connections on a server. It's a super simple command to remember too. All abourd the SS -tuna!

ss -tuna

Will produce:

Netid      State       Recv-Q      Send-Q                Local Address:Port             Peer Address:Port       Process
udp        UNCONN      0           0                     *
udp        UNCONN      0           0                  *
tcp        LISTEN      0           4096                  *
tcp        LISTEN      0           128                         *
tcp        ESTAB       0           36                      
tcp        LISTEN      0           128                            [::]:22                       [::]:*
tcp        LISTEN      0           4096                              *:1337                        *:*


This is an older more deprecated ways to see what ports a server is listening on, but many people still use it. It also works for Windows.

netstat -tulpn | grep LISTEN

Testing Connections to Listening Ports on Linux

Two options that people use here. You can do it the old school way, using telnet


Just type telnet and if it makes a connection, you know the server is listening and accepting connections! Below is the command to test if a server is listening on port 80.

telnet localhost 80

NC (Netcat)

Netcat is a more functional option. It not only allows you to test TCP connections but also UDP.

nc [options] [server] [port]

Here we can test nc by testing http access to

nc -z -v 80

Testing UDP? Simply add

nc -z -v -u 123