Test connection

2 snippets in Bash & Linux

SHBash & Linux

Test Network Connectivity

SH · Networking
Syntax
ping [options] host
Example
ping -c 5 google.com
ping -c 3 192.168.1.1
Output
64 bytes from 142.250.80.46: icmp_seq=1 ttl=118 time=11.2 ms
--- google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss

Note -c limits the number of pings (without it, Linux pings forever; macOS defaults to unlimited too). High time values indicate latency; packet loss indicates connectivity problems.

Netcat: Network Swiss Army Knife

SH · Networking
Syntax
nc [options] host port
Example
nc -zv server.example.com 443
echo 'PING' | nc -w 2 redis.local 6379
nc -l 9090
Output
Connection to server.example.com 443 port [tcp/https] succeeded!

Note -z scans without sending data (port check). -v is verbose. -w sets a timeout in seconds. -l listens on a port (useful for quick debugging). Netcat is invaluable for testing whether a port is open from a particular host.

Frequently asked questions

How do you test connection?
Bash & Linux covers this with 2 copy-ready snippets on this page. The "Test Network Connectivity" snippet in Bash & Linux uses `ping [options] host`.
Which command does the Bash & Linux example use?
The "Test Network Connectivity" snippet uses `ping [options] host`, from the Networking section of the Bash & Linux cheat sheet.
What other Bash & Linux snippets are shown for "test connection"?
Besides "Test Network Connectivity", this page also shows "Netcat: Network Swiss Army Knife".
Is there anything to watch out for?
Yes. For "Test Network Connectivity": -c limits the number of pings (without it, Linux pings forever; macOS defaults to unlimited too). High time values indicate latency; packet loss indicates connectivity problems.