$_ CLI In Depth
Learning progress 0%
lesson networking sockets ssh

Raw TCP with nc

3 min read Module 16: Networking Tools Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:08

What netcat does

A TCP connection is a two-way byte stream. Netcat makes one end of that stream and attaches it to a file descriptor. In connect mode it opens a connection to a host and port, sends the bytes it reads from standard input, and writes the bytes it receives to standard output. In listen mode it waits for one connection and does the same on the other side.

Because it has no protocol of its own, netcat is a good check for whether a port accepts connections and what it sends first. Many services announce themselves with a greeting line, which you can see with netcat before you know the protocol.

Listening on a port

The BusyBox version accepts the -l flag to listen and -p to choose the port. Listening blocks until a client connects. To keep the example safe, the listener in this unit runs under the timeout command, which stops it after a set number of seconds even if no client arrives.

BASH1 lines
timeout 5 busybox nc -l -p 9124 > received.txt &

The listener runs in the background and writes whatever it receives into received.txt. The timeout stops it after five seconds, so a forgotten listener cannot hold the port open for long.

Sending a line

A client connects by giving the host and port. The text to send comes from standard input, typically from printf or from a file. When standard input reaches end of file, netcat closes the connection, which lets the server finish.

BASH1 lines
printf "hello\n" | busybox nc 127.0.0.1 9124

Run the listener first and then the client, in that order, so that the port is open when the client connects. The listener then writes hello into the file. If the listener started after the client, the client would fail with connection refused.

Warning

Netcat has no authentication and sends everything in clear text. Never run an open listener on a machine that other people can reach, and never send passwords or keys through it.

Testing a port without sending data

To test whether a port is open, you can try a connection and immediately close it. Bash does this without any extra program, using the /dev/tcp path from the sockets unit. The redirection either succeeds or fails, and the status tells you which.

BASH1 lines
bash -c 'exec 3<>/dev/tcp/127.0.0.1/9999' 2>/dev/null; echo "status=$?"

The status is 1 when nothing listens on port 9999. The message about connection refused goes to standard error, which the redirection hides here so the status line is easy to read.

Netcat and the other tools

The full netcat programs have more options, such as keeping the listener open for many clients and sending files in both directions. Their options differ between versions. Check the help of the version installed on your machine rather than copying flags from another system. The BusyBox nc on the lab machine supports the listen and connect forms shown here.

Hands-on: a one-line exchange

The lab asks you to start a listener, send the word hello through it, and save what the listener received. The solution runs the same steps, so you can compare. The important habit is starting the listener before the client.

BASH5 lines
timeout 5 busybox nc -l -p 9124 > received.txt &
sleep 1
printf "hello\n" | busybox nc 127.0.0.1 9124
sleep 1
cat received.txt

The final line prints hello, which is the byte stream that crossed the loopback connection.

A TCP connection on loopback, from listen to close

Visualizer

A TCP connection starts with a three-packet handshake and ends with a four-packet close. Watch the kernel state of each socket as the packets pass, the way ss -tan shows them.

1 / 5
socket()
bind(127.0.0.1:9000)
listen(backlog)
accept() waits
Three-way handshake, as ss shows it
StepPacketClient socketServer connection
server: listen()--LISTEN (listener)

The server calls socket(), then bind() to pick the address and port, then listen(). listen() makes the socket a passive listener with a queue for finished connections. Only the listener exists so far.

nc -l -s 127.0.0.1 -p 9000 & pid=$!; sleep 0.3; ss -tlnp | grep 9000; kill $pid
Terminal lab · 2 steps · real Linux in your browser

Send bytes through a local listener

Start a short listener, send a line through it, and then send a file's contents.

01
Send hello through a listener
Start a BusyBox netcat listener on port 9124 under timeout, writing what it receives to received.txt. Then send the word hello to that port from a second command. Use this sequence: ``bash timeout 5 busybox nc -l -p 9124 > received.txt & sleep 1 printf 'hello\n' | busybox nc 127.0.0.1 9124 sleep 1 `` Then read received.txt.
Show hint for step 1
Start the listener before the client. The sleep gives the listener time to open the port.
02
Send a file through netcat
The file note.txt is in /workspace. Start a listener on port 9125 under timeout that writes to recv2.txt, then send the contents of note.txt to that port by giving the file as standard input to netcat: ``bash timeout 5 busybox nc -l -p 9125 > recv2.txt & sleep 1 busybox nc 127.0.0.1 9125 < note.txt sleep 1 ``
Show hint for step 2
The redirection with < feeds the file to netcat as standard input, and netcat sends it as the bytes of the connection.
Check yourself Question 1 of 4
Concept Check Listen flag recall

Which BusyBox nc option makes it listen for a connection?

Recap & Key Takeaways

Netcat connects standard input and output to a TCP socket, with no protocol of its own, which makes it a simple test tool.
The BusyBox nc supports listen mode with -l and -p, and a client connects by giving host and port.
Start the listener before the client, and wrap a listener in timeout so it does not stay open.
Netcat sends data in clear text and has no authentication, so use it only for local tests.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue