Short answer: nc -zv HOST PORT tells you whether a TCP port accepts connections, nc -l -p PORT opens a listener, and whatever you pipe into nc travels over the connection. Which flags work depends on which netcat you have: Ubuntu ships netcat-openbsd, Debian's standard tools bring netcat-traditional, and on Fedora nc is Nmap's Ncat.
nc -zv -w 3 203.0.113.10 443 # is TCP port 443 open?
nc -l -p 8080 # listen on port 8080 (Ctrl+C to stop)
nc -w 3 203.0.113.10 22 # read the server's greeting (banner)
nc -l -p 9000 > backup.tar.gz # receive a file...
nc -N 203.0.113.10 9000 < backup.tar.gz # ...and send it from the other machinenc is not installed on the Debian 12 server this was written on, so every flag and message below is taken from the manuals: netcat-openbsd 1.219 (Debian 12) and 1.229 (Debian 13), netcat-traditional 1.10-47 and Ncat 7.93 on manpages.debian.org, and the Ncat users' guide.
Which nc do you have?
Three different programs answer to nc, and they disagree on several flags. Find out which one runs before you copy a command:
readlink -f "$(command -v nc)" # Debian/Ubuntu: path ends in nc.openbsd or nc.traditional
update-alternatives --display nc # Debian/Ubuntu: every installed netcat and the one in use
nc --version # Ncat answers with its name and version| Variant | Package | Where it is the usual nc |
|---|---|---|
| netcat-openbsd | netcat-openbsd | Ubuntu: a dependency of ubuntu-minimal (24.04), so server and desktop installs have it |
| netcat-traditional | netcat-traditional | Debian 12 and 13: priority standard, installed with the standard system utilities; minimal and cloud images may have no nc at all |
| Ncat (Nmap project) | nmap-ncat (Fedora, RHEL family), ncat (Debian, Ubuntu) | Fedora and the RHEL family: nmap-ncat provides nc (on Fedora it installs both /usr/bin/nc and /usr/bin/ncat) |
On Debian and Ubuntu both netcats can be installed side by side (sudo apt install netcat-openbsd or netcat-traditional). Each registers /bin/nc as an alternative, openbsd with priority 50 and traditional with 10, so openbsd wins once both are there; sudo update-alternatives --config nc switches. On Fedora and the RHEL family, sudo dnf install nmap-ncat.
Test whether a port is open
nc -zv -w 3 203.0.113.10 443 # one TCP port
nc -zv -w 3 203.0.113.10 20-30 # a range (openbsd, traditional), your own servers only
nc -zvu -w 3 203.0.113.10 53 # UDP: always "succeeds", see below-z connects and closes without sending data, -v prints the result and -w 3 stops waiting after three seconds. The netcat-openbsd manual shows the output for a range:
$ nc -zv host.example.com http 20 22-23
nc: connect to host.example.com 80 (tcp) failed: Connection refused
nc: connect to host.example.com 20 (tcp) failed: Connection refused
Connection to host.example.com port [tcp/ssh] succeeded!
nc: connect to host.example.com 23 (tcp) failed: Connection refusedRefused means the host answered and nothing listens there (or a firewall rejects you); a timeout usually means a firewall dropped the packets. What the result means goes through every outcome, with the Windows and bash equivalents. For UDP, the netcat-openbsd manual itself warns that -uz always reports success, whatever the state of the target, so that line proves nothing. The reliable UDP test uses nc on both ends:
nc -u -l -p 51820 # on the server (stop the real service first)
echo hello | nc -u -w 1 203.0.113.10 51820 # on your computerIf hello appears on the server, UDP gets through every firewall on the way in. netcat-traditional takes the same two lines.
Open a quick listener to test a firewall
Before an application exists, a listener shows whether a port is reachable at all. Start it on the server, then test from outside:
nc -l -p 8080 # server: netcat-openbsd or netcat-traditional
ncat -l 8080 # server: Ncat
nc -zv 203.0.113.10 8080 # your computer- Drop
-zon the client and what you type on either side appears on the other, a two-way chat that proves the path works in both directions. - A listener serves one connection and exits.
-kkeeps it listening, in netcat-openbsd and Ncat; netcat-traditional's manual has no-k. - Ports below 1024 need root.
nc -l 127.0.0.1 8080(openbsd) orncat -l 127.0.0.1 8080listens on one address only. - While it runs,
ss -tlnpon the server shows nc holding the port; the netstat and ss guide explains the columns.
On a VPS or dedicated server this tells you, in one minute, whether the port gets past the host firewall and any filter in front of the server. If the connection times out, open the port first, for example with sudo ufw allow 8080/tcp (see the UFW guide), and close it again when the test is done.
Send a file or a whole directory
Start the receiver first, then the sender. The sender needs a way to say "my input has ended", and each netcat spells that differently:
# netcat-openbsd (the manual's recipe)
nc -l -p 9000 > backup.tar.gz # receiver
nc -N 203.0.113.10 9000 < backup.tar.gz # sender: -N shuts the socket at end of file
# netcat-traditional
nc -l -p 9000 > backup.tar.gz
nc -q 1 203.0.113.10 9000 < backup.tar.gz # quit 1 second after end of file
# Ncat (from its users' guide)
ncat -l 9000 > backup.tar.gz
ncat --send-only 203.0.113.10 9000 < backup.tar.gz
# a directory, packed on the fly (netcat-openbsd)
nc -l -p 9000 | tar xzf - # receiver, in the target folder
tar czf - /srv/site | nc -N 203.0.113.10 9000 # senderWithout -N, Debian's netcat-openbsd waits forever after the file ends (the default of its -q option), and Ncat keeps the connection open until the other side closes it. Compare sha256sum backup.tar.gz on both machines afterwards.
Grab a banner and talk to a service
Many services greet you first, and the greeting names the software and often its version, which is worth checking on your own servers. Connect without -z; with netcat-openbsd, -w 3 also ends the session after three idle seconds:
nc -w 3 203.0.113.10 22 # SSH: one line, SSH-2.0- and the server software
nc -C -w 5 203.0.113.10 25 # SMTP greeting: 220 ...; -C sends CRLF line endings
printf 'HEAD / HTTP/1.0\r\n\r\n' | nc -w 5 203.0.113.10 80 # HTTP response headers-C matters for SMTP, POP3 and IMAP, which expect CRLF at the end of each line; all three variants have it. For a full SMTP conversation, the commands are in talking to a mail server with telnet, and they work the same in nc. Which service normally sits on which port is in the common ports list. TLS ports such as 443, 465 or 993 stay silent, because the client has to start the TLS handshake; Ncat can do that with --ssl, the other two cannot.
Flags that differ between the three
| Task | netcat-openbsd | netcat-traditional | Ncat |
|---|---|---|---|
| Listen on a port | nc -l 8080 | nc -l -p 8080 | ncat -l 8080 |
| Listen with -p | Allowed (Debian build) | Required | -p sets the port to bind to |
| Keep listening after a client leaves | -k | Not in its manual | -k, --keep-open |
| Report open or closed only | -z | -z | -z |
| Timeout | -w: connect and idle; ignored with -l | -w: connect and final reads | -w: connect; -i: idle |
| Close after the input ends | -N, or -q 0 | -q SECONDS | --send-only |
| CRLF line endings | -C | -C | -C, --crlf |
| Run a program on connect | No | -e, -c | -e, -c (--sh-exec) |
| TLS | No (Debian build) | No | --ssl |
| Allow or deny clients | No | No | --allow, --deny |
| Through a SOCKS or HTTP proxy | -x with -X 4, 5 or connect | Not in its manual | --proxy |
Two habits keep commands portable: nc -l -p PORT for listeners (netcat-openbsd accepts it, netcat-traditional needs it), and -w on every connecting command, because without it a dropped packet means waiting for the kernel's own TCP timeout.
Frequently asked
How do I check if a port is open with nc?
Run nc -zv -w 3 HOST PORT. -z connects without sending data, -v prints the result and -w 3 gives up after three seconds. A succeeded message means something listens on the port, Connection refused means the host answered but nothing listens, and a timeout usually means a firewall drops the packets.
What is the difference between netcat-openbsd, netcat-traditional and Ncat?
They are three separate programs that can all be called as nc. netcat-openbsd, the usual nc on Ubuntu, has -k, -N and proxy support but no -e. netcat-traditional, which Debian installs with its standard tools, needs nc -l -p PORT to listen and can run a program with -e. Ncat from the Nmap project adds TLS with --ssl and access control with --allow and --deny.
Why does nc not exit after sending a file?
Debian's and Ubuntu's netcat-openbsd keeps waiting after its input ends unless told otherwise. Add -N (or -q 0) on the sending side so it closes the connection at the end of the file. netcat-traditional uses -q 1 for the same job and Ncat uses --send-only.
How do I make netcat listen on a port?
With netcat-openbsd run nc -l 8080 (the Debian and Ubuntu build also accepts nc -l -p 8080), with netcat-traditional nc -l -p 8080, and with Ncat ncat -l 8080. Add -k with netcat-openbsd or Ncat to keep listening after the first client leaves, and allow the port in your firewall to test it from outside.