Nc Command with 10 Examples


The nc or netcat command is a community instrument that permits customers to switch information between gadgets, scan ports and diagnose issues. This educational explains 10 nc usages to scan ports, switch information, and banner grabbing.

Putting in netcat in Linux

 To start out, on Debian-based Linux distributions, run the command underneath to put in Netcat.

On Crimson Hat-based Linux distributions, you’ll set up Netcat by way of working the command underneath.

Scanning a port the usage of nc

Netcat or nc can be utilized to scan ports. The syntax to scan a unmarried port is the next.

nc -zvn

As you’ll see, the syntax calls Netcat with the selected choices (defined underneath) adopted by way of the objective IP deal with and the objective port, as proven within the following sensible instance.

nc -zvn 80

The place:

  • -z: This feature is used to scan with out organising a connection.
  • -v: The verbosity choice prints the scan consequence.
  • -n: This feature is used to skip the DNS search for and warnings.

Scanning more than one ports with nc

 You can also use Netcat/nc to scan more than one ports. The syntax is equal to proven up to now; simply upload an area and the ports you wish to have to scan, as proven within the instance underneath by which ports 80, 22, and 53 are scanned.

nc -zvn 80 22 53

Scanning port levels with Netcat

 You’ll be able to scan port levels by way of imposing a hyphen, as proven within the following instance by which all ports from 80 to 89 (incorporated) are scanned.

nc -zvn 80-89

 Banner grabbing is a method used to be informed the device model working on a goal. Machine directors use it to stay a listing of on-device device. Hackers additionally use it as a part of the footprinting procedure.

Banner grabbing analyzes the community responses to check out to bet or be informed the device in the back of our goal. The next instance displays how the usage of nc or netcat with out choices (or the usage of the -v choice) results in data at the goal FTP server model.

As you’ll see, Netcat detected Natural-FTPd at the goal. The output additionally shall we us know nameless login is forbidden, and the server helps IPv6.

Moving information the usage of nc (Present listing)

 Netcat (nc) may be helpful to switch information between gadgets. Within the following instance, a record named linuxhint.txt will likely be transferred from a tool to some other.

The system the record will likely be despatched to at the receiving system, run the command underneath, the place the -l choice tells Netcat to stick in listening mode looking ahead to inbound connections. The -p choice defines the port quantity, and > [FileName] specifies the record to be gained. Run the command underneath at the receiving system, changing linuxhint.txt with the identify of the record you wish to have to switch.

nc -l -p 9899 > linuxhint.txt

The pc from which the record is being despatched at the sending system, run the command underneath the place the -w choice specifies the timeout (2 seconds on this case). At the sending system, the port doesn’t require the -p choice. The specifies the record to be despatched.

nc -w 2 9899

As you’ll see at the vacation spot system, the record used to be transferred as it should be.

Sending information saved in a special listing the usage of nc

 The former instance displays ship a record this is saved within the present listing. If the sender desires to ship a record that isn’t saved within the present listing, he can specify a trail to ship.

At the receiving system, run the similar command of the former instance with out adjustments, as proven underneath.

nc -l 9899 > linuxhint.txt

The one alternate we can practice at the sending system is the inclusion of the listing the place the record is saved. For this case, I moved the linuxhint.txt to the linuxhintdir listing. As you’ll see, the entire trail to the record is specified as linuxhintdir is a subdirectory of the present listing.

nc -w 2 9899

The record you wish to have to ship is within your house listing, and your present listing is other; the command carried out at the sender system will be the following.

nc -w 2 9899

Receiving information and storing them in a special listing the usage of nc

 Opposite to the former situation, the receiver won’t need to retailer the transferred record within the present listing. When enabling the listening mode for inbound connections at the receiving laptop, you’ll outline the listing to retailer information. The syntax is equal to when sending information from a subdirectory; simply specify the subdirectory and record identify as proven underneath.

nc -l -p 8999 > subdirectory/linuxhint.txt

On the second one laptop, use the similar instructions defined up to now; on this instance, the record to ship is saved in the house, no longer within the present listing.

nc -w 2 9899

And as you’ll see, the record is saved within the outlined listing. Additionally, the record identify modified from linuxhint2.txt to linuxhint.txt as outlined within the receiving laptop command.

Appearing record switch growth when the usage of nc

The pv command implementation shows the transference growth during the pipe. With this command, we will be able to upload growth data when the usage of Netcat.

To put in pv in Debian-based Linux distributions, run the command underneath.

Upload a pipe after the port at the receiving system, adopted by way of the pv command, the trail and record identify you wish to have to avoid wasting, as proven within the instance underneath.

netcat -l 9899 | pv > linuxhint2.txt

Ship the record from the sender system:

You’ll see the growth within the receiving system the place you added the pv command.

Compressing and shifting information at the fly with nc

 You can also compress information when sending thru Netcat with a unmarried execution the usage of pipe. The next instance displays compress the linuxhint2 listing and ship it thru netcat.

Depart nc listening for inbound connections; you’ll put into effect the pv command to look growth.

nc -l -p 9899 | pv > linuxhint2

At the sender, compress the listing or record the usage of the tar command with the right kind choices and proven underneath. Then upload pipe and ship as commonplace with out specifying the record, already specified when compressing.

tar cfvz – linuxhint2 | nc -w 2 9899

As you’ll see, the record used to be transferred and extracted correctly.

Moving a complete disk or partition the usage of nc

 This educational’s remaining instance displays switch a complete partition or disk the usage of Netcat.

At the receiving system, run the command underneath.

nc -p 9899 -l | bzip2 -d | dd of=/dev/sda1

At the sender, kind the next command, change /dev/sda1 for the disk or partition you wish to have to switch.

bzip2 -c /dev/sda1 | nc 9899


Netcat is an excessively elementary instrument any Linux consumer or consumer coping with networking will have to know. The usage of it’s beautiful easy, as proven on this educational. Netcat is designed for use by way of different techniques or scripts; this can be a just right help for builders.

I’m hoping this Netcat educational explaining 10 other usages used to be helpful to you. Stay following Linux Trace for extra Linux pointers and tutorials.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More