ZetCode

Linux nc (netcat) Command

last modified October 24, 2026

The nc (netcat) command reads and writes data over network connections. It connects standard input and output to a TCP or UDP socket, so it can act as a client or a server for almost any text protocol. Administrators use it to check whether ports are open, to talk to services by hand, to transfer files, and to test firewalls and services in scripts.

There are several implementations of netcat. The examples use the OpenBSD version (netcat-openbsd 1.219), the default nc on Debian and Ubuntu, run in a Debian 12 container. A second container named server runs an SSH server on port 22 and a web server on port 80. Examples with a listener run both sides on the same machine; in practice, the two commands would run on different machines.

Checking open ports

The -z option makes nc only check whether a connection can be made, without sending data. -v reports the result.

nc -zv server 22
nc -zv server 80 443
nc -zv server 20-25

A port is open when the connection succeeds, and closed when the host answers with a refusal. Several ports or a range can be checked at once. The service names (ssh, http) come from /etc/services; they show the usual service for a port, not what really runs there.

$ nc -zv server 22
Connection to server (172.19.0.2) 22 port [tcp/ssh] succeeded!
$ nc -zv server 80 443
Connection to server (172.19.0.2) 80 port [tcp/http] succeeded!
nc: connect to server (172.19.0.2) port 443 (tcp) failed: Connection refused
$ nc -zv server 20-25
nc: connect to server (172.19.0.2) port 20 (tcp) failed: Connection refused
nc: connect to server (172.19.0.2) port 21 (tcp) failed: Connection refused
Connection to server (172.19.0.2) 22 port [tcp/ssh] succeeded!
nc: connect to server (172.19.0.2) port 23 (tcp) failed: Connection refused
nc: connect to server (172.19.0.2) port 24 (tcp) failed: Connection refused
nc: connect to server (172.19.0.2) port 25 (tcp) failed: Connection refused

Exit status and timeouts

Without -v, nc -z prints nothing and reports the result in its exit status, which makes it convenient in scripts.

nc -z server 22; echo $?
nc -z server 23; echo $?
nc -zv -w 2 192.0.2.1 80; echo $?

The status is 0 for an open port and 1 otherwise. A host that does not answer at all, typically because a firewall drops the packets, makes the connection hang until the system's TCP timeout of about two minutes. The -w option sets a timeout in seconds. 192.0.2.1 is an address reserved for documentation, so nobody answers there.

$ nc -z server 22; echo $?
0
$ nc -z server 23; echo $?
1
$ nc -zv -w 2 192.0.2.1 80; echo $?
nc: connect to 192.0.2.1 port 80 (tcp) timed out: Operation now in progress
1

Checking UDP ports

The -u option switches to UDP. Checking UDP ports is unreliable, because UDP has no connection setup.

nc -zvu server 53; echo $?
nc -zvu -w 1 192.0.2.1 53; echo $?

For the closed port 53 on server, the host replied with an ICMP port unreachable message, and nc returned 1 without a message. For the address where nothing exists, no reply came back, and nc reported success. "No answer" can mean an open port, a filtered port, or no host at all, so a UDP check proves only that a port is closed.

$ nc -zvu server 53; echo $?
1
$ nc -zvu -w 1 192.0.2.1 53; echo $?
Connection to 192.0.2.1 53 port [udp/domain] succeeded!
0

Reading a service banner

Many services greet clients with a line that names the software. Connecting with nc shows it.

time nc -w 2 server 22 < /dev/null

The SSH server reveals its version. Without -w, the command would wait until the server gives up on the silent client, two minutes for OpenSSH. With -w 2, nc quits after two seconds without traffic.

$ time nc -w 2 server 22 < /dev/null
SSH-2.0-OpenSSH_9.2p1 Debian-2+deb12u10

real	0m2.016s
user	0m0.002s
sys	0m0.001s

Talking to a web server

For text protocols, nc can send a hand-written request.

printf 'GET / HTTP/1.0\r\nHost: server\r\n\r\n' | nc server 80

HTTP lines end with \r\n, and an empty line ends the headers; printf produces these exactly, while echo would not. With HTTP/1.0, the server closes the connection after the response, which ends nc. This shows the raw response with all headers, useful when debugging proxies and load balancers. For everyday use, curl -v is more convenient.

$ printf 'GET / HTTP/1.0\r\nHost: server\r\n\r\n' | nc server 80
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.14.0
Date: Sat, 24 Oct 2026 19:31:40 GMT
Content-type: text/html
Content-Length: 27
Last-Modified: Sat, 24 Oct 2026 19:22:57 GMT

<h1>Hello from server</h1>

Listening for a connection

With -l, nc waits for an incoming connection instead of making one. Whatever the client sends appears on the listener's standard output.

