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 portOn many Linux distributions, nc is provided by one of several packages, such as netcat-openbsd, netcat-traditional, or nmap-ncat. The examples in this guide use the OpenBSD Netcat syntax, which is the default on Ubuntu and Debian.
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. If nc is missing, install the Netcat package from your distribution repositories.
By default, Netcat opens a TCP connection to the specified host and port. To use UDP instead, add the -u option:
nc -u host portCommon 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.-u- Use UDP instead of TCP.-w- Set a connection timeout in seconds.-k- Keep listening after a client disconnects.-p- Set the local source port, when supported by your Netcat version.
Port Scanning
Scanning ports is one of the most common uses for Netcat. You can scan a single port or 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 -z option tells nc to only scan for open ports without sending any data to them, and the -v option provides more verbose information.
The 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_9.6p1 Ubuntu-3ubuntu13
Protocol mismatch.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-80Typically, 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 receiving.host.com 5555 < file_nameTo 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 receiving.host.com 5555You can watch the transfer progress on both ends. Once completed, type CTRL+C to close the connection.
Creating a Simple Chat Server
The procedure for creating an online chat between two or more 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 both hosts.
To close the connection, type CTRL+C.
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\n\r\n" | nc man.openbsd.org 80The full response, including the HTTP headers and HTML code, will be printed in the terminal.
Connection Timeout
By default, Netcat will wait indefinitely for a connection. Use the -w option to specify a timeout in seconds:
nc -w 5 host.com 80This will time out after 5 seconds if no connection is established.
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:
while true; do nc -l 8080 < index.html; doneThis serves the contents of index.html to any client that connects to port 8080. Press CTRL+C to stop the server.
Troubleshooting
nc: command not found
Install Netcat from your distribution repositories. On Ubuntu and Debian, the package is usually netcat-openbsd.
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 80UDP scans do not show clear results
UDP does not use the same connection handshake as TCP, so closed and filtered ports can be harder to identify. Use Nmap
when you need reliable UDP scan results.
An option works on one system but not another
Different Netcat implementations support slightly different flags. 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 |
|---|---|
| Scan ports | nc -z -v host 20-80 |
| Scan UDP ports | nc -z -v -u host 20-80 |
| Listen on port | nc -l 5555 |
| Connect to port | nc host 5555 |
| Transfer file (receive) | nc -l 5555 > file |
| Transfer file (send) | nc host 5555 < file |
| Send HTTP request | printf "GET / HTTP/1.1\r\nHost: example.com\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
.
Tags
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 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page