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,-vand-w, and read a refusal or a wait - Send a file with
-Nand check its checksum on both sides - Understand why a
-ztest 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 ofnc
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.
echo '127.0.0.1 srv' | sudo tee -a /etc/hostsInstall netcat-openbsd on the workstation and on the server (Debian 13, Ubuntu 26.04), then check the variant:
sudo apt install netcat-openbsdnc -h 2>&1 | head -1OpenBSD 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.
ssh srv mkdir nc-lab-srv && mkdir ~/nc-lab && cd ~/nc-lab && printf 'bonjour\n' > a.txtThe 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
nc-z-v-w 2srv22 -
ncthe programIt opens a connection and passes bytes through it. -
-zthe testoptionalConnect without sending anything. -
-vthe detailsoptionalDisplay the result of the connection. -
-w 2the waitoptionalGive up after 2 seconds. -
srvthe hostA name or an address. -
22the 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:
ssh -f srv "cd nc-lab-srv && timeout 8 nc -l 9000 > recu.txt"; sleep 1ssh srv "ss -ltn | grep 9000"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:
nc -N -w 2 srv 9000 < a.txt; echo "code $?"sleep 1; ssh srv "wc -l < nc-lab-srv/recu.txt"code 01Code 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:
nc -zv -w 2 srv 22nc -zv -w 2 srv 9001Connection to srv (IP) 22 port [tcp/ssh] succeeded!nc: connect to srv (IP) port 9001 (tcp) failed: Connection refusedsucceeded! 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.
nc -z -w 2 srv 9001; echo $?1Third 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.
nc -zv -w 3 192.0.2.1 80nc: connect to 192.0.2.1 port 80 (tcp) timed out: Operation now in progresstimed 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:
nc -zv -w 2 srv 20-25nc: connect to srv (IP) port 20 (tcp) failed: Connection refusednc: connect to srv (IP) port 21 (tcp) failed: Connection refusedConnection to srv (IP) 22 port [tcp/ssh] succeeded!nc: connect to srv (IP) port 23 (tcp) failed: Connection refusednc: connect to srv (IP) port 24 (tcp) failed: Connection refusednc: connect to srv (IP) port 25 (tcp) failed: Connection refusedFile 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.
head -c 100000 /dev/urandom > gros.binssh -f srv "cd nc-lab-srv && timeout 8 nc -l 9002 > gros.bin"; sleep 1nc -N srv 9002 < gros.bin; sleep 1sha256sum gros.bin | cut -c1-16ssh srv "cd nc-lab-srv && sha256sum gros.bin | cut -c1-16; wc -c < gros.bin"b258464ae02d1116b258464ae02d1116100000The 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:
ssh -f srv "cd nc-lab-srv && timeout 8 nc -l 9040 > n.txt"; sleep 1timeout 4 nc srv 9040 < a.txt; echo $?124How 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
Try What you run
Listen, connect, send
Port 9000 of srv
$ 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"
- The server opens port 9000 and waits for a single connection.
- No test: the port keeps listening for the transfer.
- The client sends a.txt. The server receives one line and stops.
- One line received on the server.
What the animation shows
- Two sides:
nc -llistens,ncconnects. - A single connection:
nc -lstops after the first one. - Testing uses up the connection:
-zcounts as one. - 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 22Check what arrived
Compare the hashes on both sides:
sha256sum a.txtEssential vocabulary
The client · connect and test
-zv- An open port
nc -zv -w 2 srv 22→ succeeded -zv- A closed port
nc -zv -w 2 srv 9001→ Connection refused -z- The code only
nc -z -w 2 srv 9001; echo $?→ code 1 nc srv- Without -v, nothing is displayed
nc -N -w 2 srv 9001 < a.txt; echo $?→ code 1
The server · listen
-l- Send a file
ssh -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 UDP
ssh -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 connection
ssh -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 wait
nc -w 2 srv 9001; echo fin→ fin
The port nobody can reach
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.
- nc -zv
- ss -ltn
- wc -l
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
printf 'un\ndeux\ntrois\n' > b.txtssh -f srv "cd nc-lab-srv && timeout 60 nc -l 127.0.0.1 9007"; sleep 1nc -zv -w 2 srv 9007; echo $?ssh srv "ss -ltn | grep 9007"nc: connect to srv (IP) port 9007 (tcp) failed: Connection refused1LISTEN 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):
ssh srv "pkill -x nc"ssh -f srv "cd nc-lab-srv && timeout 60 nc -l 9010 > b.txt"; sleep 1nc -N srv 9010 < b.txt; echo $?; sleep 1; ssh srv wc -l nc-lab-srv/b.txt03 nc-lab-srv/b.txtThe 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:
ssh -f srv "cd nc-lab-srv && timeout 8 nc -k -l 9000 > k.txt"; sleep 1nc -N srv 9000 < a.txt; nc -N srv 9000 < a.txt; sleep 1; ssh srv wc -l nc-lab-srv/k.txt2 nc-lab-srv/k.txtBoth 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:
ssh -f srv "cd nc-lab-srv && timeout 8 nc -u -l 9004 > udp.txt"; sleep 1nc -u -w 1 srv 9004 < a.txt; echo $?; sleep 1; ssh srv wc -l nc-lab-srv/udp.txt01 nc-lab-srv/udp.txtThe 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:
ssh -f srv "cd nc-lab-srv && timeout 8 nc -u -l 9004 > udp.txt"; sleep 1nc -zvu -w 1 srv 9004; echo $?nc -zvu -w 1 srv 9005; echo $?nc -u -w 1 srv 9005 < a.txt; echo $?Connection to srv (IP) 9004 port [udp/*] succeeded!010On 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
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.
(head -c 40000 gros.bin; sleep 3; tail -c +40001 gros.bin) | nc -N srv 9021 & sleep 1.5; ssh srv pkill -x nc; waitCompare
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.
- nc -l
- sha256sum
- stat -c %s
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
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; waitssh srv stat -c %s nc-lab-srv/coupe.bin40000A 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:
ssh -f srv "cd nc-lab-srv && timeout 60 nc -l 9020 > gros.bin"; sleep 1nc -N srv 9020 < gros.bin; echo $?; sleep 1; sha256sum gros.bin; ssh srv sha256sum nc-lab-srv/gros.bin0b3da4adcca5532e1302cba27716b22da8663215ad2511c283bbf07a5b7ecd642 gros.binb3da4adcca5532e1302cba27716b22da8663215ad2511c283bbf07a5b7ecd642 nc-lab-srv/gros.binThe 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 (>):
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 1nc -N -w 3 srv 9030 < a.txt; sleep 1; ssh srv cat nc-lab-srv/c.txtrecu par le serveurbonjourThe 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):
nc -v -w 2 srv 22 < /dev/nullConnection to srv (IP) 22 port [tcp/ssh] succeeded!SSH-2.0-OpenSSH_10.2p1 Ubuntu-2ubuntu3.8The 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):
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 1curl -m 5 -s http://srv:8080/; echo $?bonjour0Without -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:
ssh -f srv 'cd nc-lab-srv && timeout 20 python3 -m http.server 8090' > /dev/null 2>&1; sleep 2printf 'GET /rep.txt HTTP/1.0\r\n\r\n' | nc -N -w 2 srv 8090HTTP/1.0 200 OKServer: SimpleHTTP/0.6 Python/3.14.4Date: Wed, 25 Nov 2026 14:31:05 GMTContent-type: text/plainContent-Length: 27Last-Modified: Wed, 25 Nov 2026 14:30:56 GMT
HTTP/1.1 200 OK
bonjourThe 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.
ssh srv 'timeout 3 nc -l 80; echo $?'nc: Permission denied1Netcat 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 netcat | ncat (package nmap-ncat) | |
|---|---|---|
| Listen | nc -l 9000 | ncat -l 9000 |
| Stay open after a connection | -k | -k |
| TLS encryption | no | yes, dedicated option |
| Accept only certain addresses | no | --allow |
| Run a program on connection | no | -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.
| Need | Command |
|---|---|
| Test a port, read a refusal, send data | nc |
| Scan a range, detect services and versions | nmap |
| See what listens on the machine | ss -ltn |
| HTTP request or download | curl |
| Send an encrypted file | scp |
| Watch the packets exchanged | tcpdump |
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.
| Symptom | Cause | Solution |
|---|---|---|
Connection refused, immediate | Nothing listens, the listener has already served its connection, it is bound to 127.0.0.1, or a firewall rejects | ss -ltn on the server, restart the listener, check the firewall |
timed out: Operation now in progress | Packets dropped (firewall) or machine unreachable | Check the firewall rules on srv, test with -w |
| Nothing printed, code 1 | Refusal without -v | Add -v, or read echo $? |
| Send with no visible error, empty or truncated file | Listener used up by a -z test, or stopped midway | Restart the listener, compare size and checksum |
nc does not give the prompt back after sending | -N missing | Add -N |
Unknown option, or -l without effect | Different variant | nc -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.
cd ~ssh srv "pkill -x nc; rm -r nc-lab-srv"rm -r ~/nc-labIf you added the line 127.0.0.1 srv to /etc/hosts, remove it:
sudo sed -i '/^127.0.0.1 srv$/d' /etc/hostsKey points
nc -l PORTlistens,nc HOST PORTconnects;nc -lserves only one connection then stops, unless you use-k.-ztests a port without sending anything, but uses up the single connection of a listener: the next send is refused without a message.Connection refusedis an immediate answer (nothing listens, or a firewall rejects);timed outis silence. Without-v, a refusal is silent, only code 1 reports it.-Ncloses after sending,-wlimits the wait. Compare size and checksums on both sides:ncchecks nothing.- In UDP,
-zprintssucceeded!on an open port and returns 1 without a message on a closed port; annc -usend 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
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
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
A new full quiz with random questions
Work again only on the questions you missed
FAQ: frequently asked questions about netcat
The Swiss army knife of networking
netcat (thenc command) reads and writes data over TCP or UDP connections. It adds no protocol on top of the connection: what you send goes out as is, and what comes back is displayed.It is used to:- test whether a port is open;
- grab a banner from a service;
- transfer a file between two machines;
- open a listener on a port.
nc), and ncat (from the Nmap project, with TLS and access control).A quick connectivity test
# TCP: is the port open?
nc -zv srv 22
# Connection to srv (IP) 22 port [tcp/ssh] succeeded!
# UDP
nc -zuv srv 53
The options:-z: scan mode, sends no data (just the connection);-v: verbose, prints the result;-u: switches to UDP instead of TCP.
succeeded! confirms that a service accepts the connection. In UDP, succeeded! (code 0) only means that no refusal came back; a closed port prints nothing and returns code 1. To know whether a UDP service answers, send it a real request.Receiver first, then sender
# 1. Receiving machine: listen and write into a file
nc -l 1234 > recu.tar
# 2. Sending machine: send the file and close the connection
nc -N 192.168.1.50 1234 < envoi.tar
# 3. On both machines: compare the checksum
sha256sum recu.tar # on the receiver
sha256sum envoi.tar # on the sender
The receiving machine opens a listener (-l) on port 1234 and redirects what it receives to a file. The sender connects and sends the file through a redirection; -N closes the connection once the file is sent, otherwise nc does not give the prompt back. nc checks nothing: only two identical checksums prove that the file is complete.Warning: the transfer is not encrypted. For a secure transfer, prefer ncat --ssl or scp.One connection versus a scan
| Need | netcat | nmap |
|---|---|---|
| Test one port | yes | yes |
| Scan thousands of ports | no | yes |
| Transfer data | yes | no |
| Detect service/version | limited | yes (-sV) |
| Open a listener | yes | no |
A legitimate tool turned to other uses
netcat is a perfectly legitimate administration tool. But its ability to open a listener and attach a connection to a shell also makes it an attack instrument: the reverse shells used in intrusions often rely onnc or an equivalent binary.Practical consequences:- the unexpected presence of netcat on a production server is a warning sign;
- some EDRs monitor its execution.