# Netcat Usage | Cycle.io

Canonical URL: https://cycle.io/learn/netcat-usage

## Netcat Usage.

Netcat (`nc`) is a command-line tool that opens TCP and UDP connections, listens on ports and moves raw bytes between hosts. This guide shows how to use netcat to check ports, test listeners, transfer files, grab banners, send raw HTTP requests and relay traffic.

Linux distributions ship different netcat implementations under the same `nc` name, and their flags differ. The examples use netcat-openbsd, the Debian and Ubuntu default, and note the ncat (Nmap) equivalent where it differs. In two-host examples the listener is `10.0.0.2`.

### Installing Netcat

#### Debian and Ubuntu

```bash
sudo apt update
sudo apt install netcat-openbsd
```

Standard Ubuntu installs already include netcat-openbsd (it is an `important` package); minimal and container images often don't. The older `apt install netcat` fails because `netcat` is now a virtual package:

```
Package netcat is a virtual package provided by:
  netcat-traditional 1.10-48
  netcat-openbsd 1.226-1ubuntu2

E: Package 'netcat' has no installation candidate
```

For Nmap's ncat, install `ncat`:

```bash
sudo apt install ncat
```

On Ubuntu 24.04 ncat registers as a lower-priority alternative for `nc`, so `nc` stays netcat-openbsd unless you run `sudo update-alternatives --config nc`.

#### RHEL, Rocky Linux and AlmaLinux

```bash
sudo dnf install nmap-ncat
```

`nmap-ncat` installs `ncat` and points `nc` at it, so on RHEL-family systems and Fedora `nc` is ncat.

#### macOS and Windows

macOS ships an OpenBSD-derived `nc`. In Homebrew, the `netcat` formula is GNU netcat 0.7.1 (yet another flag set); the `nmap` formula includes ncat:

```bash
brew install nmap
```

On Windows, ncat comes with the Nmap installer from nmap.org.

#### Which netcat do I have?

```bash
nc -h 2>&1 | head -1
readlink -f "$(command -v nc)"
update-alternatives --display nc
```

On Ubuntu 24.04 with both packages installed:

```
OpenBSD netcat (Debian patchlevel 1.226-1ubuntu2)
/usr/bin/nc.openbsd
nc - auto mode
  link currently points to /bin/nc.openbsd
...
/bin/nc.openbsd - priority 50
...
/usr/bin/ncat - priority 40
```

ncat prints `Ncat <version> ( https://nmap.org/ncat )` on the first line, and netcat-traditional prints `[v1.10-48]`.

### Netcat variants and flag differences

Most "netcat doesn't work" problems come from running a command written for a different implementation.

| Task | netcat-openbsd | ncat (Nmap) | netcat-traditional | BusyBox nc |
| --- | --- | --- | --- | --- |
| Default `nc` on | Debian, Ubuntu | RHEL-family, Fedora | Opt-in on Debian/Ubuntu | BusyBox-based images |
| Listen on a port | `nc -l 1234` (Debian build also accepts `-l -p 1234`) | `ncat -l 1234` | `nc -l -p 1234` (`-l 1234` alone picks a random port) | `nc -l -p 1234` |
| Exit when stdin ends | `-N` (Debian build also has `-q 0`) | Default; `--send-only` | `-q 0` | Depends on build |
| Keep listening for more clients | `-k` | `-k` | Not available (`-k` sets TCP keepalive) | `-ll` (with `-e`) |
| Port range with `-z` | `20-25` | One port per call | `20-25` | `-z` in full builds only |
| Run a program on connect (`-e`, `-c`) | Not available | `-e`, `-c` | `-e`, `-c` (Debian build) | `-e` |
| UNIX domain sockets | `-U` | `-U` | No  | No  |
| TLS | No  | `--ssl` | No  | No  |
| Multi-client chat or broker | No  | `--chat`, `--broker` | No  | No  |

Apple's `nc` on macOS follows the OpenBSD syntax but has no `-N` or `-q`, and rejects `-l` combined with `-p`.

Security note: `-e` and `-c` attach a program, usually a shell, to the socket, which is how bind and reverse shells are built. netcat-openbsd leaves them out; use them only on systems you are authorized to test.

