Introduction
When verifying connectivity to external servers or databases, which command do you typically use? I'd like to recommend the nc (Netcat) command for this purpose.
Why I Recommend the nc Command
The nc (Netcat) command is convenient for connectivity verification. Here's how to use it:
$ nc -zv <hostname or IP address> <port number>
$ nc -zv google.com 80
If you see Connection to ... succeeded! as output, the connectivity is successful.
user@localhost:~$ nc -zv google.com 80
Connection to google.com (142.250.207.46) 80 port [tcp/http] succeeded!
user@localhost:~$
What About Using ping?
It's common to just try ping first. However, ping uses the ICMP protocol, so if the other side doesn't allow ICMP, it will fail (security groups and firewalls often block it).
Why Not Just Use curl or psql?
Ultimately, it's best to verify that you can properly access the API with curl or the database with psql. However, before that, you want to confirm: "Can we connect at the network level in the first place?"
If you only verify at the application layer, troubleshooting becomes tedious when a connection fails. It's better to use the nc command to verify layer by layer.
What About the Traditional telnet?
While telnet is a classic option, it's often considered a security risk and is not recommended. Personally, I prefer not to use it.
Why nc is Underrated but Practical
- It straightforwardly tells you whether a TCP port is open
- It also supports UDP (surprisingly powerful)
However, nc is not installed by default on systems like Amazon Linux, so install it beforehand:
$ dnf install nmap-ncat
Top comments (0)