Developer & Network Tools

Testing a Port With Command Line Tools

Check whether a TCP port answers using PowerShell, netcat, curl and openssl, and read the result.

Key takeaways

  • A port test checks that something accepts connections at an address and port, which ping cannot do.
  • Refused means a rejection came back; a timeout means nothing useful came back at all.
  • Testing from the server itself and then from outside shows whether the service or the path is at fault.
  • UDP ports can't be judged reliably with a simple connect test.
  • A successful connection doesn't prove the application behind it is healthy.

To test whether a TCP port is reachable, run Test-NetConnection example.com -Port 443 in Windows PowerShell, or nc -vz example.com 443 on Linux and macOS. A success means something accepted the connection. A refusal or a timeout tells you two quite different things about where the problem is.

Use these tests on systems you own or are authorized to check. Testing your own server is routine administration; sweeping other people's machines for open ports can breach terms of service or law, as port scanning and when it is legal explains.

What a port test does

A port identifies a particular service on a host. A TCP test attempts the same opening exchange a real client would: your side sends a connection request, and a listening service answers.

Three outcomes of a TCP port test
Your computer Target host SYN: may I connect? 1 SYN-ACK: yes 2 Open: a program is listening RST: no 3 Refused: nothing listening, or actively rejected SYN again, still no answer 4 Timeout: dropped or unreachable Your computer Target host SYN: may I connect? 1 SYN-ACK: yes 2 Open: a program is listening RST: no 3 Refused: nothing listening, or actively rejected SYN again, still no answer 4 Timeout: dropped or unreachable

Any single attempt ends in only one of these outcomes.

These messages are alternatives, not one sequence: any given attempt ends in only one of them. The normal opening is covered in the TCP three-way handshake.

Common ports worth knowing include 22 for SSH, 25 for mail transfer between servers, 53 for DNS, 80 for HTTP, 443 for HTTPS, 587 for mail submission, 993 for IMAP over TLS, 3306 for MySQL, 3389 for Remote Desktop and 5432 for PostgreSQL. Services can be moved to other numbers, so these are conventions.

Windows PowerShell

Test-NetConnection example.com -Port 443
Test-NetConnection 192.168.1.50 -Port 3389 -InformationLevel Detailed

Look for TcpTestSucceeded : True. If it fails, the command may also try a ping and report that separately. Remember that a failed ping with a successful TCP test is perfectly normal where ICMP is blocked.

The command takes a while to give up on a silent port. The old telnet client can do a similar check but isn't installed by default on current Windows, and there's no need to add it just for this.

Linux and macOS with netcat

nc -vz example.com 443
nc -vz -w 5 example.com 22

-v prints what happened, -z connects without sending data and -w sets a timeout in seconds. Several programs go by the name netcat, with small differences in options and messages, so check man nc on your system.

If netcat isn't installed, Bash has a built-in trick that works in many builds:

timeout 5 bash -c '</dev/tcp/example.com/443' && echo open || echo failed

curl and openssl

For web ports, test the real protocol:

curl -sv -o /dev/null --connect-timeout 5 https://example.com/
curl -v --connect-timeout 5 telnet://example.com:587

The first goes all the way through TLS and HTTP. The second just opens a TCP connection and shows anything the service sends; press Ctrl+C to leave. For more on reading the output, see using curl to understand web requests.

To check TLS on any port, use OpenSSL:

openssl s_client -connect example.com:443 -servername example.com
openssl s_client -connect mail.example.com:587 -starttls smtp

-servername sends the host name so a server with many sites picks the right certificate. The output shows the certificate chain and the verification result. -starttls handles protocols that begin in plain text and upgrade to encryption. Press Ctrl+C to exit.

Checking several ports you manage

For a handful of ports on your own server, a short loop saves retyping. In Bash with netcat:

for p in 22 80 443; do
  nc -z -w 3 server.example.com $p && echo "$p open" || echo "$p not reachable"
done

In PowerShell:

foreach ($p in 22, 80, 443) {
  $r = Test-NetConnection server.example.com -Port $p -WarningAction SilentlyContinue
  "{0}: {1}" -f $p, $r.TcpTestSucceeded
}

Keep lists short and targeted. A test like this answers "are the services I expect reachable?" It isn't a security audit.

Timing and intermittent failures

How long a result takes is a clue. An immediate success or refusal means packets are going out and coming back quickly. A success that arrives after a few seconds can mean the first connection request was lost and had to be sent again, or that name resolution is slow. Test by IP address as well as by name to separate those two.

A name may also resolve to several addresses, including both IPv4 and IPv6. One can work while another fails, which looks random from a browser or a script. Check what the name returns, then test each address in turn. With netcat, -4 and -6 restrict the family; with curl, the same options apply.

