Master Netcat (nc): Install, Test Ports, Transfer Files, and Measure Network Speed
This guide introduces the powerful Netcat (nc) tool, covering installation on CentOS, core syntax and options, practical examples for TCP/UDP port testing, file and directory transfer between servers, and a method to benchmark network throughput using /dev/zero and dstat.
What Is Netcat (nc)?
Netcat, often called the "Swiss army knife" of networking, is a command‑line utility for reading and writing data across network connections using TCP or UDP. It can listen on ports, scan ports, transfer files, and even measure network speed, making it indispensable for operations and troubleshooting.
Installing nc on CentOS 7
yum install -y ncAfter installation, verify the version:
# nc --version
Ncat: Version 7.50 ( https://nmap.org/ncat )Basic Syntax and Common Options
nc [options] hostname/IP portKey options include: -h: display help -v: verbose output -u: use UDP (default is TCP) -z: zero‑I/O mode for scanning -l: listen mode (acts as a server) -w: set timeout in seconds
Environment Setup for Demonstrations
Two test servers are used:
Server 1 – IP 192.168.20.231
Server 2 – IP 192.168.20.232
Both firewalls are disabled for the examples.
1️⃣ Testing TCP/UDP Port Availability
TCP test
On Server 2 start a listener: nc -l 8888 On Server 1 probe the port: nc -vz 192.168.20.232 8888 If the connection succeeds you will see “1 bytes sent, 0 bytes received” and a return code of 0; otherwise “Connection refused” and a return code of 1.
UDP test
On Server 2 start a UDP listener: nc -lu 9999 On Server 1 probe the UDP port: nc -vuz 192.168.20.232 9999 Successful probes display the same “1 bytes sent, 0 bytes received” message.
2️⃣ Transferring Files and Directories with nc
File transfer
# On Server 2 (receiver)
nc -l 9898 > haodao_rece.txt # On Server 1 (sender)
nc 192.168.20.232 9898 < haodao_send.txtThe received file matches the original.
Directory transfer
# On Server 2 (receiver)
nc -l 9898 | tar -xzvf - # On Server 1 (sender)
tar czvf - haodao_test | nc 192.168.20.232 9898The entire directory structure is reproduced on Server 2.
3️⃣ Measuring Network Throughput Between Two Servers
The method streams an infinite zero stream from Server 1 to Server 2 while dstat monitors bandwidth.
# Install dstat on both servers
yum install -y dstat # On Server 2 (receiver)
nc -l 9696 > /dev/null # On Server 1 (sender)
nc 192.168.20.232 9696 < /dev/zeroRun dstat on each server in separate terminals to observe the transfer rates (e.g., ~140 KB received vs. ~110 MB sent on Server 1, and the opposite on Server 2).
Conclusion
Netcat offers a versatile set of functions for port listening, scanning, file transfer, and network performance testing. By mastering its basic commands and options, engineers can streamline many routine operations and troubleshooting tasks.
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.
