Nc Command in Linux: Tool to Debug Network Issues

nc command
nc command

The “nc” command is often referred to as “Netcat”. It is a flexible yet robust tool often used in the Linux and other Unix based OS environments. It is a powerful utility to read from and write to network connections using either UDP or TCP. If you are a professional like network administrator, system administrator, or security professional, this utility can help you do important tasks like:

  • Simple client-server communications
  • File transfers
  • Network debugging
  • Port scanning

In this article, let us see how the nc command is used widely, its features, and some practical examples to understand this utility better.

Salient Features of the nc Command

  • Client-server communication: You can connect two machines using this utility to relay simple messages between the machines.
  • Port scans: This utility can help you see which ports are open on a target system
  • Listen to ports: Setting up a server to listen for incoming connections on a port is possible with nc command.
  • Grab service banners: You can capture service banners to know the running services and also their respective versions.
  • Find network bugs: This is the primary use of the nc command. You can test network connectivity and its performance.
  • File or data transfer: Using nc command you can send and receive data between two connected devices over the network.

How to Install NC on Linux Machines

Before we go to the part where we explain how the nc command will be used often, ensure you have nc or Netcat installed on your system. Nowadays, on most Linux distributions like Ubuntu, Netcat comes pre-installed. If it is not available, you can install it using apt command. Here is the command to install Netcat. Once nc is installed, you can start using it without any restarts or configurations.

sudo apt-get install netcat

Basic Use of nc Command

Once you have installed Netcat, here are some basic actions you can perform with it to get you started.

Basic Port Scanning

The primary use of Netcat is to scan for open ports on a remote machine. To do this, execute the following command:

nc -zv IPAddress Port

Do you see the parameters along with the nc utility? They are for fine-tuning the command. Let us see about those one by one now.

  • -z: To tell Netcat to just scan and not to send any data
  • -v:To get a detailed output by enabling verbose output
  • IPAddress: This is just a sample target IP address
  • Port: This is the specified range of ports to scan

Netcat will try to connect to each port in the range specified upon execution of this command. Then it will report if the port is open.

Set Up a Server Using Netcat

You can use the nc command to set up a simple TCP server. To start a server that listens on a particular port, execute this command:

nc -l XXXX

The parameter “-l” instructs Netcat to listen for any incoming connections and “xxxx” is just a sample port number on which Netcat should listen. The terminal window will immediately display any data sent to this port.

Simple Client-Server Communication

You can set up basic client-server communication using Netcat. Execute this command to set up a server on one of the machines:

nc -l XXXX

On another machine with nc installed, connect to the server using the command:

nc YYY.YYY.Y.Y XXXX

Here, YYY.YYY.Y.Y is the IP address and XXXX is the target port. Netcat sends anything you type in the client terminal to the server terminal and vice versa. This helps in relaying simple text messages across machines with nc installed.

File Transfer Using Netcat

With nc command you transfer files between two connected machines. On the machine with the file package, execute this command:

nc -l XXXX < unixmen.txt

On the receiving machine, execute the following command.

nc YYY.YYY.Y.Y XXXX > unixmen.txt

The nc utility transfers the file named “unixmen.txt” from the sender to the receiver.

How to Grab Service Banners

You can use nc command to grab banners from the services running on connected remote servers. Grabbing banners help you identify the version of the service running on a specified port.

nc YYY.YYY.Y.Y XXXX

Enter “HEAD / HTTP/1.0” and press the Enter key twice. This receives the HTTP headers including the server banner which usually contains all details about the server software.

Limitations of Netcat

A lot of security tools flag Netcat or the nc command is flagged by many security software and it is not preferred in many environments. There are chances to use Netcat or the nc command maliciously as well. The capabilities of nc command also includes setting up reverse shells and proxy connections. This helps attackers use Netcat to penetrate systems.

Wrapping Up

The Netcat utility is an essential tool to work with networks even if you are just playing around with multiple machines on a network. The nc command can empower you to effectively troubleshoot network issues and manage network connections. Use Netcat to use your networks effectively and do more. To learn more, refer to Netcat’s official documentation.

Some More Articles that would be of Interest to You