nc and socat — Three Programs Called nc, and the One That Connects Anything to Anything

ByLinuxtastic

November 29, 2026

You want to know whether a port is reachable from this machine, without installing the client for whatever runs on it. You want to see what a service says when you connect, type a request to it by hand, or stand up a listener for thirty seconds to prove a firewall rule. You want to move a file between two hosts that share nothing but a network. nc does all of that with a socket on one side and your terminal on the other; socat does the same with anything on either side — TLS, Unix sockets, serial ports, another process — which is why it is the one still open when nc has run out.

nc -zv db.internal 5432                        # is the port open from here?
printf 'GET / HTTP/1.0\r\n\r\n' | nc web.internal 80   # talk to it by hand
$ nc -zv 127.0.0.1 8090
Connection to 127.0.0.1 8090 port [tcp/*] succeeded!
$ nc -zv 127.0.0.1 8091
nc: connect to 127.0.0.1 port 8091 (tcp) failed: Connection refused

-z connects and disconnects without sending anything; -v says what happened. The exit status follows the result, so nc -z host port && echo up works in a script. “Connection refused” is a useful answer: the host is up and nothing is listening on that port. Silence until -w runs out is the other answer, and it means a firewall or a dead host — Networking Basics is the page on telling those apart, and tcpdump is how you prove which.

There are three programs called nc

nc is not one tool. It is three unrelated programs that share a name and disagree about their flags, and the one you get depends on the distribution. Debian and Ubuntu install OpenBSD netcat; Fedora, RHEL and their rebuilds install Ncat from the Nmap project as nc; Alpine’s is a BusyBox applet; Arch offers OpenBSD’s and the old GNU one as separate packages. A one-liner copied from the web was written for one of them. The listener syntax is the usual casualty: OpenBSD’s is nc -l 127.0.0.1 9100, GNU’s is nc -l -p 9100, and each rejects the other’s. OpenBSD’s has no -e at all, on purpose; Ncat has -e, --ssl, -k and proxy support that the others lack. The first thing to do on an unfamiliar machine is find out which you have:

$ nc -h 2>&1 | head -1
OpenBSD netcat (Debian patchlevel 1.226-1ubuntu2)
$ readlink -f "$(command -v nc)"
/usr/bin/nc.openbsd

If you need the same behaviour on every machine you touch, install ncat (package ncat or nmap-ncat) and call it by that name; it is the same program everywhere.

TaskOpenBSD nc (Debian, Ubuntu)Ncat (Fedora, RHEL; ncat anywhere)
Port checknc -zv HOST PORTncat -zv HOST PORT
Connect timeout-w SECS-w SECS
Listennc -l ADDR PORTncat -l ADDR PORT
Keep listening after the first clientnot available-k
Close the connection when stdin ends-N (otherwise it waits for the other side)--send-only, or it closes on its own after EOF
UDP-u-u
TLSnot available--ssl, --ssl-verify
Through a proxy-X connect -x PROXY:PORT--proxy PROXY:PORT --proxy-type http
Unix socket-U PATH-U PATH
Run a command on connectnot available-e CMD / -c 'shell line'
Source port / address-p PORT, -s ADDR-p PORT, -s ADDR

Talking to services by hand

$ printf 'GET / HTTP/1.0\r\n\r\n' | nc 127.0.0.1 8090 | head -3
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.13.18
Date: Sun, 29 Nov 2026 17:50:21 GMT

Anything that speaks a text protocol can be driven like this: EHLO to a mail server on 25, PING to Redis on 6379, stats to memcached, a bare newline to SSH to read its version banner. It is the quickest way to find out whether a service is answering as opposed to merely listening, and to see the banner that tells you what version you are dealing with. For TLS services use ncat --ssl or socat‘s OPENSSL: address, below; plain nc to port 443 gets you a handshake failure, not a web page.

A listener, and a file across the gap

# on the receiving host
nc -l 0.0.0.0 9100 > received.tar.gz
# on the sending host
nc -N receiving-host 9100 < backup.tar.gz
# or, with a progress bar and a checksum on each end
tar cz /srv/data | pv | nc -N receiving-host 9100
sha256sum backup.tar.gz received.tar.gz

A listener is also the fastest way to test a firewall rule: nc -l 0.0.0.0 9100 on the server, nc -zv server 9100 from the client, and the answer is in one line, with no application in the way to be blamed. Two things to know. The file transfer is unencrypted and unauthenticated, which is fine on a private network for a one-off and wrong for anything else; rsync over SSH is the normal tool. And on OpenBSD nc, -N is what closes the connection when the file has been sent; without it the sender sits waiting for the other side to hang up, which it never does, and the transfer looks hung at 100%.

Ncat’s -e runs a program and wires its input and output to the connection. It has legitimate uses (a quick echo or test server, a wrapper for a protocol tester) and one infamous one: ncat -l -e /bin/sh is a remote shell for anyone who can reach the port, with no authentication whatsoever. It is the reason OpenBSD’s maintainers removed -e, and it should not appear on any machine that matters, even briefly.

UDP, and what “open” means there

-u switches both tools to UDP. A UDP port check cannot work the way a TCP one does, because there is no handshake: the only sign of a closed port is an ICMP “port unreachable” coming back, and firewalls routinely drop those. So nc -zuv host 514 saying “succeeded” means no refusal arrived, not that anything is listening. On the local machine the refusal does arrive and the check is honest; across a network, send a real datagram the service will answer and look for the answer.

socat: when one end is not a terminal

socat takes two addresses and copies bytes between them in both directions. An address can be a TCP or UDP connection or listener, a Unix socket, a TLS connection, a file, a pipe, a pseudo-terminal, a serial device or a running command, each with its own comma-separated options. That generality is the whole tool, and three uses cover most days.

# 1. a port forward: anything that reaches 8100 here is relayed to 8090 on another host
socat TCP-LISTEN:8100,fork,reuseaddr,bind=0.0.0.0 TCP:10.0.0.5:8090

# 2. talk to a Unix socket as if it were a port (Docker, PostgreSQL, systemd services)
echo hello | socat - UNIX-CONNECT:/run/service.sock
socat TCP-LISTEN:2375,fork,reuseaddr,bind=127.0.0.1 UNIX-CONNECT:/var/run/docker.sock

# 3. a TLS client that checks the certificate, then one that does not
printf 'GET / HTTP/1.0\r\n\r\n' | socat - OPENSSL:host.example:443,cafile=/etc/ssl/certs/ca-certificates.crt,commonname=host.example
printf 'GET / HTTP/1.0\r\n\r\n' | socat - OPENSSL:127.0.0.1:8443,verify=0

fork is the option that makes a listener serve more than one client, and forgetting it is the commonest socat mistake: without it the relay handles one connection and exits. reuseaddr lets you restart it without waiting out TIME-WAIT. -v prints everything that passes through, with direction and timestamps, which turns the port forward into a protocol debugger for any client you cannot otherwise instrument:

$ socat -v TCP-LISTEN:8101,reuseaddr,bind=127.0.0.1 TCP:127.0.0.1:8090
> 2026/11/29 17:50:50.000857624  length=77 from=0 to=76
GET / HTTP/1.1\r
Host: 127.0.0.1:8101\r
User-Agent: curl/8.5.0\r

Two version notes from testing on socat 1.8. UDP listeners want the address family spelled out — UDP4-LISTEN:, not UDP-LISTEN:, which fails with unknown address family when combined with bind=. And OPENSSL: verifies by default and refuses a self-signed certificate; verify=0 is for the lab only, and cafile= with commonname= is how you check a real one, the same job openssl s_client does with more words.

The flags that matter

Flag or optionToolDoes
-znc, ncatScan: connect, report, disconnect. No data
-vallSay what happened. socat -v also dumps the traffic; -d -d is its verbose-logging form
-w SECSnc, ncatConnect timeout, and idle timeout once connected
-lnc, ncatListen. OpenBSD: -l ADDR PORT; GNU: -l -p PORT
-kncatKeep listening after a client disconnects
-Nnc (OpenBSD)Shut down the socket when stdin hits EOF. Essential for file transfers and scripted sends
-unc, ncatUDP
-U PATHnc, ncatUnix socket instead of TCP
-4 / -6allForce IPv4 / IPv6
--ssl, --ssl-verifyncatTLS; the second checks the certificate against the system store
forksocatServe each connection in a child so the listener keeps going
reuseaddrsocatBind even if the port is in TIME-WAIT
bind=ADDRsocatWhich local address to listen on; without it, all of them
verify=0, cafile=, commonname=socatTLS verification off / against a CA file / with a name check
-T SECSsocatInactivity timeout; how a UDP exchange with no EOF ends
EXEC:, SYSTEM:socatA command as one end of the connection (same caution as -e)

Common problems

SymptomCauseFix
nc: invalid option -- 'p' or a listener that connects out insteadA GNU-netcat one-liner on an OpenBSD nc, or vice versanc -h | head -1; use the table above
File transfer stops at 100% and never exitsOpenBSD nc waits for the remote side to close-N on the sender
A piped request gets no reply before nc exitsSome versions close too early on stdin EOF-q 2 (OpenBSD) or omit --send-only (Ncat); or socat
The socat relay served one client and quitNo fork on the listening addressAdd ,fork
Address already in use on restartPrevious socket in TIME-WAIT,reuseaddr (socat); wait sixty seconds (nc)
UDP check says open for a port you know is closedNo ICMP unreachable came back; “open” means “no refusal”Send a real datagram and look for a real answer
socat ... UDP-LISTEN: ... unknown address familysocat 1.8 with bind=UDP4-LISTEN: or UDP6-LISTEN:
TLS connection fails with a certificate error in socatVerification is on by defaultRight fix: cafile= and commonname=; lab only: verify=0
-e “not supported”OpenBSD nc removed itUse ncat, and think twice about why you need it
Listener works from the host, unreachable from elsewhereBound to 127.0.0.1, or a firewall-l 0.0.0.0 / bind=0.0.0.0; then the firewall

Quick reference

nc -h 2>&1 | head -1                               # which nc is this?
nc -zv -w 3 HOST PORT                              # reachable from here?
nc -zv -w 3 HOST 20-25                             # a small range (OpenBSD nc; ncat refuses ranges)
printf 'EHLO me\r\n' | nc -w 3 mail.example 25     # banner and greeting by hand
nc -l 0.0.0.0 9100 > file   /   nc -N HOST 9100 < file     # receive / send a file
ncat --ssl -v HOST 443                             # TLS by hand
nc -U /run/docker.sock                             # a Unix socket
socat TCP-LISTEN:8100,fork,reuseaddr TCP:HOST:80   # port forward
socat -v TCP-LISTEN:8101,fork,reuseaddr TCP:HOST:80   # port forward that shows the traffic
socat - UNIX-CONNECT:/run/service.sock             # talk to a Unix socket
socat - OPENSSL:HOST:443,cafile=/etc/ssl/certs/ca-certificates.crt,commonname=HOST   # verified TLS

Related reading

  • Networking Basics — refused, timed out, and the diagnosis each one starts
  • tcpdump — proving where the packet went when nc -zv says nothing came back
  • ss — the listener you just started, seen from the kernel’s side
  • curl and wget — once the port answers, the HTTP client that speaks the protocol properly
  • openssl — s_client, the fuller TLS conversation
  • The Life of a Unix Socket — what -U and UNIX-CONNECT: are connecting to