$linuxjunkies
>

nc(1)

Read and write data across network connections using TCP or UDP.

UbuntuDebianFedoraArch

Synopsis

nc [OPTION]... [hostname] [port]

Description

nc (netcat) is a versatile networking utility for reading from and writing to network connections. It can operate as a server listening on a port or as a client connecting to remote hosts, supporting both TCP and UDP protocols.

Common uses include port scanning, transferring files, banner grabbing, debugging network services, and creating simple chat or remote shell applications. It reads from stdin and writes to stdout, making it highly composable with other Unix tools via pipes.

Common options

FlagWhat it does
-lListen mode; create a listening socket (server)
-p portSpecify local port to bind to (listen mode) or source port (client mode)
-uUse UDP instead of TCP (default is TCP)
-vVerbose output; print connection details and diagnostic info
-nDo not perform DNS lookups; use only numeric IP addresses
-w timeoutSet timeout in seconds for connections and idle data
-zScan mode; do not send data, only check if port is open
-e programExecute program after connection is established (some systems only)
-q secondsExit after specified seconds of EOF on stdin
-4Force IPv4 connections only
-6Force IPv6 connections only

Examples

Listen on port 8000 for incoming connections; useful for receiving data or testing clients

nc -l 8000

Connect to example.com on port 80; can then type HTTP requests manually or pipe them in

nc example.com 80

Scan ports 1-1000 on example.com to find open ports (port scan)

nc -zv example.com 1-1000

Send a single message to a remote host on port 9000 and close connection

echo 'Hello' | nc example.com 9000

Listen on port 5000 and send the contents of file.txt to any connecting client

nc -l 5000 < file.txt

Listen on port 5000 and extract a gzipped tar archive received from a remote sender

nc -l 5000 | tar xzf -

Listen on port 5353 using UDP protocol instead of TCP

nc -u -l 5353

Connect to example.com port 22 with verbose output and 5-second timeout; useful for SSH banner grabbing

nc -v -w 5 example.com 22

Related commands