Netcat (nc) Command in Linux with Examples

Netcat, usually run as nc, is a command-line tool for reading and writing data over network connections. It can connect to TCP or UDP ports, listen for incoming connections, transfer files, test services, and send raw requests.
Administrators often use Netcat for quick network checks because it is small, scriptable, and available on most Linux systems. It is not a replacement for tools such as Nmap
or curl
, but it is useful when you need a fast way to test whether a port, service, or connection works.
This guide explains how to use the Netcat nc command in Linux through practical examples.
Netcat Syntax
The most basic syntax of the Netcat utility takes the following form:
nc [options] host portBy default, Netcat opens a TCP connection to the specified host and port. To use UDP instead, add the -u option:
nc -u host portNetcat Versions
There is more than one version of Netcat. On most Linux distributions, nc is provided by one of three packages, and each one supports a slightly different set of options:
| Package | Common on | Listen on port 5555 | Close after sending | -e option |
|---|---|---|---|---|
netcat-openbsd | Ubuntu, Debian (default) | nc -l 5555 | -N | No |
netcat-traditional | Ubuntu, Debian (optional) | nc -l -p 5555 | -q 0 | Yes |
nmap-ncat | Fedora, RHEL | nc -l 5555 | Automatic | Yes |
The examples in this guide use the OpenBSD Netcat syntax, which is the default on Ubuntu and Debian. To find out which version you have, run nc -h:
nc -hThe first line of the output names the implementation:
OpenBSD netcat (Debian patchlevel 1.234-1)Some options differ between implementations. If a command does not work as shown, check the local manual page:
man ncNetcat is often preinstalled, but minimal server images may not include it. To install the OpenBSD version on Ubuntu and Debian, run:
sudo apt install netcat-openbsdOn Fedora and RHEL, the nc command comes from the Nmap project. Install the nmap-ncat package:
sudo dnf install nmap-ncatThe -e option runs a program and connects its input and output to the network connection. The traditional version and Ncat support it. The OpenBSD version leaves it out on purpose, and nc -e fails with the following error:
nc: invalid option -- 'e'-e on a network you do not control.Common Netcat Options
The most useful nc options are:
-l- Listen for an incoming connection instead of opening a connection.-z- Scan for listening services without sending data.-v- Print more connection details.-n- Do not resolve hostnames through DNS.-u- Use UDP instead of TCP.-w- Set a connection timeout in seconds.-k- Keep listening after a client disconnects.-N- Close the connection after reaching the end of the input. OpenBSD version only.-p- Set the local source port, when supported by your Netcat version.
Checking If a Port Is Open
The quickest way to find out whether a service accepts connections is to check its port. Pass the host and the port number with the -z and -v options:
nc -zv 10.10.8.8 22The -z option tells nc to only scan for open ports without sending any data to them, and the -v option provides more verbose information. -zv is the short form of -z -v.
If the port is open, the output will look something like this:
Connection to 10.10.8.8 22 port [tcp/ssh] succeeded!If nothing is listening on the port, nc reports that the connection was refused:
nc: connect to 10.10.8.8 port 22 (tcp) failed: Connection refusedThe nc command exits with status 0 when the port is open and 1 when it is not. This makes it useful in shell scripts. Add the -w option to set a timeout, so the check does not hang on a host that drops the packets:
if nc -z -w 3 10.10.8.8 22; then echo "SSH is up"; fiFor other ways to test a port, see our guide on how to check open ports in Linux .
Port Scanning
Scanning ports is one of the most common uses for Netcat. Instead of a single port, you can pass a port range.
For example, to scan for open ports in the range 20-80, you would use the following command:
nc -z -v 10.10.8.8 20-80The output will look something like this:
nc: connect to 10.10.8.8 port 20 (tcp) failed: Connection refused
nc: connect to 10.10.8.8 port 21 (tcp) failed: Connection refused
Connection to 10.10.8.8 22 port [tcp/ssh] succeeded!
nc: connect to 10.10.8.8 port 23 (tcp) failed: Connection refused
...
nc: connect to 10.10.8.8 port 79 (tcp) failed: Connection refused
Connection to 10.10.8.8 80 port [tcp/http] succeeded!If you want to print only the lines with the open ports, you can filter the results with the grep
command.
nc -z -v 10.10.8.8 20-80 2>&1 | grep succeededConnection to 10.10.8.8 22 port [tcp/ssh] succeeded!
Connection to 10.10.8.8 80 port [tcp/http] succeeded!You can also use Netcat to find the server software and its version. For example, if you send an “EXIT” command to the server on the default SSH port 22 :
echo "EXIT" | nc 10.10.8.8 22The output will look something like this:
SSH-2.0-OpenSSH_10.2p1 Ubuntu-2ubuntu3.6
Invalid SSH identification string.The first line is the version string the SSH server sends to every client. The second line is the server rejecting EXIT, which is not a valid SSH greeting.
To scan for UDP ports, simply add the -u option to the command as shown below:
nc -z -v -u 10.10.8.8 20-80With OpenBSD Netcat, -uz reports success regardless of the target machine’s state. A successful result does not confirm that a UDP port is open.
Typically, Nmap is a better tool than Netcat for complex port scanning.
Sending Files through Netcat
Netcat can be used to transfer data from one host to another by creating a basic client/server model.
This works by setting Netcat to listen on a specific port (using the -l option) on the receiving host and then establishing a regular TCP connection from the other host and sending the file over it.
On the receiving host, run the following command which will open port 5555 for incoming connections and redirect the output to the file:
nc -l 5555 > file_nameFrom the sending host, connect to the receiving host and send the file:
nc -N receiving.host.com 5555 < file_nameThe -N option closes the connection once the whole file is sent. Without it, the OpenBSD version keeps the connection open, and you have to press CTRL+C to end the transfer. The traditional version uses -q 0 for the same purpose.
To transfer a directory, use tar to archive the directory on the source host and to extract the archive on the destination host.
On the receiving host, set the Netcat tool to listen for an incoming connection on port 5555. The incoming data is piped to the tar
command, which will extract the archive:
nc -l 5555 | tar xzvf -On the sending host, pack the directory and send the data by connecting to the listening nc process on the receiving host:
tar czvf - /path/to/dir | nc -N receiving.host.com 5555You can watch the transfer progress on both ends. When the transfer completes, nc closes the connection, and both commands exit.
Creating a Simple Chat Server
The procedure for creating an online chat between two hosts is the same as when transferring files.
On the first host, start a Netcat process to listen on port 5555:
nc -l 5555From the second host run the following command to connect to the listening port:
nc first.host.com 5555Now, if you type a message and press ENTER, it will be shown on the other host.
To close the connection, type CTRL+C.
You will often see a listener started with nc -lvnp 4444. This command combines four options:
-l- Listen for an incoming connection.-v- Print connection details.-n- Do not resolve hostnames through DNS.-p- Set the port to listen on. The OpenBSD version accepts the port with or without-p, but the traditional version requires it.
nc -lvnp 4444When a client connects, the output will look something like this:
Listening on 0.0.0.0 4444
Connection received on 10.10.8.20 50948The second line shows the IP address and source port of the connecting host.
Performing an HTTP Request
Although there are better tools for HTTP requests such as curl
, you can also use Netcat to send various requests to remote servers.
For example, to retrieve the Netcat man page from the OpenBSD website, you would type:
printf "GET /nc.1 HTTP/1.1\r\nHost: man.openbsd.org\r\nConnection: close\r\n\r\n" | nc man.openbsd.org 80The full response, including the HTTP headers and HTML code, will be printed in the terminal.
The Connection: close header tells the server to close the connection after sending the response. Without it, many servers keep the connection open, and nc waits until you press CTRL+C.
Connection Timeout
Netcat does not set a timeout of its own. When a host does not respond, the connection attempt waits until the kernel gives up, which takes about two minutes with the default Linux settings. Once a connection is established, it stays open even when no data is sent.
Use the -w option to specify a timeout in seconds, and add -v to print connection details and errors:
nc -v -w 5 host.com 80This will time out after 5 seconds if no connection is established, or if the connection stays idle for 5 seconds. When the connection attempt times out, nc prints an error like this:
nc: connect to host.com port 80 (tcp) timed out: Operation now in progressThe -w option has no effect in listen mode. A listener started with nc -l waits for a connection indefinitely.
Keep Connection Open
The -k option tells Netcat to keep listening for another connection after the current one is completed. This is useful when you want to accept multiple connections:
nc -k -l 5555Without -k, Netcat exits after the first connection closes.
Creating a Simple Web Server
You can use Netcat to create a simple HTTP server that serves a single file. Browsers and curl expect an HTTP status line and headers before the content, so the command prints them first and then sends the file:
while true; do { printf 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n'; cat index.html; } | nc -N -l 8080; doneThis serves the contents of index.html to any client that connects to port 8080. Each pass of the loop handles one request, and -N closes the connection after the page is sent. Open http://localhost:8080 in your browser to test it, and press CTRL+C to stop the server.
Without the status line, curl refuses the response with Received HTTP/0.9 when not allowed.
Troubleshooting
nc: command not found
Install Netcat from your distribution repositories. On Ubuntu and Debian, install the netcat-openbsd package. On Fedora and RHEL, install nmap-ncat.
Connection refused
The remote host is reachable, but nothing is listening on that port, or the service is rejecting the connection. Confirm the service is running and that the port number is correct.
The command hangs
Netcat waits for a connection or response by default. Add -w to set a timeout:
nc -w 5 host.com 80The command does not exit after sending a file
The OpenBSD version keeps the connection open after it reaches the end of the input. Add -N on the sending host to close the connection when the transfer is complete.
UDP scans do not show clear results
OpenBSD Netcat’s -uz combination reports success regardless of the target machine’s state, so it cannot confirm that a UDP port is open. Use Nmap
for UDP scanning.
An option works on one system but not another
Different Netcat implementations support slightly different flags. See the table in the Netcat Versions
section, and run man nc on the system where you are using the command.
Quick Reference
For a printable quick reference, see the Netcat cheatsheet .
| Task | Command |
|---|---|
| Check one port | nc -zv host 22 |
| Scan ports | nc -z -v host 20-80 |
| Probe UDP ports (success does not confirm an open port) | nc -z -v -u host 20-80 |
| Listen on port | nc -l 5555 |
| Listen with details, no DNS | nc -lvnp 4444 |
| Connect to port | nc host 5555 |
| Transfer file (receive) | nc -l 5555 > file |
| Transfer file (send) | nc -N host 5555 < file |
| Send HTTP request | printf "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n" | nc example.com 80 |
| Set timeout | nc -w 5 host 80 |
| Keep listening | nc -k -l 5555 |
| UDP connection | nc -u host 5555 |
Conclusion
Netcat is useful for quick TCP and UDP checks, simple listeners, file transfers, and raw network testing. For deeper scans use Nmap
, and for HTTP work use curl
.
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page