--- title: "Checking for Connectivity on Linux: TCP/IP Basics, Netcat, Nmap and /dev/tcp" author: "Marcus Brosda" published: "2026-11-22T06:52:55.031Z" updated: "2026-11-22T08:04:26.061Z" url: "https://zyvop.com/checking-for-connectivity-on-linux-tcp-ip-basics-netcat-nmap-and-dev-tcp-f3x6k" canonical: "https://zyvop.com/checking-for-connectivity-on-linux-tcp-ip-basics-netcat-nmap-and-dev-tcp-f3x6k" tags: [] --- # Checking for Connectivity on Linux: TCP/IP Basics, Netcat, Nmap and /dev/tcp ![Cover image]() 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? ```bash ping -c 4 example.com ``` Output: ```text 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: ```mermaid 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): ```mermaid 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. ```bash 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: ```text 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`: ```text 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. ```bash 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: ```text 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: ```bash nc -zv -w 2 127.0.0.1 20-25 ``` Output (this netcat prints only the open port in a range): ```text localhost [127.0.0.1] 22 (ssh) open ``` > **Note:** There are several netcat implementations (OpenBSD `nc`, GNU `netcat-traditional`, `ncat` from Nmap). Options differ slightly; check `man nc` on 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: ```bash nmap -p 22,80,443 example.com ``` Output: ```text 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: ```bash 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): ```text $ 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: ```bash timeout 3 bash -c '