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:
sudo apt install netcat
If the above command fails with error like this
Reading package lists... DoneBuilding dependency tree... DoneReading state information... DonePackage netcat is a virtual package provided by: netcat-openbsd 1.219-1 netcat-traditional 1.10-47You should explicitly select one to install.
E: Package 'netcat' has no installation candidate
Then you can install netcat-openbsd
package instead:
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:
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:
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.
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:
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:
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 in100+0 records out104857600 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:
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:
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.
Enjoying the content? Support my work! π
Your support helps me create more high-quality technical content. Check out my support page to find various ways to contribute, including affiliate links for services I personally use and recommend.
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! π