How to Use the Linux Netcat Command for Network Communication and Testing

📆 · ⏳ 4 min read · · 👀

Introduction

The nc command, also known as Netcat, is a command-line networking tool that has been around for many years. It is a simple yet powerful tool that can be used for a wide range of tasks such as network communication, port scanning, file transfer, and network testing.

The nc command is available on most Linux distributions and can be installed using the default package manager.

In this blog post, we’ll explore how to use the nc command in Linux and provide practical examples to help you get started.

Install Netcat

Firstly, we will install the netcat package on our system. To do this, we will use the default package manager for our distribution. For example for debian/ubuntu you can use apt package manager:

Terminal window
sudo apt install netcat

If the above command fails with error like this

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package netcat is a virtual package provided by:
netcat-openbsd 1.219-1
netcat-traditional 1.10-47
You should explicitly select one to install.
E: Package 'netcat' has no installation candidate

Then you can install netcat-openbsd package instead:

Terminal window
sudo apt install netcat-openbsd

Usage

Let’s now see some examples of how to use the nc command in Linux.

For Port Scanning

The nc command can also be used for port scanning, which is the process of checking whether a specific port is open or closed.

To perform a port scan using the nc command, you need to run the following command:

Terminal window
nc -z <server_ip_address> <port_range>

In the above command, port_range specifies the range of ports you want to scan. The -z option tells the nc command to scan for open ports without sending any data.

For example to scan ports 1 to 1000 on the device with IP address 192.168.0.102 you need to run the following command:

Terminal window
nc -z 192.168.0.102 1-100

Once the command is executed, the nc command will display a list of open ports on the specified device.

Connection to 192.168.0.102 port 53 [tcp/domain] succeeded!
Connection to 192.168.0.102 port 69 [tcp/tftp] succeeded!
Connection to 192.168.0.102 port 80 [tcp/http] succeeded!
Connection to 192.168.0.102 port 443 [tcp/https] succeeded!

To capture all attempts, you can use the -v flag which is the verbose flag.

Terminal window
nc -vz 192.168.0.102 1-100

For Testing Network Speed

The nc command can also be used to test network speed between two networked devices.

To test the network speed using the nc command, you need to run the following command on the device that will act as the server:

Terminal window
nc -l <port_number> > /dev/null

This command will listen for incoming connections on the specified port number and discard all incoming data.

To test the network speed from another device, you need to run the following command:

Terminal window
dd if=/dev/zero bs=1M count=100 | nc <server_ip_address> <port_number>

This command will send 100 MB of data to the server on the specified port number. The dd command is used to generate the data and the nc command is used to send the data to the server.

Once the command is executed, the nc command will display the network speed in bytes per second.

100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.001434 s, 73.1 GB/s

For File Transfer

The nc command can also be used for file transfer between two devices.

To transfer a file using the nc command, you need to run the following command on the device that will act as the server:

Terminal window
nc -l <port_number> > <file_name>

This command will listen for incoming connections on the specified port number and save the incoming data to the specified file name.

To send a file to the server from another device, you need to run the following command:

Terminal window
nc <server_ip_address> <port_number> < <file_name>

This command will establish a connection to the server on the specified port number and send the data from the specified file to the server.

Conclusion

In this article, we have explored how to use the nc command in Linux and provided practical examples to help you get started.

The nc command is a versatile networking tool that can be used for a variety of tasks such as network communication, port scanning, file transfer, and network testing. It provides a simple and effective way to connect and interact with other networked devices.

If you have any questions or suggestions, feel free to reach out to me on Twitter ↗️ / Reddit ↗️.

Happy hacking! 👋

You may also like

  • # linux# homelab

    HTTPS with self-signed certificates for your Homelab services

    In this article we will deep dive into understanding how we can setup HTTPS with self-signed certificates for our Homelab services.This is often required when you are running your own services and you want to access them over HTTPS.

  • # linux# homelab# networking

    Setup Shareable Drive with Samba in Linux

    In this article we will setup a shareable drive in Linux with SMB. We will learn how to setup the share directory using Samba on server and how to mount it on client.

  • # networking# linux# homelab

    Setup Shareable Drive with NFS in Linux

    In this article we will learn how to setup a shareable drive with NFS in Linux. We will see the steps to setup NFS server and mount the drive on a client machine.