How to Check Ports in Linux Using /dev/tcp and /dev/udp (Without Telnet or Netcat)

Checking if a port is open on a remote server is a common task for system administrators. Although tools like telnet or netcat (nc) are often used for this purpose, Bash offers a built-in alternative that requires no additional installation: using /dev/tcp/ and /dev/udp/.

In this article, you’ll learn how to use these built-in Bash features to check TCP and UDP ports quickly, simply, and without external dependencies.

What is /dev/tcp/host/port and /dev/udp/host/port?

Bash has a hidden feature that allows using special redirections to create network connections. These paths (/dev/tcp/ and /dev/udp/) do not exist in the filesystem, but Bash internally interprets them to open sockets.

echo > /dev/tcp/host/port

This attempts to open a TCP connection to the specified host and port.
Check a TCP Port from the Command Line

To test if a port is open, simply run:

echo > /dev/tcp/google.com/80 && echo "Port open" || echo "Port closed"

What does this do?

If the connection opens successfully, Bash proceeds and displays “Port open”.
If it fails (e.g., the port is closed or the host doesn’t respond), it shows “Port closed”.

With timeout

To prevent the command from hanging if the host doesn’t respond, you can use timeout:

timeout 3 bash -c "echo > /dev/tcp/google.com/80" && echo "Open" || echo "Closed or Timeout"

🛈 Requires Bash with /dev/tcp redirection support. Doesn’t work in shells like sh or dash.

Check a UDP Port with /dev/udp

Usage for UDP is similar:

echo > /dev/udp/8.8.8.8/53 && echo "Sent" || echo "Failed"

But there’s an important detail: UDP doesn’t guarantee delivery or response, so this method doesn’t truly confirm if the port is open.

How to receive a response?

You can use a file descriptor to send and then try to read:

exec 3<> /dev/udp/8.8.8.8/53 echo -ne '\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00' >&3 read -t 2 -u 3 response && echo "Response: $response" || echo "No response"

This example sends an empty DNS query to port 53 and waits for a response.
Method Limitations

Feature TCP UDP
Detect if the port is open Reliable Unreliable
Requires external tools No No
Receives service response Yes (if the service replies) Only if a valid response is expected

What if I want something more reliable for UDP?

If you need to check whether a UDP port is truly open and responsive, it’s better to use tools like:

nc -uzv # netcat in UDP mode nmap -sU -p # UDP scan with Nmap

Using /dev/tcp/ and /dev/udp/ is a great way to quickly check ports without relying on external tools. It’s especially useful in minimalist environments or automated scripts.

Quick summary:

✅ TCP: Works great for checking open ports.
⚠️ UDP: Can send datagrams but doesn’t guarantee knowing if the port is open.

🧰 You don’t need to install anything if you have Bash.

Have you already tried it in your scripts? Leave us your questions or share how you use it.

Leave a Reply