netcat Linux Command | Explained

In Linux distributions, the β€œnetcat(nc)” command is a powerful tool to diagnose networks using TCP or UDP protocols. It monitors traffic flow between systems, including port scanning, client/server connection, transferring files, and many more. This guide will demonstrate the installation and practical usage of the β€œnetcat” command in Linux systems. The content that carried out this article is as follows:

Let’s start with installation.

How to Install netcat in Linux?

The β€œnetcat” command is quite useful for network admin for troubleshooting the network. To install the β€œnetcat”, the syntaxes for various distributions are listed below:

For Debian/Ubuntu Based Distributions:

$ sudo apt install netcat  

For installation of β€œnetcat” in other linux distributions, follow the script as mentioned below:

For CentOS/RHEL:

$ sudo yum install nc

For Fedora:

$ sudo dnf install nc

Now, let’s check how the β€œnetcat” command works.

How Does the netcat Command Work in Linux?

The β€œnetcat” command’s working depends on the syntax which is described below:

Syntax:

$ nc [options] host port

In the above syntax, the β€œhost” represents the IP address, and β€œport” specifies the numeric port number.

The options supported by the β€œnetcat” command are provided below:

OptionsDescription
-vThe option specifies the verbosity for printing output.
-nIt is utilized to skip DNS warnings.
-zIt performs scanning without a connection established.
-lThis option is used for listening connections.

Additionally, you can explore all functionality of β€œnetcat” through the β€œman netcat”. It displays multiple synopses with their description below:

Let’s explore the usage of the β€œnetcat” command in various scenarios.

How to Use the netcat Command in Linux?

This section briefly demonstrates the β€œnetcat” command through practical examples. Let’s dig into them one by one:

Example 1: Ping a Specific Port to a Website

An example is carried out through the β€œnc” command to ping a port β€œ443” to the β€œgoogle.com” site. Additionally, the β€œ-z” flag displays the connection status, and β€œ-v” sets the verbose mode:

$ nc -zv google.com 443

The message shows that the connection is successfully established through the β€œ443” port.

Example 2: Create a Command Line Chat Box

In Linux, the β€œnc” command assists in creating an environment for sending or receiving messages. For this, β€œ-p” specify the port number as β€œ1234”, and β€œ-l” shows the operation performed in the β€œlisten” mode to send a custom message, i.e., β€œHello Sir”:

$ nc -l -p 1234

Keep running the terminal and meanwhile open another terminal. In that new terminal, execute the command β€œnc localhost port#”. For instance, the following command will connect us to the chat box:

$ nc 127.0.0.1 1234

In the above script, users can target a random IP address such as β€œ127.0.0.1” and port number β€œ1234”.

After executing the script, the messages can be received and sent from the respective terminals.

Here you go! The chat box has been created successfully.

Example 3: Scanning Multiple Ports

Port scanning is one of the essential tasks that the β€œnetcat” command does. To scan a specific port, the β€œnc” command can be used with different port numbers such as β€œ80”, β€œ22”, and β€œ53”:

$ nc -zvn 172.67.209.252 80 22 53

The above syntax is used to scan multiple ports

After executing the command, you can verify that a connection has been successfully established through the β€œ80” port.

Moreover, you can specify the range of port numbers as β€œ80-90” to scan multiple ports as below:

$ nc -zvn 172.67.209.252 80-90

All ports failed to establish connection except the β€œ80” port number.

Let’s explore another β€œnetcat” command usage.

Example 4: Transferring Files

A file can be transferred to different devices using the β€œnetcat” command. To do so, a file β€œitslinuxfoss.txt” is created and sent through the Linux terminal:

$ nc -l -p 9899 > itslinuxfoss.txt

 The β€œ-l” and β€œ-p” represent the listening mode and port number, respectively. Additionally, the β€œ9899” specifies the port number for transferring files.

Now, open another terminal and execute the below script in the home directory that receives the same file as seen below:

$ nc -w 2 192.168.1.102 9899 > itslinuxfoss.txt

In the above execution, the β€œ-w 2” represents the timeout of two seconds, β€œ9899” specifies the port number and a random IP Address β€œ192.168.1.102”.

Finally, you can verify through the β€œls” command that the specified file β€œitslinuxfoss.txt” has been successfully transferred to this terminal:

That’s all from this guide.

Conclusion

In Linux, the β€œnetcat” command is utilized to monitor the data across multiple devices within a network. Using this command, you can perform various operations, such as testing a specific port to a website, creating a chat box for bidirectional messages, scanning multiple ports, and transferring files. This guide has explained the practical usage of β€œnetcat” along with installation in the Linux operating system.