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:
If the above command fails with error like this
Then you can install netcat-openbsd
package instead:
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:
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:
Once the command is executed, the nc
command will display a list of open ports on the specified device.
To capture all attempts, you can use the -v
flag which is the verbose flag.
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:
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:
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.
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:
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:
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! 👋