If a port works only some of the time, repeat the test at intervals and log the outcomes with timestamps. Compare those times with server restarts, deployments or load peaks. A listener whose waiting queue is full can ignore new connections, which produces timeouts only when it's busy.

Interpreting the result

What did the test show?
Did the name fail to resolve? YES Fix DNS first. The port was never tested. 1 NO Did the connection succeed? YES The path and listener work. Test the application next. 2 NO Was it refused right away? YES Check that the service runs and listens on that address and port. 3 NO Did it time out? YES Look for a firewall, security group, routing or NAT problem. 4 NO Another error Read the exact message, for example network unreachable. Did the name fail to resolve? 1 IF YES Fix DNS first. The port was never tested. NO Did the connection succeed? 2 IF YES The path and listener work. Test the application next. NO Was it refused right away? 3 IF YES Check that the service runs and listens on that address and port. NO Did it time out? 4 IF YES Look for a firewall, security group, routing or NAT problem. NO Another error Read the exact message, for example network unreachable.

Refusals aren't always from the server itself. A firewall can be configured to reject with a reset or an ICMP message instead of dropping silently. Browsers report the same family of failures in their own words, covered in connection refused, timed out and reset.

Test from inside, then outside

Work outward from the service.

Narrowing down where a port is blocked
1 Check the listener On the server, confirm a program listens on the expected address and port. 2 Connect locally Test 127.0.0.1 on the server itself. 3 Connect on the same network Test from a neighboring machine to involve the host firewall. 4 Connect from outside Test from another network to involve routers and cloud rules. Check the listener On the server, confirm a program listens on the expected address and port. 1 Connect locally Test 127.0.0.1 on the server itself. 2 Connect on the same network Test from a neighboring machine to involve the host firewall. 3 Connect from outside Test from another network to involve routers and cloud rules. 4

For the first step, use the commands in netstat and ss. A service listening only on 127.0.0.1 passes the local test and fails the others, and the fix is in its configuration, not the firewall.

If local network tests pass and outside tests fail on a home connection, check the router's port forwarding, and consider whether your provider uses carrier-grade NAT, which prevents incoming connections regardless of your settings. Testing your public address from inside your own network can mislead, because not every router loops such traffic back. Use a mobile connection or a server you control elsewhere.

On cloud platforms, there may be several layers: the provider's security rules, network access lists and the operating system firewall. All must allow the traffic. Some networks and providers also block outbound mail on port 25.

UDP is different

UDP has no handshake. If you send a datagram and hear nothing, the port may be open with a service that ignores your unexpected data, or a firewall may have dropped it. Only an ICMP "port unreachable" gives a clear negative, and that can be filtered too.

So test UDP services with a real client for the protocol. For DNS:

dig @192.0.2.53 example.com

Replace the documentation address with your own resolver. A proper answer proves far more than any generic probe. netcat's -u option exists, but a "succeeded" message from it for UDP shouldn't be trusted.

What success doesn't tell you

An open port means something answered. It could be the wrong program, an old version, a proxy or load balancer in front of a dead backend, or a firewall completing connections on the server's behalf. A database may accept TCP and then reject every login.

Finish with an application check: request a page and look at the status and content, run a harmless query, or send a test message. For ongoing monitoring, automate that application check and treat the bare port test as a supplement.

Keeping your own ports tidy

While you're testing, note anything open that shouldn't be. Close services you don't use, bind internal ones to loopback or a private interface, and restrict administration ports to known addresses or a VPN. Then run the outside test again to confirm that the port you meant to close really is closed. A change you haven't verified is only a hope.

Write down the expected list for each server: port, service, and who should be able to reach it. Repeating the tests against that list after firewall changes or upgrades catches both accidental openings and accidental blocks. Both directions matter, because a rule that quietly cuts off monitoring or backups may not be noticed until you need them.

A worked example

Suppose you've set up a hypothetical web application on port 8080 of a home server, and your colleague on the same network can't open it. On the server, ss -tlnp shows it listening on 127.0.0.1:8080. A local test succeeds, and a test from the colleague's machine is refused. The cause is the listening address: the program accepts connections only from itself.

You change its configuration to listen on the server's network address and restart it. Now the remote test times out where before it was refused. That change of symptom is progress. The listener is fine, and something is now silently dropping the packets, most likely the host firewall. You add a rule permitting port 8080 from your local subnet, test again, and it connects.

Each result pointed to one layer, and you changed one thing at a time.

Common questions

Why does a port test succeed while the website still fails?

The connection step worked, but something later didn't: the certificate, the HTTP response or the application. Test with curl and read the status.

Why do results differ between networks?

Firewalls, provider filtering and DNS answers vary by network. Note where each test was run from, and compare the resolved addresses.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Developer & Network Tools →

Latest news on this topic