Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

netcat: test ports and data streams

28 min read
Improve this page

A service does not answer, and you cannot tell whether the port is closed, blocked, or open with a service that stays silent. netcat (the nc command) opens a TCP or UDP connection and plugs your terminal into it: what you type goes out on the network, and what comes back is displayed. It tests a port, reads a refusal and sends a file, with nothing to install on the server except nc itself.

This page teaches these tasks with two machines, checking each time what actually arrived. For the difference between TCP and UDP, see TCP vs UDP (in French); for a complete diagnosis method, the network diagnosis methodology (in French). To scan a whole network or detect versions, see nmap; for an encrypted transfer, scp.

What you will learn

  • Put a machine in listening mode with nc -l, and connect to it from another one
  • Test a port with -z, -v and -w, and read a refusal or a wait
  • Send a file with -N and check its checksum on both sides
  • Understand why a -z test uses up the listener, and when to keep the listener open with -k
  • Test a UDP port, knowing what the result proves
  • Read a service banner and send an HTTP request by hand
  • Choose between OpenBSD netcat and ncat, and know the security limits of nc

Prerequisites

You know how to open two terminals, move around with cd, redirect with < and > and connect to a server over SSH. You need a server srv reachable with ssh srv without a password (SSH key, ssh-copy-id srv, see scp). Work with an ordinary account and ports above 1024.

nc does not read ~/.ssh/config: the name srv must resolve through DNS or /etc/hosts. Without a server of your own, your own machine can play that role: add the name srv to it with the command below, and remove the line during the final cleanup. Avoid localhost: nc tries ::1 (IPv6) first and then 127.0.0.1, and prints two refusals instead of one.

Terminal window
echo '127.0.0.1 srv' | sudo tee -a /etc/hosts

Install netcat-openbsd on the workstation and on the server (Debian 13, Ubuntu 26.04), then check the variant:

Terminal window
sudo apt install netcat-openbsd
nc -h 2>&1 | head -1
Output, Debian 13 workstation
OpenBSD netcat (Debian patchlevel 1.229-1)

The first line names the variant and its version: 1.229 on Debian 13, 1.234 on Ubuntu 26.04. On AlmaLinux, the package is called nmap-ncat and the nc command is ncat, whose options and messages differ: the examples on this page apply to OpenBSD netcat.

Create the test folder

The examples use ~/nc-lab locally and a single folder on the server, nc-lab-srv, created first: if it already exists, the command stops before touching anything. The final cleanup deletes only these two folders. a.txt contains one line.

Terminal window
ssh srv mkdir nc-lab-srv && mkdir ~/nc-lab && cd ~/nc-lab && printf 'bonjour\n' > a.txt

The outputs come from a Debian 13 workstation talking to an Ubuntu 26.04 server; (IP) stands for the server's address, you will read your own.

The netcat command in detail

A client-side nc command is written in this order: the options, the host, then the port. Read the line from left to right: each number points to the explanation of that part.

Anatomy of the command

Syntaxnc [options] host port

  1. nc the programIt opens a connection and passes bytes through it.
  2. -z the testoptionalConnect without sending anything.
  3. -v the detailsoptionalDisplay the result of the connection.
  4. -w 2 the waitoptionalGive up after 2 seconds.
  5. srv the hostA name or an address.
  6. 22 the portFrom 1 to 65535.

OutputConnection to srv 22 port [tcp/ssh] succeeded!

The two operating modes

A complete test involves two machines: one listens with -l, the other connects. Without -l, nc is a client; with -l, it waits for a connection, accepts only one, then stops and frees the port.

On srv, start a listener on port 9000 that writes whatever arrives into recu.txt. ssh -f sends ssh to the background to free the terminal, timeout 8 stops nc after 8 seconds, and sleep 1 gives it time to start. Then look at what is listening:

Terminal window
ssh -f srv "cd nc-lab-srv && timeout 8 nc -l 9000 > recu.txt"; sleep 1
ssh srv "ss -ltn | grep 9000"
Output
LISTEN 0 1 0.0.0.0:9000 0.0.0.0:*

ss lists the machine's network connections: -l keeps only listening ports, -t TCP, and -n prints numbers instead of names.

