Practical Uses of nc(netcat) command in Linux

Netcat is one of the powerful networking tool, security tool or network monitoring tool. It acts like cat command over a network. It is even considered as a Swiss army knife of networking tools.
It is generally used for the following reasons:

  • Operation related to TCP, UDP or UNIX-domain sockets
  • Port Scanning
  • Port listening
  • Port redirection
  • open Remote connections
  • Read/Write data across network
  • Network debugging
  • Network daemon testing
  • Simple TCP proxies
  • A Socks or HTTP Proxy Command for ssh

It is designed by keeping in mind that it should be a flexible “back-end” tool that can be used directly or driven by any other program.

Installing netcat(nc) Process Monitoring Tool

To install the Netcat tool use the following commands as per your Linux distribution.

In case of Debian/Ubuntu

$sudo apt-get install netcat

In case of CentOS/RHEL



$yum install nc 

In case of Fedora 22+ and RHEL 8

$dnf install nc

Working with netcat Security Tool

1. To start listening on a port, first Open 2 terminal windows.

Terminal 1 for listening

$nc -l -p 1234

Terminal 2 sending request

$nc 127.0.0.1 1234

Note: Here the port number is 1234 and by default host is localhost.

using netcat command to send message between two terminals

It will not display anything but will start listening to port 1234 at the localhost from terminal 1. And anything entered in terminal 2 will be reflected back in terminal 1 as well which confirms that the connection is established successfully.

2. To transfer data. Open 2 terminal windows.



Terminal 1 for listening

$nc -l -p 1234 >output.txt

Terminal 2 for sending request

$echo "GeeksforGeeks" >input.txt
$nc 127.0.0.1 1234 <input.txt

Note: Here the port number is 1234 and by default host is localhost. It will send the input.txt file’s data from terminal 2 to the output.txt file at terminal 1.

Transferring-the-Data-using-Netcat-Command-in-Linux-1

Transferring-the-Data-using-Netcat-Command-in-Linux-2

3. To perform Port Scanning. Enter the following command on the terminal.

Scanning a single port

$netcat -z -v 127.0.0.1 1234

Scanning multiple ports

$nc -z -v 127.0.0.1 1234 1235

Scanning a range of ports

$nc -z -v 127.0.0.1 1233-1240

Note: Here the port numbers are 1234, 1235, 1233, and 1240 you may change them as per your need. It will display the port number with the status(open or not).



Port-Scanning-Using-Netcat-Command-in-Linux

4. To send an HTTP Request

$printf “GET /nc.1 HTTPs/1.1\r\nHost: www.geeksforgeeks.org\r\n\r\n” | nc www.geeksforgeeks.org 80

Note: Here the website is www.geeksfrogeeks.org, you may choose any. It will send a HTTP Request to www.geeksfrogeeks.org.

To-send-an-HTTP-Request-Using-Netcat-Command-in-Linux

5. To delay the interval for lines sent. Open 2 terminal as shown below:

Terminal 1 for listening

$nc -l -p 1234

Terminal 2 sending request

$nc -i 5 127.0.0.1 1234

Note: Here the port number is 1234 and by default host is localhost. The time taken is 5 seconds. Each will be sent after 5 seconds of time.

To delay the interval for lines sent using netcat command in Linux

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : manav014