
When something "doesn't work", the first question is often: is the other side even reachable, and is the port open? Answering that question quickly saves a lot of time. This article explains why it matters, what ping can and cannot tell you, the basics of a TCP session, and three practical ways to check connectivity to a port on Linux.
Why it is useful to know how connectivity can be checked
A web server that does not respond, a database that refuses connections, or a deployment that silently fails are all situations where you need to know one thing: is a TCP port reachable from where I am standing?
Knowing how to check this yourself gives you:
- Fast diagnosis – you can tell a network problem from an application problem.
- Independence from the application – you do not need a client library or a browser to test a service.
- Better security awareness – you see what your own machines expose.
What ping can do
ping sends ICMP Echo Requests and waits for ICMP Echo Replies. It answers:
- Is the IP address reachable at all?
- How long does a round trip take (latency)?
- Is there packet loss?
ping -c 4 example.com
Output:
PING (172.66.147.243) 56(84) bytes of data.
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=1 ttl=59 time=8.39 ms
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=2 ttl=59 time=7.65 ms
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=3 ttl=59 time=7.75 ms
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=4 ttl=59 time=7.80 ms
--- ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 7.650/7.898/8.392/0.290 ms
What ping cannot do
ping works on the network layer (IP/ICMP). It does not know anything about TCP ports, so it cannot tell
you:
- Whether a specific port (22, 80, 443, 5432, ...) is open.
- Whether a service behind that port is actually running and healthy.
- Whether a firewall allows TCP traffic, since many firewalls allow ICMP but block TCP, or the other way round.
A host that does not answer ping may still serve HTTPS perfectly well, because ICMP is often filtered. A host that answers ping may have every port closed. Ping tells you about the host; port checks tell you about the service.
TCP/IP basics: the lifecycle of a TCP session
TCP is a connection-oriented protocol. Before any application data flows, both sides agree on a connection, and when they are done, they close it. Understanding these steps is the key to reading the output of the tools below.
The 3-way handshake
A client connects to a server by exchanging three segments:
sequenceDiagram
participant C as Client
participant S as Server
C->>S: 1. SYN - "I want to connect"
S-->>C: 2. SYN-ACK - "OK, I am listening"
C->>S: 3. ACK - "Connection established"
Note over C,S: Data can now flow
What each outcome means for a port check:
| Server reply to SYN | Meaning | Typical tool output |
|---|---|---|
| SYN-ACK | Port is open (something listens) | succeeded / open |
| RST | Port is closed (host reachable) | refused / closed |
| No reply | Filtered by firewall, or host down | timeout / filtered |
This table is the core of everything that follows. A port scanner is essentially a program that sends SYNs and interprets the replies.
Ending a session: FIN or RST?
A session normally ends gracefully with a FIN exchange (four segments, because each direction is closed separately):
sequenceDiagram
participant C as Client
participant S as Server
C->>S: FIN - "I am done sending"
S-->>C: ACK
S->>C: FIN - "I am done too"
C-->>S: ACK
An RST (reset) aborts the connection immediately, without the graceful exchange. It is used when:
- A segment arrives for a port nobody listens on (this is why closed ports answer with RST).
- An application crashes or closes its socket with unread data pending.
- A firewall or intrusion prevention system decides to tear down a connection.
- A connection is idle too long and an intermediate device drops its state.
Rule of thumb: a FIN means "the conversation ended normally"; an RST means "something went wrong or nobody was there". When a port check ends immediately with "connection refused", you are seeing an RST.
Checking connectivity with netcat
netcat (nc) opens raw TCP (or UDP) connections. With -z it only performs the handshake and closes again, sending
no data. This makes it a good one-off tester.
nc -zv -w 3 172.66.147.243 443
Options used:
-z– scan mode, no data is sent.-v– verbose, print the result.-w 3– 3 seconds timeout for the check
Output:
172.66.147.243: inverse host lookup failed: Unknown host
(UNKNOWN) [172.66.147.243] 443 (https) open
The warning comes from a reverse DNS lookup of the IP address and can be ignored. The exact wording of the success message depends on the netcat implementation (see the note below).
A closed port reports Connection refused:
localhost [127.0.0.1] 25 (smtp) : Connection refused
A filtered port usually runs into the timeout.
Without -v, nothing is printed. Check the exit code instead: 0 means the port is open, 1 means it is closed or filtered.
nc -z -w 3 172.66.147.243 443; echo "exit code: $?"
nc -z -w 3 127.0.0.1 25; echo "exit code: $?"
Output:
exit code: 0
exit code: 1
This makes the check easy to use in scripts, for example if nc -z -w 3 host 443; then echo open; fi.
-w 3– wait at most 3 seconds (important, otherwise filtered ports can hang for minutes).
To check a range of ports:
nc -zv -w 2 127.0.0.1 20-25
Output (this netcat prints only the open port in a range):
localhost [127.0.0.1] 22 (ssh) open
Note: There are several netcat implementations (OpenBSD
nc, GNUnetcat-traditional,ncatfrom Nmap). Options differ slightly; checkman ncon your system.
Checking connectivity with nmap
nmap is the standard port scanner. It does everything netcat does, but in parallel, with service detection, and with
a clearer view of filtered ports.
Install it with your package manager, for example sudo apt install nmap on Debian/Ubuntu or sudo dnf install nmap on Fedora.
Check a few common ports:
nmap -p 22,80,443 example.com
Output:
Starting Nmap 7.93 ( https://nmap.org ) at 2026-11-22 06:22 CET
Nmap scan report for example.com (172.66.147.243)
Host is up (0.0086s latency).
Other addresses for example.com (not scanned): 2606:4700:10::6814:179a 2606:4700:10::ac42:93f3 104.20.23.154
PORT STATE SERVICE
22/tcp filtered ssh
80/tcp open http
443/tcp open https
Nmap done: 1 IP address (1 host up) scanned in 1.29 seconds
The STATE column maps directly to the handshake table above:
open– received SYN-ACK.closed– received RST.filtered– no usable reply, probably a firewall.
Useful options:
nmap -Pn -p 1-1024 example.com # skip host discovery; useful when ping is blocked
nmap -sT -p 22 127.0.0.1 # TCP connect scan, works without root
nmap -sV -p 80 example.com # try to detect the service version
sudo nmap -sS -p- 127.0.0.1 # SYN scan over all 65535 ports (needs root)
Output (headers and trailing lines trimmed where noted):
$ nmap -Pn -p 1-1024 example.com
PORT STATE SERVICE
80/tcp open http
443/tcp open https
Nmap done: 1 IP address (1 host up) scanned in 4.60 seconds
$ nmap -sT -p 22 127.0.0.1
PORT STATE SERVICE
22/tcp open ssh
Nmap done: 1 IP address (1 host up) scanned in 0.08 seconds
$ nmap -sV -p 80 example.com
PORT STATE SERVICE VERSION
80/tcp open http Cloudflare http proxy
Nmap done: 1 IP address (1 host up) scanned in 12.28 seconds
$ sudo nmap -sS -p- 127.0.0.1
PORT STATE SERVICE
22/tcp open ssh
53/tcp open domain
80/tcp open http
... (remaining open ports omitted)
Not shown: 65485 closed tcp ports (reset)
Nmap done: 1 IP address (1 host up) scanned in 3.10 seconds
Only scan hosts you own or have explicit permission to scan.
Checking connectivity in Linux with timeout and bash's /dev/tcp
Sometimes you have no netcat or nmap installed, for example on a minimal server or inside a container. Bash can check a port on its own.
Bash can open TCP connections through the special path /dev/tcp/HOST/PORT. Wrapping it in timeout makes sure the
check never hangs:
timeout 3 bash -c '</dev/tcp/example.com/443' && echo "open" || echo "closed or filtered"
Output when the port is open:
open
For a closed port, here 127.0.0.1 port 1, the same command prints:
timeout 3 bash -c '</dev/tcp/127.0.0.1/1' && echo "open" || echo "closed or filtered"
bash: connect: Connection refused
bash: line 1: /dev/tcp/127.0.0.1/1: Connection refused
closed or filtered
- If the handshake succeeds, bash exits with 0 and you see
open. - If the port is closed (RST), bash fails immediately.
- If the packets are dropped,
timeoutkills the attempt after 3 seconds and the result is the same failure message.
This works on any system with bash. Note that /dev/tcp is a bash feature and does not exist in sh, dash or
zsh in the same way.
Other tools worth knowing
Besides netcat and nmap, these are useful for connectivity work on Linux:
ss(from iproute2) – the modern replacement fornetstat.ss -tlnplists listening TCP sockets and the owning process, which is useful for checking what is listening locally.curl -v telnet://host:portorcurl -v https://host– checks a port and, for HTTP(S), the full response, including TLS handshake details.traceroute/mtr– shows the path packets take and where they stop.mtrcombines traceroute and ping over time and is excellent for finding where packet loss starts.tcpdump– captures packets so you can see the actual SYN, SYN-ACK, RST and FIN segments described above. Example:sudo tcpdump -i any -nn 'tcp port 443 and (tcp[tcpflags] & (tcp-syn|tcp-fin|tcp-rst) != 0)', shown below as captured duringcurl https://example.com.fping– pings many hosts at once, useful for a quick sweep of a subnet.hping3– crafts custom TCP/UDP/ICMP packets, for example to test whether a firewall rule lets a specific flag combination through.masscan– very fast port scanner for large address ranges. Use with care and permission.socat– a netcat-like tool with more features, such as TLS connections and proxying.
For most day-to-day checks, nc, nmap, ss and tcpdump cover almost everything.
Summary
- ping tests whether a host answers ICMP. It says nothing reliable about TCP ports.
- A TCP handshake (SYN, SYN-ACK, ACK) is what really decides if a port is open. A RST means closed, silence means filtered.
- netcat is quick for single ports, nmap is the tool for ranges and detailed results.
- timeout + /dev/tcp works anywhere bash is available, for remote and local ports alike.
ssshows what is listening locally.
Comments(0)
Join the discussion by logging into your account.
No comments yet. Be the first to comment!