The netcat tool is quite popular among network administrators. However, it can also serve different purposes for you as a casual Linux user in your day-to-day life. While doing some research, I’ve found some great use cases to play with the nc command.

6Establish Network Connections

I’ll start with the simplest and most fundamental thing nc can do. That is to create a directTCP or UDP connectionbetween two machines. This is the building block for many of the more advanced uses.

Suppose you have two Linux systems. For demonstration, I’m using an Ubuntu machine and a Linux Mint machine. Ubuntu will serve as the server or listener while Linux Mint will act as the client or connector.

Establishing a network connection between two machines using nc.

First, start the listener.

The -l flag is for the listener mode. The -p flag is for defining the port. Here, we’re using port 12345, but you can use any other you want, unless it’s already in use. After you run this command, your terminal will just sit there, waiting for a connection.

Now, from the connector machine, run:

Replace ip_address with the IP address of your server. The port number will be the one you used in your listener. So, in my case:

This establishes a TCP connection between the two machines. Moreover, it’s not one-way communication where only the server can send. Rather, both parties can send and receive.

Establishing a UDP network connection between two machines using nc.

If you want to establish a UDP connection instead, you can do so using the -u flag. On the listener machine, run:

On the client machine, run:

UDP connections are less reliable than TCP. However, they are preferable when speed is critical and a bit of data loss is acceptable.

5Port Scanning

Aside from connecting to a single port, nc cancheck a whole range of portsto see which ones are open. This can help with troubleshooting or enumerating services. It’s also why nc is sometimes called the “Swiss Army Knife” of networking.

Let’s say you want to find which TCP ports are open on your server machine from your client machine. You create a listener on the server as before.

Port scanning using the nc command.

Then, from the client, run:

The -z flag tells netcat to use the Zero-I/O mode. This means netcat should just scan for listeningdaemonsinstead of sending any data. The -v flag enables verbose mode, which just displays the results in a better way. We are also defining the range of ports from 20 to 130 that we’re interested in.

Only scan systems you own or have explicit permission to scan. Unauthorized scanning can be illegal or viewed as hostile.

Transferring files between machines using the nc command.

In the demonstration, almost all the ports refused connection because they were closed, except port 80. In the same way, you can also scan UDP ports.

4File Transfer

Netcat can send files directly over TCP or UDP without needingFTP,SCP, or shared folders. This makes it ideal for quick transfers, especially when other methods are unavailable. Let’s send a file from my Linux Mint to my Ubuntu machine.

I already have a file named test.txt, which reads “File for testing nc file transfer.” First, create a listener on the machine where you want to transfer the file. When doing so, redirect the connection output to a file, like this.

Creating a minimal web server using the nc command.

Then, on the sender machine, redirect the file content to your nc connection like this.

To check if the transfer was successful, usethe ls commandto see if the file is there and thenthe cat commandto read its content. You can also do the reverse and send files the other way around. All you have to do is reverse the roles by using the commands I showed above.

Creating a minimal web server using the nc command and fetching the content using curl.

3Create a Web Server

Netcat can act as a simple HTTP server by listening on a port and sending raw HTML to anyone who connects. It’s not going to replaceApache or Nginx, of course. However, for quick demos, debugging, or learning about HTTP requests, it’s perfect.

First, create a static HTML file (I’m calling it page.html) on your server machine. For me, that’s Ubuntu.

Creating a simple chat application using the nc command.

In this file, I’ve included an HTTP status line, a content type header, and a simple HTML body. The\r\nsequences are critical in HTTP to separate headers from the body.

Now, start a netcat web server on the same machine:

We’re using a while loop here so that even after each request, the server stays up. We’re listening on port 8080 and sending the HTML file as a response. Now, open a web browser from the client machine (Linux Mint in my case,) and visit the static HTML page by specifying the IP and port.

You can also use the curl command to fetch the HTML page.

Simple chat application using the nc command with sender names appended.

2Create a Simple Chat Application

Netcat can connect two terminals, so anything you type on one appears instantly on the other. With a little shell trickery, you can make it into a real-time chat tool. No need for any third-party chat apps.

To do this, you need to create a network connection just like shown in the first section. Create a listener on one machine:

Then connect to it from another machine.

Once you establish the connection, you can type anything on one terminal. It will appear on the other terminal. Then you can type anything on the second terminal, and it’ll appear in the first.

Now you can type back and forth in the terminal to send messages from one computer to another. I tried to get a bit creative and see if I could also add the sender names with the corresponding messages. That way, it’s easier to track who sent which message.

You can do that by appending the sender name using echo and a while loop. Here’s the setup. Run this on the server machine:

And this on the connector machine:

The above command waits for input, prefixes it, and sends it. On the receiving end, the prefix appears exactly as sent. Even if there are more than two users, you’ll be able to track messages this way.

1Network Troubleshooting

Finally, you can use the nc command to fix some common networking issues. When something’s wrong on a network, it can help confirm whether it’s a connectivity issue, a service issue, or a firewall issue. It can test ports, simulate services, and check raw responses.

Suppose an application isn’t responding. you may use nc to see whether you can even connect to its port.

If it hangs or says “Connection refused,” the port is closed or filtered.

you’re able to manually talk to a service to see if it responds as expected. For example, checking if a local HTTP service responds.

If the service works, it will send back an HTTP response header and possibly some HTML. This is great for debugging misbehaving web apps without a browser.

If you suspect an app can connect to a port but isn’t sending the right data, you can pretend to be the service and capture what the app sends. This is useful for debugging custom client/server protocols. With netcat, you can quickly perform these small tests and find out where your network is failing and how to fix it.

There’s much more you can do with netcat. If you’re interested, you can check outthe official manpage of nc. There are many moreLinux networking commandsyou should explore.