nc -l 9000 > message.txt &
echo "Hello from the client" | nc -N localhost 9000
wait; cat message.txt

The listener runs in the background and writes the received data to a file. The -N option closes the client's sending side when its input ends, which tells the listener that no more data comes; both programs then exit. In two terminals, nc -l 9000 on one side and nc host 9000 on the other give a simple chat: each typed line appears on the other side.

$ nc -l 9000 > message.txt &
$ echo "Hello from the client" | nc -N localhost 9000
$ wait; cat message.txt
Hello from the client

Forgetting -N

The OpenBSD nc does not close the connection when its input ends, unless -N is given.

nc -l 9000 > /dev/null &
echo "Hello" | timeout 3 nc localhost 9000; echo $?

The data was sent, but the client kept the connection open, waiting for the other side, and the listener kept waiting for more data. timeout stopped the client after three seconds, as the status 124 shows. Other netcat versions behave differently: the traditional netcat uses -q 0 for the same purpose, and ncat closes its sending side automatically.

$ nc -l 9000 > /dev/null &
$ echo "Hello" | timeout 3 nc localhost 9000; echo $?
124

Transferring a file

Because nc copies raw bytes, it can transfer files between machines without SSH or a file server. The receiver listens and redirects the output to a file; the sender reads the file from standard input.

ls -l backup.tar.gz
nc -l 9000 > received.tar.gz &
nc -N localhost 9000 < backup.tar.gz
wait
sha256sum backup.tar.gz received.tar.gz

Start the receiver first. The matching checksums show that the file arrived intact. The data travels unencrypted and unauthenticated, so use this only on trusted networks; otherwise use scp or rsync.

$ ls -l backup.tar.gz
-rw-r--r-- 1 jano jano 545761 Oct 24 21:31 backup.tar.gz
$ nc -l 9000 > received.tar.gz &
$ nc -N localhost 9000 < backup.tar.gz
$ wait
$ sha256sum backup.tar.gz received.tar.gz
2e6f19bb60bbcd8454024acf2080db9d734158aa8a8f70785d544f75ed1fa09b  backup.tar.gz
2e6f19bb60bbcd8454024acf2080db9d734158aa8a8f70785d544f75ed1fa09b  received.tar.gz

Transferring a directory

Combined with tar, nc copies whole directory trees in one stream.

mkdir restore
nc -l 9000 | tar -xzf - -C restore &
tar -czf - project | nc -N localhost 9000
wait; find restore

The sender writes a compressed archive to standard output, and the receiver unpacks it as it arrives, so no temporary archive file is needed. File permissions and timestamps are preserved by tar.

$ mkdir restore
$ nc -l 9000 | tar -xzf - -C restore &
$ tar -czf - project | nc -N localhost 9000
$ wait; find restore
restore
restore/project
restore/project/README.md
restore/project/src
restore/project/src/app.py

Accepting several connections

A listener normally exits after the first connection. The -k option keeps it running.

nc -lk 9000 > log.txt &
for i in 1 2 3; do echo "message $i" | nc -N localhost 9000; done
kill $!
cat log.txt

The listener collected the messages of three separate clients. It handles one connection at a time, which is enough for simple tests and log collection. $! holds the PID of the last background process.

$ nc -lk 9000 > log.txt &
$ for i in 1 2 3; do echo "message $i" | nc -N localhost 9000; done
$ kill $!
$ cat log.txt
message 1
message 2
message 3

A one-shot web server

A listener can also send data: whatever is on its standard input goes to the client. Here, nc serves a single HTTP response and saves the request it received.

printf 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 13\r\nConnection: close\r\n\r\nHello, world\n' | nc -l -N 8080 > request.txt &
curl -s http://localhost:8080/status
wait; cat request.txt

curl received the response, and request.txt shows exactly what the client sent. This is a quick way to see the requests that an application, a webhook, or a monitoring system makes, or to fake a response for a test.