The column that matters is the listening address: 0.0.0.0:9000 means the port is open on all the interfaces of srv, so it can be reached from the network. To limit the listener to one interface, write its address before the port: nc -l 127.0.0.1 9000.

Within the 8 seconds, send a.txt from the workstation. < hands the content of the file to nc, -N closes the connection once the file is sent, -w 2 gives up after 2 seconds of inactivity:

Terminal window
nc -N -w 2 srv 9000 < a.txt; echo "code $?"
sleep 1; ssh srv "wc -l < nc-lab-srv/recu.txt"
Output
code 0
1

Code 0 says the send did not fail; wc -l counts the lines of recu.txt on the server: one line arrived. The connection was served, and the listener stopped by itself.

Scan ports

The -z option opens the connection then closes it without sending any data; -v prints the result. Test the SSH port, which is open, then port 9001, where nothing listens:

Terminal window
nc -zv -w 2 srv 22
nc -zv -w 2 srv 9001
Output
Connection to srv (IP) 22 port [tcp/ssh] succeeded!
nc: connect to srv (IP) port 9001 (tcp) failed: Connection refused

succeeded! says that something accepted the connection. Connection refused says that the machine answered immediately and refused: either nothing listens on that port, or a firewall rejects the connection (see the diagnosis methodology (in French)). Without -v, the refusal is silent: only the return code reports it.

Terminal window
nc -z -w 2 srv 9001; echo $?
Output
1

Third case: a firewall drops the packets, nothing comes back, and nc gives up at the end of -w. The address 192.0.2.1 is reserved for documentation: nobody answers there, so you can replay this case.

Terminal window
nc -zv -w 3 192.0.2.1 80
Output, after 3 seconds
nc: connect to 192.0.2.1 port 80 (tcp) timed out: Operation now in progress

timed out means "no answer" (packets dropped or machine unreachable), refused means "negative answer".

To test several ports, separate them with spaces or give a range with a dash; the comma is rejected (nc: port number invalid: 22,80). Here is the range 20 to 25:

Terminal window
nc -zv -w 2 srv 20-25
Output of the range, tested port by port
nc: connect to srv (IP) port 20 (tcp) failed: Connection refused
nc: connect to srv (IP) port 21 (tcp) failed: Connection refused
Connection to srv (IP) 22 port [tcp/ssh] succeeded!
nc: connect to srv (IP) port 23 (tcp) failed: Connection refused
nc: connect to srv (IP) port 24 (tcp) failed: Connection refused
nc: connect to srv (IP) port 25 (tcp) failed: Connection refused

File transfer

The transfer relies on shell redirections: > on the receiving side writes what arrives, < on the sending side sends the content of the file. Start the listener before sending, otherwise the sender gets a refusal.

Code 0 and a wc -l do not prove that the file is identical. For a binary file, compare the size and the checksums on both sides: sha256sum computes a checksum, a hexadecimal number that changes if a single byte changes, and cut -c1-16 keeps only the beginning for readability. This record covers 100,000 random bytes: your values will differ, only the equality matters.

Terminal window
head -c 100000 /dev/urandom > gros.bin
ssh -f srv "cd nc-lab-srv && timeout 8 nc -l 9002 > gros.bin"; sleep 1
nc -N srv 9002 < gros.bin; sleep 1
sha256sum gros.bin | cut -c1-16
ssh srv "cd nc-lab-srv && sha256sum gros.bin | cut -c1-16; wc -c < gros.bin"
Output
b258464ae02d1116
b258464ae02d1116
100000

The first line is the local checksum, the next two come from the server: the checksum is the same and the size is 100,000 bytes, so the transfer is complete. Nothing in nc checks integrity: it is up to you to compare the size and the checksum.

Without -N, the client does not give the prompt back after sending, because it waits for the server to close the connection. timeout 4 stops it after 4 seconds with code 124:

Terminal window
ssh -f srv "cd nc-lab-srv && timeout 8 nc -l 9040 > n.txt"; sleep 1
timeout 4 nc srv 9040 < a.txt; echo $?
Output, after 4 seconds
124

How netcat connects, step by step

