Strovent SystemsSign in
Reference

Check if a TCP port is open without Telnet: nc, Test-NetConnection, bash

Test whether a host answers on a TCP port without a Telnet client: nc -vz on macOS and Linux, Test-NetConnection in PowerShell, or bash /dev/tcp.

A port test only answers one question: does a TCP connection to this host and port succeed? It does not log in, and it does not prove the right service is behind the port. Use it before you blame a terminal setting.

macOS and Linux: nc

nc -vz -w 5 host.example.com 23

-z connects and closes again without sending data, -v prints the result, and -w 5 gives up after 5 seconds. A success line names the port, for example Connection to host.example.com 23 port [tcp/telnet] succeeded!. The exact wording differs between nc versions. Leave out -z to stay connected and see what the server sends first, such as an SSH-2.0-... line on port 22.

Windows: PowerShell

Test-NetConnection host.example.com -Port 992

TcpTestSucceeded : True means the port accepted the connection. Add -InformationLevel Quiet to get only True or False. The cmdlet is part of the NetTCPIP module that comes with Windows, so it works when the Telnet client is not installed.

Any bash shell

When neither tool is present, bash can open the connection itself:

timeout 5 bash -c 'echo > /dev/tcp/host.example.com/23' && echo open || echo closed

/dev/tcp/host/port is a bash feature, not a real file, so it does not work in sh, dash or zsh. timeout comes with Linux (GNU coreutils) but not with macOS. Without it, a port that never answers can make the test hang for a long time.

Reading the result

  • Succeeded or True: something accepts connections on that port. If your terminal still fails, look at the protocol, TLS or sign-on next.
  • Connection refused: the host answered, but nothing listens on that port, or a firewall rejects it. Check the port number and that the service is started.
  • Timed out: nothing answered. A firewall, VPN or routing problem is the usual cause, or the host is down.
  • Name not resolved: the host name is wrong or DNS cannot find it. Try the IP address.

Common terminal ports: 23 for Telnet, TN3270 and TN5250; 992 for TN3270 or TN5250 over TLS; 22 for SSH. Hosts can use others.

Sources

Spotted a mistake? Email [email protected].