netcat

Also known as: nc

On this page 7

netcat, usually invoked as nc, is a command-line utility that opens, listens on and relays raw TCP or UDP connections, copying data between the network and standard input and output. In Linux networking it is often called the network Swiss Army knife, because one small binary can act as a client, a server, a port scanner or a pipe between two hosts.

Administrators reach for netcat when they need to answer a basic question quickly: can this host reach that port, and what does the service send back. It does not understand application protocols, which is both its strength and its main limitation: it shows exactly what travels over the socket, with no interpretation, no encryption and no retry logic unless a particular variant adds them.

How netcat moves data between sockets

netcat works by connecting standard streams to a network socket. In client mode it opens a connection to a given host and port, sends everything it reads from standard input and prints everything it receives. In listen mode, enabled with -l, it binds to a local port, waits for one peer and then performs the same bidirectional copy.

Because it relies on ordinary Unix pipes, netcat composes naturally with other tools. Redirecting a file into nc sends the file, redirecting its output to a file saves what arrives, and piping it through tar or gzip turns it into an ad hoc transfer channel. The default transport is TCP; the -u option switches to UDP, where there is no handshake and no delivery guarantee.

The following table lists the options that appear most often in daily administration. Exact flags differ between implementations, so the manual page of the installed variant is the final reference.

OptionPurposeTypical use
-lListen for an incoming connectionTemporary test server
-p portSet the local source or listening portOlder variants in listen mode
-uUse UDP instead of TCPTesting DNS or syslog paths
-zScan without sending dataChecking whether ports are open
-vPrint verbose connection statusSeeing refused or timed-out attempts
-w secondsSet a connect or idle timeoutScripts that must not hang
-kKeep listening after a client leavesRepeated tests on one port
Common netcat options and their effect

netcat implementations on Linux distributions

Several independent implementations share the netcat name, and the differences matter in scripts. The original traditional netcat from 1995 introduced the tool, the OpenBSD netcat rewrite added IPv6, proxy support and Unix domain sockets, and Ncat from the Nmap project added TLS, access control and connection brokering. Many distributions ship one of these as nc through an alternatives system.

The comparison below helps when a command copied from documentation behaves unexpectedly. A frequent surprise is that the `-e` option is missing in the OpenBSD variant, which deliberately removed program execution for security reasons.

VariantKey capabilitiesNotable difference
Traditional netcatTCP, UDP, basic scanningHas -e in some builds, no IPv6
OpenBSD netcatIPv6, proxies, Unix socketsNo -e, uses -l without -p
NcatTLS, access lists, brokeringLarger, part of the Nmap suite
BusyBox ncMinimal client and listenerReduced option set for embedded systems
Main netcat variants compared

netcat sits at the transport layer, which separates it from tools that either observe traffic or configure interfaces. It creates its own connection, whereas tcpdump captures packets that other programs send, and the ip command or nmcli change addresses and routes rather than exchanging data.

The table below places netcat next to tools an administrator may confuse it with. Choosing the right one saves time: netcat answers reachability questions, while packet capture explains why a connection fails at a lower level.

ToolMain roleWhen to prefer it
netcatRaw TCP or UDP client and serverQuick port and payload tests
socatBidirectional relay with many address typesComplex forwarding, TLS, serial lines
telnet clientInteractive TCP clientLegacy systems without nc
tcpdumpPacket capture and filteringInspecting handshakes and drops
ssSocket state listingSeeing which ports are already bound
netcat and neighboring tools

Practical uses in Linux administration

The most common task is a port reachability check: nc -vz host 443 reports whether a TCP handshake succeeds. If it does, routing and firewall rules on the path allow the connection, and any remaining failure lies in the application. A connection refused reply means the host answered but nothing listens, while a timeout usually points to a firewall that silently drops packets, for example an nftables or firewalld rule.

netcat is equally useful for speaking text protocols by hand. Typing an HTTP request line or an SMTP greeting into a connection shows the raw server response, including headers that a browser would hide. For one-off file transfers inside a trusted network, a listener writing to a file and a sender reading from one move data at line speed without configuring a file service.

The decision table maps frequent administrative questions to a suitable netcat approach.

Tasknetcat approachReason
Check if a firewall allows a portnc -vz -w 3 host portHandshake test with a timeout
Verify a service bannernc host port and read outputShows the raw greeting
Test a UDP pathListener with -u on both endsConfirms datagrams arrive
Move a directory oncetar piped into ncNo server setup required
Simulate a backendListener with -kLets clients connect repeatedly
Choosing a netcat approach by task

Security risks and common mistakes with netcat

netcat transfers everything in plain text, so passwords, tokens and file contents are readable by anyone who can capture the traffic. For sensitive data the safer options are Ncat with TLS, an SSH tunnel or a dedicated tool such as scp. A forgotten listener is also an open, unauthenticated port that accepts data from any peer allowed by the firewall.

Attackers value netcat for the same reasons administrators do. Its ability to attach a shell to a socket makes it a classic tool for reverse and bind shells, which is why some distributions ship variants without -e and why security teams treat unexpected nc processes as a signal worth investigating.

  • UDP scans are unreliable: an open UDP port often sends no reply, so silence does not prove the port is closed.
  • Missing timeouts make scripts hang when a peer never closes the connection; add -w or -q as the variant allows.
  • Flag syntax differs between variants, so a command that works on one distribution may fail on another.
  • A successful handshake does not prove the application works, only that the transport path and listener exist.

A non-technical view

Think of netcat as a plain telephone line between two computers. One side dials a number, the other side picks up, and whatever is said on one end comes out on the other, word for word. Nobody translates, checks or encrypts the conversation.

Technicians use it to find out whether a line can be connected at all and what the other side says when it answers. It is simple, fast and present on almost every Linux system, but because the line is not private, it should not carry secrets.

A concrete example

A web application on a Linux server suddenly cannot reach its PostgreSQL database on another host, port 5432. The administrator runs nc -vz -w 3 db01 5432 from the application server and receives a timeout after three seconds. Running the same command from the database host itself succeeds, so the service is running and listening.

The timeout, rather than a refusal, suggests that packets are being dropped on the way. The administrator checks the database host and finds that a recent change to the nftables ruleset allowed port 5432 only from the old application subnet. After the rule is corrected, the netcat check connects immediately, and the application recovers. Two short netcat tests narrowed the fault to a firewall rule in a few minutes, without touching the database configuration.