When troubleshooting network connectivity or firewall rules between servers, you often need to verify whether a specific TCP or UDP port is open on a remote host. Instead of using telnet (which only supports TCP, does not handle timeouts cleanly in scripts, and is often omitted from minimal Linux installations), you can use Netcat (nc).


Prerequisite: Install Netcat (nc)

On CentOS/RHEL 6 (nc) or CentOS/RHEL 7+ (nmap-ncat), install the nc package if it is not already available:

sudo yum install -y nc

Check a Single TCP Port

Use -z (zero-I/O mode, used for scanning without sending application data), -v (verbose output), and -w <seconds> (connection timeout):

nc -zv -w 3 127.0.0.1 80

Replace 127.0.0.1 with the hostname or IP address of the remote server.


Check a Range of TCP Ports

On traditional OpenBSD/CentOS 6 nc, you can specify a port range (start-end):

nc -zv -w 2 127.0.0.1 20-30

Example Output

[root@server ~]# nc -zv 127.0.0.1 443
Connection to 127.0.0.1 443 port [tcp/https] succeeded!

[root@server ~]# nc -zv 127.0.0.1 443-445
Connection to 127.0.0.1 443 port [tcp/https] succeeded!
Connection to 127.0.0.1 444 port [tcp/snpp] succeeded!
nc: connect to 127.0.0.1 port 445 (tcp) failed: Connection refused

Note on nmap-ncat (RHEL 7/8/9): Newer RHEL releases ship Nmap’s implementation of nc (nmap-ncat), where -z may behave differently across versions and port ranges in the form 443-445 may require a shell brace expansion loop (for p in {443..445}; do nc -zv host "$p"; done) or nmap -p 443-445 host.


Check a UDP Port

Add -u to test UDP reachability (for example, DNS on port 53 or NTP on port 123):

nc -zvu -w 3 192.0.2.10 53

(Because UDP is connectionless, a successful result means no ICMP Port Unreachable packet was returned within the timeout; application-level tools such as dig or chronyc are more definitive for UDP protocols.)


Alternative Port Check Methods (When nc Is Not Installed)

If you are on a locked-down host without nc or telnet, you can test TCP connectivity using built-in tools:

1. Bash /dev/tcp Pseudo-Device

Bash supports opening TCP sockets directly via /dev/tcp/<host>/<port>:

timeout 3 bash -c "</dev/tcp/127.0.0.1/443" && echo "Port 443 is open" || echo "Port 443 is closed"

2. curl (TCP Handshake Test)

curl can test any TCP port using telnet:// or http:// with a connection timeout:

curl -v --connect-timeout 3 telnet://127.0.0.1:443

References