Test port connectivity with nc -zv
In the previous steps, you learned how to list open ports using netstat
and ss
. Now, let's use the nc
command (netcat) to test if you can actually connect to a specific port. nc
is a versatile networking utility that can read from and write to network connections using TCP or UDP.
We will use nc
with the following options:
-z
: Specifies that nc
should just scan for listening daemons, without sending any data to them. This is useful for checking if a port is open.
-v
: Enables verbose output, showing more details about the connection attempt.
We will test the connectivity to port 22 (SSH), which we saw was listening in the previous steps. We will test connecting to the local machine, which can be referred to by the IP address 127.0.0.1
or the hostname localhost
.
Open your terminal if it's not already open.
Type the following command and press Enter:
nc -zv 127.0.0.1 22
You should see output similar to this:
Connection to 127.0.0.1 22 port [tcp/ssh] succeeded!
This output confirms that nc
was able to successfully connect to port 22 on your local machine.
Now, let's try testing a port that is likely not open, for example, port 80 (HTTP), as there is no web server running by default in this environment.
Type the following command and press Enter:
nc -zv 127.0.0.1 80
You will likely see output indicating a connection refused or timeout, similar to this:
nc: connect to 127.0.0.1 port 80 (tcp) failed: Connection refused
This output shows that nc
was unable to connect to port 80, which is expected since no service is listening on that port.
Using nc -zv
is a quick and easy way to verify if a specific port is reachable and open from your current location.
Click Continue to complete this lab.