Predict before you look at the map. Case 1: a listener with nc -l, then sending a.txt, with no test. Case 2: the same listener, a test nc -zv -w 2 srv 9000, then sending a.txt. What does recu.txt contain at the end of each case, and what does nc return on the client side? Then check: tick nc -l and -z in the options, then look at the state of port 9000 at each step and at the client screen.

Mental model

How two netcats talk

a server listensa client connectsbytes flow, then everything stops a single connection per nc -l

$ ssh -f srv "timeout 8 nc -l 9000 > nc-lab-srv/recu.txt"; sleep 1; nc -N -w 2 srv 9000 < a.txt; echo "code $?"; sleep 1; ssh srv "wc -l < nc-lab-srv/recu.txt"

  1. The server opens port 9000 and waits for a single connection.
  2. No test: the port keeps listening for the transfer.
  3. The client sends a.txt. The server receives one line and stops.
  4. One line received on the server.

What the animation shows

  1. Two sides: nc -l listens, nc connects.
  2. A single connection: nc -l stops after the first one.
  3. Testing uses up the connection: -z counts as one.
  4. Nothing is encrypted, and anyone can connect to the open port.

Golden rules

Test a port without sending anything

-w limits the wait:

nc -zv -w 2 srv 22

Check what arrived

Compare the hashes on both sides:

sha256sum a.txt

Essential vocabulary

The client · connect and test

-zv
An open portnc -zv -w 2 srv 22 → succeeded
-zv
A closed portnc -zv -w 2 srv 9001 → Connection refused
-z
The code onlync -z -w 2 srv 9001; echo $? → code 1
nc srv
Without -v, nothing is displayednc -N -w 2 srv 9001 < a.txt; echo $? → code 1

The server · listen

-l
Send a filessh -f srv "timeout 8 nc -l 9000 > nc-lab-srv/recu.txt"; sleep 1; nc -N -w 2 srv 9000 < a.txt; sleep 1; ssh srv "wc -l < nc-lab-srv/recu.txt" → 1 line received
-u
Over UDPssh -f srv "timeout 8 nc -u -l 9004 > nc-lab-srv/recu.txt"; sleep 1; nc -u -w 1 srv 9004 < a.txt; sleep 1; ssh srv "wc -l < nc-lab-srv/recu.txt" → 1 line received
-l
A single connectionssh -f srv "timeout 8 nc -l 9000 > nc-lab-srv/recu.txt"; sleep 1; nc -z srv 9000; sleep 1; nc -z srv 9000; echo $? → a success, then code 1
-w
A limited waitnc -w 2 srv 9001; echo fin → fin
The port nobody can reach Exercise 1 of 2 · ~8 min · terminal

A colleague started a listener on port 9007 of srv and asks you to send b.txt (three lines) to it. From your workstation, the connection fails. Your job: find out why, then get the three lines to arrive.

Prepare the file and the colleague's listener

Create b.txt with three lines (un, deux, trois). On srv, start the colleague's listener in nc-lab-srv, with timeout 60: nc -l 127.0.0.1 9007.

Test the port from the workstation

Test port 9007 of srv with -zv and read the return code.

Look on the server side

On srv, list the listening TCP ports and find the line for 9007.

Fix and send

Stop the colleague's listener, start one that the workstation can reach, writing into b.txt on the server, send b.txt and count the lines received.

A hint 1 / 1

Compare what the workstation sees (a refusal) with what the server sees: the listening address column.

I have tried: show the solution
Terminal window
printf 'un\ndeux\ntrois\n' > b.txt
ssh -f srv "cd nc-lab-srv && timeout 60 nc -l 127.0.0.1 9007"; sleep 1
nc -zv -w 2 srv 9007; echo $?
ssh srv "ss -ltn | grep 9007"
Output
nc: connect to srv (IP) port 9007 (tcp) failed: Connection refused
1
LISTEN 0 1 127.0.0.1:9007 0.0.0.0:*

The refusal looks like a closed port, but the port is open: the listener is bound to 127.0.0.1, the machine's own address, so only connections made from srv itself get in. To fix it, stop the listener and start it again without an address (so on 0.0.0.0):

Terminal window
ssh srv "pkill -x nc"
ssh -f srv "cd nc-lab-srv && timeout 60 nc -l 9010 > b.txt"; sleep 1
nc -N srv 9010 < b.txt; echo $?; sleep 1; ssh srv wc -l nc-lab-srv/b.txt
Output
0
3 nc-lab-srv/b.txt

The three lines arrived. When ss -ltn shows 127.0.0.1 in front of the port, the service cannot be reached from the network.

Single connection

After the first connection, nc -l stops, even if that connection carried nothing: this is case 2 of the map, where the -z test uses up the listener. The -k option keeps the listener open for several connections. Here, two sends in a row are received by the same listener:

Terminal window
ssh -f srv "cd nc-lab-srv && timeout 8 nc -k -l 9000 > k.txt"; sleep 1
nc -N srv 9000 < a.txt; nc -N srv 9000 < a.txt; sleep 1; ssh srv wc -l nc-lab-srv/k.txt
Output
2 nc-lab-srv/k.txt

Both lines are in k.txt. With -k, a -z test no longer uses up the listener: two tests in a row each print succeeded! and return 0.

UDP protocol

The -u option switches to UDP, used for instance by DNS. UDP has no handshake: there is no connection to accept. Each attempt below starts a fresh listener, because a UDP listener that has already been used no longer behaves like the original. First send a line to a UDP listener:

Terminal window
ssh -f srv "cd nc-lab-srv && timeout 8 nc -u -l 9004 > udp.txt"; sleep 1
nc -u -w 1 srv 9004 < a.txt; echo $?; sleep 1; ssh srv wc -l nc-lab-srv/udp.txt
Output
0
1 nc-lab-srv/udp.txt

The line arrived. Start a fresh listener and this time test with -z, then test a UDP port where nothing listens (9005), with and without -z:

Terminal window
ssh -f srv "cd nc-lab-srv && timeout 8 nc -u -l 9004 > udp.txt"; sleep 1
nc -zvu -w 1 srv 9004; echo $?
nc -zvu -w 1 srv 9005; echo $?
nc -u -w 1 srv 9005 < a.txt; echo $?
Output
Connection to srv (IP) 9004 port [udp/*] succeeded!
0
1
0

On an open UDP port, -z prints succeeded! and returns 0. On a closed port, it prints nothing and returns 1: that is the only sign, as with a TCP refusal without -v.

Without -z, the send always returns 0, whether the port is open or closed: nc -u never says whether the data arrived. Likewise, with no handshake, succeeded! means that no refusal came back, not that a service answered: to know whether a UDP service answers, send it a real request and look at what comes back.

The transfer that stops halfway Exercise 2 of 2 · ~8 min · terminal

A colleague sends gros.bin (100,000 bytes) to srv. During the transfer, the server's listening program is stopped. On the workstation side, nc prints no error. You have to check whether the file is complete, then make the send succeed.

Prepare the listener

Reuse the gros.bin from the "File transfer" section. On srv, start in nc-lab-srv a listener on port 9021 that writes into coupe.bin, with timeout 60.

Send with an interruption

Run the command below: it sends the beginning of the file, waits 3 seconds, then sends the rest, while another command stops the server's nc processes after 1.5 seconds. Your shell may print the number of the background job.

Terminal window
(head -c 40000 gros.bin; sleep 3; tail -c +40001 gros.bin) | nc -N srv 9021 & sleep 1.5; ssh srv pkill -x nc; wait

Compare

Read the size of coupe.bin on the server with stat -c %s, then compare the checksums of gros.bin (workstation) and coupe.bin (server).

Send it in full

Start a listener on port 9020 writing into gros.bin on the server, send the file and compare the full checksums.

A hint 1 / 1

The size can be read at a glance. The full checksum comes from sha256sum without cut.

I have tried: show the solution
Terminal window
ssh -f srv "cd nc-lab-srv && timeout 60 nc -l 9021 > coupe.bin"; sleep 1
(head -c 40000 gros.bin; sleep 3; tail -c +40001 gros.bin) | nc -N srv 9021 & sleep 1.5; ssh srv pkill -x nc; wait
ssh srv stat -c %s nc-lab-srv/coupe.bin
Output
40000

A Terminated line followed by the listener command may be printed before the size: it is the server's shell reporting the stop. The server received only 40,000 bytes out of 100,000, and the sending nc reported nothing: only the size shows it, and the checksum of coupe.bin cannot be that of gros.bin. To succeed, start the listener again and send the whole file again:

Terminal window
ssh -f srv "cd nc-lab-srv && timeout 60 nc -l 9020 > gros.bin"; sleep 1
nc -N srv 9020 < gros.bin; echo $?; sleep 1; sha256sum gros.bin; ssh srv sha256sum nc-lab-srv/gros.bin
Output, sample values
0
b3da4adcca5532e1302cba27716b22da8663215ad2511c283bbf07a5b7ecd642 gros.bin
b3da4adcca5532e1302cba27716b22da8663215ad2511c283bbf07a5b7ecd642 nc-lab-srv/gros.bin

The two checksums are identical: the file is complete. A code 0 and the absence of a message never prove a complete transfer.

Advanced use cases

Once the connection is established, nc transmits what you give it and prints what comes back. These cases serve diagnosis: exchanging in both directions, reading what a service says, speaking HTTP by hand or simulating a web server. The last one flags a usage to recognize, not to copy.

Exchange in both directions

An nc connection is bidirectional: the server can send a file while it receives one. Here, srv sends q.txt to the client (<) and writes what it receives into c.txt (>):

Terminal window
ssh srv 'printf "recu par le serveur\n" > nc-lab-srv/q.txt'
ssh -f srv "cd nc-lab-srv && timeout 8 nc -l 9030 < q.txt > c.txt"; sleep 1
nc -N -w 3 srv 9030 < a.txt; sleep 1; ssh srv cat nc-lab-srv/c.txt
Output
recu par le serveur
bonjour

The first line is printed by the client: it is what the server sent. The second comes from c.txt: it is what the server received from the client.

Grab a service banner

Many services introduce themselves as soon as you connect: this is banner grabbing (in French). SSH speaks first, so you only need to connect without sending anything (< /dev/null):

Terminal window
nc -v -w 2 srv 22 < /dev/null
Output
Connection to srv (IP) 22 port [tcp/ssh] succeeded!
SSH-2.0-OpenSSH_10.2p1 Ubuntu-2ubuntu3.8

The second line is the SSH server's own (software name and version). It says nothing reliable about the configuration. Do not send an empty line with echo "": the SSH server reads it as an invalid client banner and answers Invalid SSH identification string.

HTTP server and request

nc does not understand HTTP: it sends back the text you give it. On srv, a file containing a status line, an empty line and then the body is enough for a client to receive an answer. This is useful to check that a probe or a firewall reaches the machine. The port is open to everyone as long as the command runs, and it serves only one request.

Start the listener on srv, then query it from the workstation with curl (-m 5 gives up after 5 seconds, -s hides the progress bar):

Terminal window
ssh srv 'printf "HTTP/1.1 200 OK\r\n\r\nbonjour\n" > nc-lab-srv/rep.txt'
ssh -f srv 'timeout 20 nc -N -l 8080 < nc-lab-srv/rep.txt' > /dev/null; sleep 1
curl -m 5 -s http://srv:8080/; echo $?
Output
bonjour
0

Without -N on the server side, curl also prints bonjour, but the connection stays open: it waits 5 seconds and returns code 28 (timeout). -N closes the connection after the file is sent. The > /dev/null hides curl's request, which nc would otherwise print in your terminal.

In the other direction, nc plays the client and sends an HTTP request written by hand. Start a Python web server on srv (timeout 20 stops it by itself), then ask it for rep.txt from the workstation:

Terminal window
ssh -f srv 'cd nc-lab-srv && timeout 20 python3 -m http.server 8090' > /dev/null 2>&1; sleep 2
printf 'GET /rep.txt HTTP/1.0\r\n\r\n' | nc -N -w 2 srv 8090
Output, the date and the Python version will be yours
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.14.4
Date: Wed, 25 Nov 2026 14:31:05 GMT
Content-type: text/plain
Content-Length: 27
Last-Modified: Wed, 25 Nov 2026 14:30:56 GMT
HTTP/1.1 200 OK
bonjour

The lines before the empty line are the headers sent by the Python server; what follows is the content of rep.txt, 27 bytes.

Command execution (LAB only)

The -e option of netcat-traditional, and -e or -c of ncat, run a program on each connection: whoever connects gets a shell on the listening machine. It does not exist in OpenBSD netcat, and this page does not teach it. An nc -l with -e or -c in a history or a process list is a sign to investigate, not an administration tool.

No encryption

nc copies bytes, without encrypting or authenticating: everything you send can be read by whoever captures the traffic on the path, and anyone can connect to a listening port (the 0.0.0.0 in the ss output above). For a file that matters, use scp, or ncat --ssl which adds TLS; for a connection to a local service, an ssh -L tunnel encrypts the path. To see what nc really sends, capture with tcpdump.

Privileged ports

Listening on a port below 1024 is reserved for root. With an ordinary account, nc -l 80 stops immediately: choose a port above 1024, as the examples on this page do.

Terminal window
ssh srv 'timeout 3 nc -l 80; echo $?'
Output
nc: Permission denied
1

Netcat variants

Several programs answer to the nc or ncat command, and their options differ. On Debian and Ubuntu, netcat-openbsd is the package used on this page; on AlmaLinux, the nmap-ncat package provides ncat.

OpenBSD netcatncat (package nmap-ncat)
Listennc -l 9000ncat -l 9000
Stay open after a connection-k-k
TLS encryptionnoyes, dedicated option
Accept only certain addressesno--allow
Run a program on connectionno-e and -c

netcat-traditional, which Debian and Ubuntu offer separately, needs -p in front of the listening port (nc -l -p 9000) and provides the -e option. The examples on this page use OpenBSD netcat: on another variant, check the options with nc -h. A traditional variant prints [v1.10-47] instead of the OpenBSD name.

Which command should you choose?

nc suits one-off tests. For everything else, use the tool in the table.

NeedCommand
Test a port, read a refusal, send datanc
Scan a range, detect services and versionsnmap
See what listens on the machiness -ltn
HTTP request or downloadcurl
Send an encrypted filescp
Watch the packets exchangedtcpdump

Troubleshooting

Start by reading the message with -v, then check on the server side with ss -ltn whether something really listens on the port, and on which address.

SymptomCauseSolution
Connection refused, immediateNothing listens, the listener has already served its connection, it is bound to 127.0.0.1, or a firewall rejectsss -ltn on the server, restart the listener, check the firewall
timed out: Operation now in progressPackets dropped (firewall) or machine unreachableCheck the firewall rules on srv, test with -w
Nothing printed, code 1Refusal without -vAdd -v, or read echo $?
Send with no visible error, empty or truncated fileListener used up by a -z test, or stopped midwayRestart the listener, compare size and checksum
nc does not give the prompt back after sending-N missingAdd -N
Unknown option, or -l without effectDifferent variantnc -h 2>&1 | head -1, adapt the syntax

Clean up the test folder

Stop the listeners that are still active, then delete the two test folders, the only ones the page created. With the name srv added to /etc/hosts, nc-lab-srv is in your home directory: re-read the name before running rm -r.

Terminal window
cd ~
ssh srv "pkill -x nc; rm -r nc-lab-srv"
rm -r ~/nc-lab

If you added the line 127.0.0.1 srv to /etc/hosts, remove it:

Terminal window
sudo sed -i '/^127.0.0.1 srv$/d' /etc/hosts

Key points

  • nc -l PORT listens, nc HOST PORT connects; nc -l serves only one connection then stops, unless you use -k.
  • -z tests a port without sending anything, but uses up the single connection of a listener: the next send is refused without a message.
  • Connection refused is an immediate answer (nothing listens, or a firewall rejects); timed out is silence. Without -v, a refusal is silent, only code 1 reports it.
  • -N closes after sending, -w limits the wait. Compare size and checksums on both sides: nc checks nothing.
  • In UDP, -z prints succeeded! on an open port and returns 1 without a message on a closed port; an nc -u send always returns 0.
  • Nothing is encrypted and the port is open to everyone: for a file that matters, use scp.

Knowledge check

Check that the essentials of this guide stuck. The questions cover only what has just been explained here.

Knowledge check

Check what you have learned with this interactive quiz

10 questions
6 min
80% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer

FAQ: frequently asked questions about netcat