Netcat Tutorial for Beginners

Netcat is an awesome Linux command used by network administrators and security experts. It is used for many purposes, such as reading and writing data on a remote computer by using TCP and UDP packets, creating raw connections with other computers in a network, banner grabbing, etc. It is similar to the telnet command with some additional features. This tool is also considered as the "Swiss Army Knife of Networking Tool."

Tutorial For Beginners: Learn awk Command with Examples in Linux

Installation

netcat is pre-installed on Linux system. Although it can be installed by using below command.

$sudo apt-get install netcat

After installation, you can see all usage options by using below command.

$nc -h

Basic syntax:

$nc [options] [IPAddress] [port(s)]

Netcat client

$nc [TargetIP] [port]

Netcat listener mode

$nc -l -p [port]

Use Netcat to connect a remote system

You can use netcat for a chat with a remote system.

Write below command used to connect remote system by providing IP address and port number of remote system.

$nc <IPAddress> <port>

Write below command in the remote system.

$nc -l -v <port><br>

In below example, ssh the remote system by using "ssh <user>@<IP>" command to see both screens on the same window.  Later type above commands for the chat.

File Transfer 

netcat command can be used to transfer the file from one machine to another machine.

Steps to transfer file

(1) Create a text file which needs to transfer from the source machine (say P1). You can ignore this step if the file is already there for transfer.

$cat >> {TestFile}
{content}
<Ctrl + C>

(2) Write below command to receive a file in destination machine (say P2)

$nc -l -v {PortToListen} {TestFile}

(3) Write below command to transfer the file from P1 to P2

$nc <DestinationIP> {ListeningPort} < {TestFile}

(4) On a successful transfer, you can check the transferred file by using the following command

$ls

TCP Port Scanner

You can use nc command-line tool for scanning the domain or IP for checking ports.

$nc -v -n -z -wl <DomainName> <StartPortNo-EndPortNo>
$nc -v -n -z -wl <IP> <StartPortNo-EndPortNo>

Subscribe us to receive more such articles updates in your email.

If you have any questions, feel free to ask in the comments section below. Nothing gives me greater joy than helping my readers!

Disclaimer: This tutorial is for educational purpose only. Individual is solely responsible for any illegal act.

You may also like...

Leave a Reply

Your email address will not be published.