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.
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.repoInstall the packages:
yum install -y hping3 ncTesting 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 4Result: 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 4Result: 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 37430Result: “UDP packet sent successfully”.
Closed ports
nc -vuz 192.168.211.201 5354
nc -vuz 192.168.211.201 37431Result: “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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
