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.

 Terminal
sudo lsof -i -P -n

Will produce output similar to below.

COMMAND    PID            USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
systemd-n  747 systemd-network   19u  IPv4  66150      0t0  UDP 10.1.1.51:68
systemd-r  749 systemd-resolve   12u  IPv4  20789      0t0  UDP 127.0.0.53:53
systemd-r  749 systemd-resolve   13u  IPv4  20790      0t0  TCP 127.0.0.53:53 (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 10.1.1.51:22->10.1.1.35:52359 (ESTABLISHED)
sshd      6764            brad    4u  IPv4  69954      0t0  TCP 10.1.1.51:22->10.1.1.35:52359 (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

 Terminal
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!

 Terminal
ss -tuna

Will produce:

Netid      State       Recv-Q      Send-Q                Local Address:Port             Peer Address:Port       Process
udp        UNCONN      0           0                     127.0.0.53%lo:53                    0.0.0.0:*
udp        UNCONN      0           0                  10.1.1.51%ens192:68                    0.0.0.0:*
tcp        LISTEN      0           4096                  127.0.0.53%lo:53                    0.0.0.0:*
tcp        LISTEN      0           128                         0.0.0.0:22                    0.0.0.0:*
tcp        ESTAB       0           36                        10.1.1.51:22                  10.1.1.35:52359
tcp        LISTEN      0           128                            [::]:22                       [::]:*
tcp        LISTEN      0           4096                              *:1337                        *:*

netstat

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.

 Terminal
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

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 google.com

nc -z -v google.com 80

Testing UDP? Simply add

nc -z -v -u 0.north-america.pool.ntp.org 123