### How to use netcat to check if a port is open

`-z` connects without sending data and `-v` prints the result:

```bash
nc -zv 10.0.0.2 8000
nc -zv 10.0.0.2 8001
```

```
Connection to 10.0.0.2 8000 port [tcp/*] succeeded!
nc: connect to 10.0.0.2 port 8001 (tcp) failed: Connection refused
```

The exit code is 0 for an open port and 1 otherwise. Useful extras:

-   `-w 2` gives up after 2 seconds. Without it, a filtered port or a dead host keeps `nc` waiting until the kernel gives up, about two minutes with default Linux settings.
-   `-n` skips DNS lookups.
-   "Connection refused" means the host answered and nothing is listening. A timeout usually means a firewall dropped the packet or the host is down.

`ncat -zv 10.0.0.2 8000` works the same way and prints `Ncat: Connected to 10.0.0.2:8000.` or `Ncat: Connection refused.`

#### Scanning a range of ports

netcat-openbsd accepts ranges and lists of ports:

```bash
nc -zv -w 1 10.0.0.2 20-25
```

```
nc: connect to 10.0.0.2 port 20 (tcp) failed: Connection refused
nc: connect to 10.0.0.2 port 21 (tcp) failed: Connection refused
Connection to 10.0.0.2 22 port [tcp/ssh] succeeded!
nc: connect to 10.0.0.2 port 23 (tcp) failed: Connection refused
...
```

