Operations 5 min read

How to Detect Open UDP Ports with hping3 and netcat on Linux

This guide shows how to set up a Ubuntu server and a CentOS client, install hping3 and netcat, and use them to reliably detect whether specific UDP ports are open or closed, interpreting packet loss, ICMP Port Unreachable, and connection refusal messages.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Detect Open UDP Ports with hping3 and netcat on Linux

Test Environment

Server OS: Ubuntu 20.04.6. Client OS: CentOS 7. Tools: hping3 and netcat ( nc). Ensure UDP ports are allowed through the firewall or temporarily disable the firewall.

Server Configuration

List listening UDP ports: netstat -ulnp Typical output shows ports 5353, 37430 and 631 open.

Client Configuration

Install hping3 and nc on the client.

Configure YUM repositories using Aliyun mirrors:

curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

Install the packages:

yum install -y hping3 nc

Testing UDP Ports

Compare detection of open versus closed UDP ports using both hping3 and nc.

Using hping3

Open ports (5353, 37430)

hping3 --udp 192.168.211.201 -p 5353 -c 4
hping3 --udp 192.168.211.201 -p 37430 -c 4

Result: 100 % packet loss with no ICMP error – indicates the port is reachable.

Closed ports (5354, 37431)

hping3 --udp 192.168.211.201 -p 5354 -c 4
hping3 --udp 192.168.211.201 -p 37431 -c 4

Result: 100 % packet loss plus an “ICMP Port Unreachable” message – indicates the port is not listening.

Using netcat (nc)

Open ports

nc -vuz 192.168.211.201 5353
nc -vuz 192.168.211.201 37430

Result: “UDP packet sent successfully”.

Closed ports

nc -vuz 192.168.211.201 5354
nc -vuz 192.168.211.201 37431

Result: “Connection refused”.

Scriptable online/offline check:

nc -vuz 192.168.211.201 5353 < /dev/null &> /dev/null && echo "online" || echo "offline"
nc -vuz 192.168.211.201 5354 < /dev/null &> /dev/null && echo "online" || echo "offline"

Conclusion

Both hping3 and nc can be used to verify UDP port availability. An open UDP port produces no error with hping3 (or a success message with nc), while a closed port generates an ICMP “Port Unreachable” response ( hping3) or “Connection refused” ( nc). The commands above can be reproduced on any Linux system with the same tools installed.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

network troubleshootingLinuxUDPport scanningnetcathping3
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.