$ printf 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 13\r\nConnection: close\r\n\r\nHello, world\n' | nc -l -N 8080 > request.txt &
$ curl -s http://localhost:8080/status
Hello, world
$ wait; cat request.txt
GET /status HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.88.1
Accept: */*

Sending UDP messages

UDP listeners receive datagrams, such as syslog messages or metrics.

nc -u -l 5000 > udp.txt &
echo "temperature=21.5" | nc -u -w 1 127.0.0.1 5000
kill $!; cat udp.txt

A UDP listener does not end on its own, because UDP has no end of a connection; we stop it with kill. The client exits after the one-second timeout. We use the address 127.0.0.1 rather than localhost, which may resolve to the IPv6 address ::1, while the listener uses IPv4.

$ nc -u -l 5000 > udp.txt &
$ echo "temperature=21.5" | nc -u -w 1 127.0.0.1 5000
$ kill $!; cat udp.txt
temperature=21.5

Unix domain sockets

The -U option uses a Unix domain socket, a file system path instead of a host and port. Many local services, such as Docker, MySQL, or systemd, offer such sockets.

nc -lU /tmp/app.sock > unix.txt &
ls -l /tmp/app.sock
echo "status" | nc -NU /tmp/app.sock
wait; cat unix.txt

The socket appears as a file of type s. Access to it is controlled by its file permissions, which is why Unix sockets are a common way to restrict a service to local users.

$ nc -lU /tmp/app.sock > unix.txt &
$ ls -l /tmp/app.sock
srwxr-xr-x 1 jano jano 0 Oct 24 21:31 /tmp/app.sock
$ echo "status" | nc -NU /tmp/app.sock
$ wait; cat unix.txt
status

Relaying a connection

Two nc processes and a named pipe make a simple TCP relay: connections to a local port are forwarded to another host.

mkfifo relay
nc -l 8000 < relay | nc server 80 > relay &
curl -s http://localhost:8000/
wait

The listener passes what the client sends through the pipe to the second nc, which sends it to the web server; the server's response goes back through the named pipe relay to the listener and the client. The relay serves a single connection. For permanent forwarding, use SSH port forwarding or socat.

$ mkfifo relay
$ nc -l 8000 < relay | nc server 80 > relay &
$ curl -s http://localhost:8000/
<h1>Hello from server</h1>
$ wait

Bash's /dev/tcp

When nc is not installed, Bash itself can open TCP connections through the special paths /dev/tcp/HOST/PORT.

exec 3<> /dev/tcp/server/22
head -n 1 <&3
exec 3>&-
timeout 1 bash -c '< /dev/tcp/server/80' && echo open || echo closed
timeout 1 bash -c '< /dev/tcp/server/23' && echo open || echo closed

exec 3<> opens a connection as file descriptor 3, and exec 3>&- closes it. A redirection from the path works as a port check. These paths exist only inside Bash, not in the file system, and some distributions build Bash without them.

$ exec 3<> /dev/tcp/server/22
$ head -n 1 <&3
SSH-2.0-OpenSSH_9.2p1 Debian-2+deb12u10
$ exec 3>&-
$ timeout 1 bash -c '< /dev/tcp/server/80' && echo open || echo closed
open
$ timeout 1 bash -c '< /dev/tcp/server/23' && echo open || echo closed
bash: connect: Connection refused
bash: line 1: /dev/tcp/server/23: Connection refused
closed

Waiting for a service in a script

Startup scripts often have to wait until a database or web server accepts connections. The following script polls a port with nc -z.

wait-for.sh
#!/bin/bash

# waits until host:port accepts TCP connections, or gives up after a timeout
host=$1
port=$2
timeout=${3:-30}

for (( i = 0; i < timeout; i++ )); do
    if nc -z -w 1 "$host" "$port" 2> /dev/null; then
        echo "$host:$port is up after $i s"
        exit 0
    fi
    sleep 1
done

echo "$host:$port did not come up within $timeout s" >&2
exit 1

We start a web server with a three-second delay and wait for it, then wait for a port where nothing starts. The script exits with 0 or 1, so it can be used as bash wait-for.sh db 5432 60 && ./start-app. The -w 1 keeps each attempt short when the host does not answer.

$ (sleep 3; python3 -m http.server 8000 > /dev/null 2>&1) &
$ time bash wait-for.sh localhost 8000 10
localhost:8000 is up after 4 s

real	0m4.023s
user	0m0.007s
sys	0m0.016s
$ bash wait-for.sh localhost 8001 2; echo $?
localhost:8001 did not come up within 2 s
1

Netcat versions

The options differ between implementations, which often explains why an example from the internet does not work.

ImplementationNotes
OpenBSD netcat (netcat-openbsd)Default on Debian and Ubuntu; -N to close on EOF; no -e; supports -U and proxies (-X, -x)
Traditional netcat (netcat-traditional)Listener with -l -p PORT; -q 0 to quit on EOF; -e runs a program
Ncat (ncat, from Nmap)Default nc on Fedora and RHEL; TLS with --ssl, access control, --sh-exec, multiple clients with -k
BusyBox ncMinimal version in small containers and embedded systems

Best practices

Source

OpenBSD nc(1) manual page, Ncat

In this article, we have used nc to check TCP and UDP ports, read service banners, talk HTTP by hand, transfer files and directories, serve a fake HTTP response, use UDP and Unix sockets, and relay a connection, and we have looked at Bash's /dev/tcp. See also the ss, curl, and ping and traceroute articles.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Linux tutorials.