Introduction
Netcat (nc
) command is a command-line utility for reading and writing data between two computer networks. It works like a port scanning tool, a security tool, or a network monitoring tool.
Network and system administrators use this tool to quickly identify how their network is performing and what type of activity is occurring. In addition, you can transfer files directly through Netcat or use it as a backdoor into other networked systems.
In this article, we will discuss how to use the Netcat utility. Netcat command can assist you in monitoring, testing, and sending data across network connections.
The command differs depending on the operating system (netcat
,nc
,ncat
, and others).
Below is a detailed explanation of the Netcat command:
nc [<options>] <host> <port>
On Ubuntu, the command is to be usednc
whereas, on CentOS, Debian, and RHEL, the command isncat
- <host> - can contain a numeric IP address or a hostname.
- <port> - can contain a numeric port or service name.
-
<options> - The table below contains commonly used
nc
command options:
Option | Type | Description |
-4 |
Protocol | Use IPv4 only. |
-6 |
Protocol | Use IPv6 only. |
-u --udp
|
Protocol | Use for UDP connection. |
-s <host> |
Connect mode | Binds the Netcat host to <host> |
-u |
Protocol | Use for UDP connection. |
-l |
Listen mode | Listens for connections instead of using connect mode. |
-k |
Listen mode | Keeps the connection open for multiple simultaneous connections. |
-v |
Output | Sets verbosity level. Use multiple times to increase verbosity. |
-z |
Output | Report connection status without establishing a connection. |
1. How Client-Server Connection works using Netcat
A simple client/server connection is between two machines. On one machine, start nc listening on a specific port for a connection.
For Example:
a. On machine 1, run thenc
command with IP address or hostname in listen mode and provide a port:
nc -lv 66.63.166.76 5000
With the start "-l" parameter, listen mode is activated, which makes machine 1 the server. The "-v" parameter shows the device listening for connections.
b. On machine 2, run thenc
command with the IP address or hostname of machine 1 and the port:
nc -v 66.63.166.76 5000
The output shows the connection is successful.
Also, machine 1 confirms the link and prints the IP address of machine 2.
The client/server connection was established successfully. Anything typed at the second console will be concatenated to the first, and vice-versa. The connection can be terminated using an EOF('^C').
2. Data Transfer using Netcat
Netcat allows you to transfer files between two Linux machines or servers. Below is an example that explains data transfer using the Netcat command:
a. Create a simple file on machine 1 using thetouch
command:
touch test.txt
The above command creates an empty text file.
b. Create a listening connection on machine 1 with IP address:
nc -lv 66.63.166.76 5000 < test.txt
The output shows machine 1 listening for connections due to the-v
parameter.
c. On machine 2, connect to machine 1 and transfer the file:
nc -zv 66.63.166.76 5000 > test.txt
The output displays the 'test.txt' file name is available, indicating the transfer was successful.
3. Ping Specific Port on the Website
Netcat allows you to test a specific port to a website.
For example, If you need to ping google.com
nc -zv google.com 443
The output shows the successful connection message. The-z
parameter ensures the connection does not persist.
4. Port Scanning
The commandnc
can be used to scan for open ports.
a. Run the following command to check whether port 8080 is open:
For example:
nc -zv 192.168.6.106 8080
If the port is open, the output shows a successful connection message.
b. Alternatively, scan multiple ports by adding a port range.
For example:
nc -zv 192.168.6.106 8076-8082
Above are the examples where the Netcat (nc) command is used for communication in Linux OS. To understand the various options available with Netcat commands Refer to the following link.