Yahoo
Advertisement
Advertisement
Advertisement
Advertisement

6 Networking Uses for the Linux nc Command

Linux mascot using a laptop with some multiplexer terminals around it.
Lucas Gouveia/How-To Geek

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.

Establish Network Connections

I'll start with the simplest and most fundamental thing nc can do. That is to create a direct TCP or UDP connection between 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.

Advertisement
Advertisement

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:

Establishing a network connection between two machines using nc.

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.

Advertisement
Advertisement

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:

Establishing a UDP network connection between two machines using nc.

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

Port Scanning

Aside from connecting to a single port, nc can check a whole range of ports to 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.

Then, from the client, run:

Port scanning using the nc command.

The -z flag tells netcat to use the Zero-I/O mode. This means netcat should just scan for listening daemons instead 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.

Advertisement
Advertisement

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

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.

File Transfer

Netcat can send files directly over TCP or UDP without needing FTP , 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.

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

Transferring files between machines using the nc command.

To check if the transfer was successful, use the ls command to see if the file is there and then the cat command to 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.

Create 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 replace Apache or Nginx , of course. However, for quick demos, debugging, or learning about HTTP requests, it's perfect.

Advertisement
Advertisement

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

Hello from Netcat

" > page.html

In this file, I've included an HTTP status line, a content type header, and a simple HTML body. The sequences 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.

Creating a minimal web server using the nc command.

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

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

Create 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.

Advertisement
Advertisement

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.

Creating a simple chat application using the nc command.

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.

Advertisement
Advertisement

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:

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

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.

Network 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.

Advertisement
Advertisement

Suppose an application isn't responding. You can 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 can 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 out the official manpage of nc . There are many more Linux networking commands you should explore.

Advertisement
Advertisement
Mobilize your Website
View Site in Mobile | Classic
Share by: