Is Port Open on Remote Server?

Table of contents

Is that port open on the server? question is critical to troubleshooting communication issues in a production network. Especially if the server responds to ping, but the application remains unreachable.

What follows are typical methods to test the status of the port (port knock) on the "reachable" remote server. Presuming working knowledge of TCP/IP and listed utilities, the command syntax and use cases are pretty much self-explanatory. I've included the description of not-so-obvious command options from the man page.

Netcat

nc -w<timeout-in-seconds> -z -v <IP_ADDR> <PORT | PORT_RANGE>

-z option directs netcat not send any data, only scan for listening daemon.

❯ nc -w2 -zv server 22
Connection to server (192.168.119.128) 22 port [tcp/ssh] succeeded!

❯ nc -w2 -zv server 8080
nc: connect to server (192.168.119.128) port 8080 (tcp) failed: Connection refused
❯ nc -w2 -zv server 22-25 80 8080
Connection to server (192.168.119.128) 22 port [tcp/ssh] succeeded!
nc: connect to server (192.168.119.128) port 23 (tcp) failed: Connection refused
nc: connect to server (192.168.119.128) port 24 (tcp) failed: Connection refused
nc: connect to server (192.168.119.128) port 25 (tcp) failed: Connection refused
Connection to server (192.168.119.128) 80 port [tcp/http] succeeded!
nc: connect to server (192.168.119.128) port 8080 (tcp) failed: Connection refused
for i in {20..30}; do nc -zv server $i &> /dev/null && echo "$i...open" || echo "$i...closed"; done
20...closed
21...closed
22...open
23...closed
24...closed
25...closed
26...closed
27...closed
28...closed
29...closed
30...closed

❯ for i in {20..100}; do nc -zv server $i &> /dev/null && echo "$i...open"; done
22...open
80...open
❯ nc -vz -u 8.8.8.8 53
Connection to 8.8.8.8 53 port [udp/domain] succeeded!

Telnet

❯ telnet server 80
Trying 192.168.119.128...
Connected to vm1.
Escape character is '^]'.
^CConnection closed by foreign host.

❯ telnet server 8080
Trying 192.168.119.128...
telnet: Unable to connect to remote host: Connection refused

Curl/Wget

❯ curl telnet://server:22
SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.5
^C
❯ curl telnet://server:8080
curl: (7) Failed to connect to server port 8080 after 0 ms: Connection refused

❯ wget -qO- server:22
SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.5
Invalid SSH identification string.
^C
❯ wget -qO- server:8080

Bash

$ cat < /dev/tcp/server/22
SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.5
^C
$ cat < /dev/tcp/server/8080
bash: connect: Connection refused
bash: /dev/tcp/server/8080: Connection refused