Networking

How to Check if a Port Is Open (Windows, Linux, macOS)

To check if a port is open on a remote machine from Windows, run Test-NetConnection -ComputerName example-server -Port 443 in PowerShell and read TcpTestSucceeded. On Linux and macOS, use ncat -zv example-server 443, or ss -ltn on Linux to see which ports are open on the machine you are sitting at.

Replace example-server and 443 with your host name or IP address and your port. "Open" can mean two different things, so decide which one you need first:

  • Is something listening on this computer? That is a local check, and it lists the ports your own programs have bound. Skip to the local section below.
  • Can I reach that port on another computer? That is a remote check. It tests the whole path: your network, any firewall or router in between, and the program at the far end.

Check if a port is open on Windows (PowerShell)

Test-NetConnection ships with PowerShell on Windows and tests a TCP connection. Microsoft's reference describes the -Port parameter as the TCP port number on the remote computer, used to test connectivity.

Test-NetConnection -ComputerName example-server -Port 443

In the output, the line to read is TcpTestSucceeded. True means the TCP connection was established, so the port is open and reachable. False means it was not, and the reason is not in that one line: the port may be closed, blocked by a firewall, or the host may be down.

Microsoft's own example output for a port test lists TcpTestSucceeded : True at the end of a block that also shows the remote address, the port and the interface used. For a script, add -InformationLevel Quiet. Microsoft documents that this returns a Boolean instead of the full report:

Test-NetConnection -ComputerName example-server -Port 443 -InformationLevel Quiet

The cmdlet also has -CommonTCPPort, which accepts one of four names (HTTP, RDP, SMB, WINRM) instead of a number. It tests TCP only. It cannot test a UDP port.

Check if a port is open on Linux and macOS (ncat)

Ncat is the netcat from the Nmap project. Its reference page lists -z as zero-I/O mode, which reports connection status only, -v for verbose output, and -w for a connect timeout (seconds by default, or with a suffix such as ms). Combine them:

ncat -zv -w 3 example-server 443

Ncat connects, sends no data and reports whether the connection worked. The same page says the exit code is 0 for no error and 1 for a network error such as a refused connection, so a script can test it directly:

ncat -z -w 3 example-server 443 && echo open || echo not reachable

Ncat is not installed by default on most systems. It comes with the Nmap package on Linux distributions and on macOS, through your package manager. Other netcat variants exist and their flags differ, so if ncat is missing, check the manual of the nc your system provides before copying options from here.

Scan with nmap

Nmap is the tool built for this, and it is the one to use when you want to know why a port is not reachable. Use -p to pick the port:

nmap -p 443 example-server

Example output (the exact layout can vary by version):

PORT    STATE  SERVICE
443/tcp open   https

The state column is the useful part. Nmap's documentation defines six states. The ones you will see most often:

StateNmap's meaningWhat to do next
openAn application is actively accepting TCP connections, UDP datagrams or SCTP associations on the port.The port works. If your program still fails, the problem is above the network layer.
closedThe port is accessible and answers probes, but no application is listening on it.The host is reachable. Start the service or fix its port setting.
filteredPacket filtering prevents the probes from reaching the port, so Nmap cannot determine whether it is open.Check firewall rules, security groups and router settings between you and the host.

The other three states (unfiltered, open|filtered and closed|filtered) mean Nmap could not decide between two possibilities. open|filtered is common when scanning UDP, because a UDP service often sends no reply to an empty probe.

Only scan machines you own or have permission to test. A single port on your own server is routine, but scanning other people's networks can break their acceptable use policies.

Check which ports are open on this computer

A local check tells you what is listening, with no firewall involved. On Linux, ss from iproute2 does it. Its manual page says -l shows listening sockets (omitted by default), -t selects TCP, -n skips name resolution and -p shows the processes using the sockets:

sudo ss -ltnp

To look at a single port, use the filter syntax from the same page, where sport compares the local port number:

sudo ss -ltnp 'sport = :8080'

Example output:

State   Recv-Q  Send-Q  Local Address:Port  Peer Address:Port  Process
LISTEN  0       511     0.0.0.0:8080        0.0.0.0:*          users:(("node",pid=51234,fd=23))

If the command prints only the header, nothing is listening on that port. On Windows, macOS and for the process lookup commands in general (netstat -ano, Get-NetTCPConnection and lsof -i), see how to fix "port already in use", which walks through each one.

Read the Local Address column. 0.0.0.0 (all IPv4 addresses) or [::] (all IPv6 addresses) means the program accepts connections from other computers, subject to firewalls. 127.0.0.1 means it accepts connections from this computer only, which is a common reason a port looks open locally and closed from elsewhere.

Why a port looks closed when the service is running

Work down the path from the program to the client, and stop at the first failure:

  1. The service is bound to loopback. Look at the local address as above. The fix is in the program's configuration, usually a "bind address" or "host" setting.
  2. The host firewall blocks it. Windows Defender Firewall, ufw, firewalld and similar tools block inbound connections by default for programs that have no allow rule.
  3. A router or cloud firewall blocks it. Reaching a machine from the internet usually needs a forwarding rule on the router, or an inbound rule in the cloud provider's firewall. Port forwarding explained covers how those rules work.
  4. You are testing the wrong protocol. Test-NetConnection tests TCP only, and a service that speaks UDP will look closed to it.
  5. A timeout and a refusal mean different things. A fast refusal fits the nmap closed state: the host answered and nothing listens. A long wait then failure fits filtered: something dropped the packets silently.

If you are not sure which port a service should use, the list in common port numbers shows the standard assignments, and what a port number is explains the basics.

Checking from outside your own network

All of the commands above test from the computer you type them on. A server that answers on your home network may still be unreachable from the internet, so for a service you expect outside users to reach, run the test from a machine outside your network, such as a cloud virtual machine, and not from the same LAN. Testing a public address from inside your own network can give a different result, because some routers handle traffic to their own public address differently. Treat a result as valid only for the path it tested.

Frequently asked questions

Can I check if a port is open without installing anything?
On Windows, yes: Test-NetConnection is built into PowerShell and tests a TCP port on a remote computer. On Linux and macOS, ss (Linux) or lsof (macOS) shows what is listening on the local machine without any install. Testing a remote port from Linux or macOS usually needs ncat or nmap, unless your system already ships a netcat.
What is the difference between a closed port and a filtered port?
Nmap's documentation defines a closed port as reachable but with no application listening, so the machine answers and refuses. A filtered port is one where packet filtering, such as a firewall, stops the probes from reaching it, so the scanner cannot tell whether anything is listening. A refusal usually means closed; a timeout usually means filtered.
Is it legal to scan a port on someone else's server?
Testing a single port on a machine you own or administer is routine. Scanning systems you have no permission to test can break an acceptable use policy or local law, so test your own hosts or ones whose owner has agreed. This article does not cover that question beyond the warning.
Why does the port show as open on my PC but closed from another computer?
The service may be listening only on the loopback address 127.0.0.1, which other computers cannot reach, or a firewall or router may be blocking the port from outside. Check the local address column in ss or netstat: 127.0.0.1 means local only, while 0.0.0.0 or [::] means all addresses.

Sources