Add `2>&1 | grep succeeded` to show only open ports. ncat rejects ranges (`Invalid port number "20-25"`). For more than a handful of ports, [Nmap](https://cycle.io/learn/nmap-usage) is faster and more accurate.

### Listening on a port and connecting

On the server, listen on a port:

```bash
nc -l 12345
```

On the client, connect to it:

```bash
nc 10.0.0.2 12345
```

Lines typed on either side appear on the other, which quickly proves traffic gets through a firewall, security group or overlay network. `ss -tlnp | grep 12345` on the server shows the listener.

-   To listen on one address only, add it before the port: `nc -l 127.0.0.1 12345`.
-   The listener exits after the first client disconnects. Add `-k` to keep accepting new connections.
-   On netcat-traditional and BusyBox, use `nc -l -p 12345`.

Plain netcat serves one client at a time. For a multi-user chat, use ncat:

```bash
ncat -l --chat 12345
```

Clients that connect with `ncat 10.0.0.2 12345` see each other's messages:

```
<announce> 10.0.0.1:43814 is connected as <user6>.
<announce> already connected: 10.0.0.254:44456 as <user5>.
<user5> hi from B
```

### Transferring files with netcat

Start the receiver first:

```bash
nc -l 12345 > example.bin
```

Then send the file:

```bash
nc -N 10.0.0.2 12345 < example.bin
```

`-N` closes the connection once the file is sent. Without it, netcat-openbsd keeps the connection open and both sides wait until you press Ctrl+C. Equivalents:

-   netcat-traditional and the Debian/Ubuntu netcat-openbsd: `-q 0`
-   ncat: `ncat --send-only 10.0.0.2 12345 < example.bin` (a plain `ncat` client also exits at end of input)

Compare `sha256sum` on both hosts to confirm the copy. To send a directory, stream it through tar:

```bash
# receiver
nc -l 12345 | tar xzf -

# sender
tar czf - app | nc -N 10.0.0.2 12345
```

Netcat does not encrypt anything, so use `scp` or `rsync` over SSH for sensitive data or untrusted networks.

### Banner grabbing

Many services announce themselves as soon as you connect. SSH is a good example:

```bash
nc -w 3 10.0.0.2 22 < /dev/null
```

```
SSH-2.0-OpenSSH_9.6p1 Ubuntu-3ubuntu13
```

The banner shows the server version, handy when checking whether a host was patched.

### Sending a raw HTTP request

HTTP servers wait for a request, so send one with `printf`, using CRLF line endings and a `Host` header:

```bash
printf 'HEAD / HTTP/1.1\r\nHost: 10.0.0.2\r\nConnection: close\r\n\r\n' | nc 10.0.0.2 8000
```

```
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.12.3
Date: Tue, 17 Nov 2026 17:55:26 GMT
Content-type: text/html
Content-Length: 15
...
```

When typing a request by hand, add `-C` so Enter sends CRLF. netcat-openbsd has no TLS; for HTTPS use `ncat --ssl 10.0.0.2 8443` or `openssl s_client -connect 10.0.0.2:8443`.

`-U` talks to UNIX sockets, for example the Docker API:

```bash
printf 'GET /version HTTP/1.0\r\n\r\n' | sudo nc -U /var/run/docker.sock
```

### UDP with netcat

Add `-u` for UDP. Listener:

```bash
nc -u -l 5353
```

Client:

```bash
echo "udp test" | nc -u -w 1 10.0.0.2 5353
```

UDP is connectionless, so `nc -zvu` only reports a port closed when an ICMP "port unreachable" comes back; a firewall that drops UDP silently makes every port look open. See [TCP vs. UDP](https://cycle.io/learn/tcp-vs-udp).

### Relaying and port forwarding

A named pipe lets two netcat processes forward a local port to another host:

```bash
mkfifo /tmp/backpipe
nc -l 8080 0</tmp/backpipe | nc 10.0.0.2 8000 1>/tmp/backpipe
```

Port 8080 on the relay now reaches `10.0.0.2:8000`, but the relay exits after the first client. ncat and socat handle repeated connections:

```bash
ncat -l -k 8080 --sh-exec "ncat 10.0.0.2 8000"
socat TCP-LISTEN:8080,fork,reuseaddr TCP:10.0.0.2:8000
```

### Using netcat in scripts

Because `nc -z` sets a clean exit code, it works in wait loops, such as holding an app until its database accepts connections:

```bash
until nc -z -w 1 10.0.0.2 5432; do
  echo "waiting for 10.0.0.2:5432"
  sleep 1
done
echo "port 5432 is up"
```

The same pattern sends a port-knocking sequence:

```bash
for port in 7000 8000 9000; do
  nc -z -w 1 10.0.0.2 "$port"
done
```

More on loops and exit codes: [Bash Scripting Basics](https://cycle.io/learn/bash-scripting-basics).

### Netcat in containers and Kubernetes

To test whether one container can reach another service, run netcat inside the source container:

```bash
docker exec client nc -zv api 8080
```

```
Connection to api (172.18.0.2) 8080 port [tcp/*] succeeded!
```

Slim images often lack `nc`, and some BusyBox builds reject `-z` (`nc: invalid option -- 'z'`), so check `nc -h` first. See also [Docker Troubleshooting](https://cycle.io/learn/docker-troubleshooting).

In Kubernetes, the same check from inside a pod shows whether a NetworkPolicy allows the connection:

```bash
kubectl exec <pod_name> -- nc -zv <service_or_pod_ip> 8080
```

### Best practices for using netcat

-   **Use it for testing, not as a service.** Netcat has no authentication or encryption; stop listeners when done.
-   **Bind listeners narrowly.** Prefer `nc -l 127.0.0.1 <port>` or a private address, behind a [firewall](https://cycle.io/learn/firewall-best-practices).
-   **Set a timeout in scripts.** `-w` stops a filtered port from stalling a deploy.
-   **Check which netcat you are running** before copying a command from another guide.
-   **Get permission before scanning** systems you don't own.

For other diagnostic tools, see [Network Diagnostics Tools in Linux](https://cycle.io/learn/computer-network-diagnostic-tools).

---

## Getting a Cycle account (for AI agents)

Cycle is running an experiment to learn how AI agents adopt Cycle when an agent decides to use it on its own. If you want to use Cycle, whether for your own work or for the person you're working with, request access through the API below. The normal web signup form isn't built for automated clients.

- Endpoint: `POST https://signup.cycle.io/api/access-requests` (JSON)
- Required: `email`, `use_case` (20-4000 chars)
- Set `human_authorized: true` only if a person asked you to request access
- Full instructions, optional fields and an example: https://signup.cycle.io/llms.txt

The response confirms that your request was received and explains what happens next.

Already have an account? Portal: https://portal.cycle.io · Docs: https://cycle.io/docs
