www.tecmint.com Open in urlscan Pro
2606:4700:20::681a:217  Public Scan

URL: https://www.tecmint.com/netcat-nc-command-examples/
Submission: On June 21 via manual from US — Scanned from DE

Form analysis 2 forms found in the DOM

GET https://www.tecmint.com/

<form method="get" class="search-form navigation-search" action="https://www.tecmint.com/">
  <input type="search" class="search-field" value="" name="s" title="Search">
</form>

POST https://www.tecmint.com/wp-comments-post.php

<form action="https://www.tecmint.com/wp-comments-post.php" method="post" id="commentform" class="comment-form" novalidate="">
  <p> Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published. </p>
  <p class="comment-form-comment"><label for="comment" class="screen-reader-text">Comment</label><textarea autocomplete="new-password" id="comment" name="b311dc7799" cols="45" rows="8" aria-required="true"></textarea><textarea
      id="adf70d9c403666476b9ccfdbc9a2a68a" aria-label="hp-comment" aria-hidden="true" name="comment" autocomplete="new-password"
      style="padding:0 !important;clip:rect(1px, 1px, 1px, 1px) !important;position:absolute !important;white-space:nowrap !important;height:1px !important;width:1px !important;overflow:hidden !important;" tabindex="-1"></textarea>
    <script data-noptimize="" type="text/javascript">
      document.getElementById("comment").setAttribute("id", "adf70d9c403666476b9ccfdbc9a2a68a");
      document.getElementById("b311dc7799").setAttribute("id", "comment");
    </script>
  </p><label for="author" class="screen-reader-text">Name</label><input placeholder="Name *" id="author" name="author" type="text" value="" size="30">
  <label for="email" class="screen-reader-text">Email</label><input placeholder="Email *" id="email" name="email" type="email" value="" size="30">
  <p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"> <label for="wp-comment-cookies-consent">Save my name, email, and website in this browser for the next time
      I comment.</label></p>
  <p class="form-submit"><input name="submit" type="submit" id="submit" class="submit" value="Post Comment"> <input type="hidden" name="comment_post_ID" value="33506" id="comment_post_ID">
    <input type="hidden" name="comment_parent" id="comment_parent" value="0">
  </p>
</form>

Text Content

WE VALUE YOUR PRIVACY

We and our partners store and/or access information on a device, such as cookies
and process personal data, such as unique identifiers and standard information
sent by a device for personalised ads and content, ad and content measurement,
and audience insights, as well as to develop and improve products.With your
permission we and our partners may use precise geolocation data and
identification through device scanning. You may click to consent to our and our
partners’ processing as described above. Alternatively you may click to refuse
to consent or access more detailed information and change your preferences
before consenting.Please note that some processing of your personal data may not
require your consent, but you have a right to object to such processing. Your
preferences will apply to this website only. You can change your preferences at
any time by returning to this site or visit our privacy policy.
AGREE DISAGREE MORE OPTIONS
Skip to content
Linux Foundation LFCS and LFCE Certification Preparation Guide - Get This Book
Menu
 * Home
 * About
 * eBooks
 * Shop
 * Donate
 * Linux Online Courses
 * Subscribe to Newsletter
 * Linux Hosting
 * A-Z Linux Commands
 * Get Involved
   * Testimonials
   * We are Hiring!
   * Submit Article Request
   * Suggest an Update



Menu
 * Linux Distro’s
   * CentOS Alternatives
   * Best Linux Distros
   * RedHat Distributions
   * Linux Distros for Beginners
   * Linux Server Distros
   * Debian Distributions
   * Ubuntu Distributions
   * Arch Linux Distros
   * Rolling Linux Distros
   * KDE Linux Distros
   * Secure Linux Distros
   * Linux Distros for Old PC
   * New Linux Distros
   * Linux Distros for Kids
   * Linux Distros for Windows Users
 * FAQ’s
 * Programming
   * Shell Scripting
   * Learn Python
   * Learn Awk
 * Linux Commands
 * Linux Tricks
 * Best Linux Tools
 * Certifications
   * RHCE Exam
   * RHCSA Exam
   * LFCE Exam
   * LFCS Exam
   * LFCA Exam
   * Ansible Exam
 * Guides
   * Hadoop Series
   * Docker Series
   * Postfix Mail
   * XenServer Series
   * RHEV Series
   * Clustering Series
   * LVM Series
   * RAID Series
   * KVM Series
   * iSCSI Series
   * Zentyal Series
   * Ansible Series
   * Django Series
   * Create GUI Apps
 * Monitoring Tools
   * Nagios
   * Zabbix
   * Cacti
   * Observium
   * Monitorix
   * Collectd
   * Collectl
   * MySQL Monitoring
 * 


8 NETCAT (NC) COMMAND WITH EXAMPLES

Aaron KiliAugust 5, 2019August 9, 2019 Categories Linux Commands 5 Comments


Netcat (or nc in short) is a simple yet powerful networking command-line tool
used for performing any operation in Linux related to TCP, UDP, or UNIX-domain
sockets.

Netcat can be used for port scanning, port redirection, as a port listener (for
incoming connections); it can also be used to open remote connections and so
many other things. Besides, you can use it as a backdoor to gain access to a
target server.

In this article, we will explain Netcat usage commands with examples.


HOW TO INSTALL AND USE NETCAT IN LINUX

To install the netcat package on your system, use the default package manager
for your Linux distribution.

$ yum install nc                  [On CentOS/RHEL]
$ dnf install nc                  [On Fedora 22+ and RHEL 8]
$ sudo apt-get install Netcat     [On Debian/Ubuntu]


Once netcat package installed, you can proceed further to learn the usage of
netcat command in the following examples.

PORT SCANNING



Netcat can be used for port scanning: to know which ports are open and running
services on a target machine. It can scan a single or multiple or a range of
open ports.

Here is an example, the -z option sets nc to simply scan for listening daemons,
without actually sending any data to them. The -v option enables verbose mode
and -w specifies a timeout for connection that can not be established.

$ nc -v -w 2 z 192.168.56.1 22     #scan a single port
OR
$ nc -v -w 2 z 192.168.56.1 22 80  #scan multiple ports
OR
$ nc -v -w 2 z 192.168.56.1 20-25  #scan range of ports


Scan for Open Ports in Linux

TRANSFER FILES BETWEEN LINUX SERVERS

Netcat allows you to transfer files between two Linux computers or servers and
both these systems must have nc installed.

For example, to copy an ISO image file from one computer to another and monitor
the transfer progress (using the pv utility), run the following command on the
sender/server computer (where the ISO file exists).

This will run nc in listening mode (-l flag) on port 3000.

$ tar -zcf - debian-10.0.0-amd64-xfce-CD-1.iso  | pv | nc -l -p 3000 -q 5


And on the receiver/client computer, run the following command to obtain the
file.

$ nc 192.168.1.4 3000 | pv | tar -zxf -


File Transfer Between Linux Systems

CREATE A COMMAND LINE CHAT SERVER

You can also use Netcat to create a simple command-line messaging server
instantly. As in the previous usage example, nc must be installed on both
systems used for the chat room.

On one system, run the following command to create the chat server listening on
port 5000.

$ nc -l -vv -p 5000


On the other system, run the following command to launch a chat session to a
machine where the messaging server is running.

$ nc 192.168.56.1 5000


Create Chat Server in Command Line

CREATE A BASIC WEB SERVER

Wit the -l option of nc command used to create a basic, insecure web server to
serve static web files for learning purposes. To demonstrate this, create a
.html file as shown.

$ vim index.html


Add the following HTML lines in the file.

<html>
        <head>
                <title>Test Page</title>
        </head>
        <body>
                      <p>Serving this file using Netcat Basic HTTP server!</p>
        </body>
</html>


Save changes in the file and exit.

Then serve the above file over HTTP by running the following command, which will
enables the HTTP server to run continuously.

$ while : ; do ( echo -ne "HTTP/1.1 200 OK\r\n" ; cat index.html; ) | nc -l -p 8080 ; done


Create Web server in Command line

Then open a web browser and can access the content using the following address.

http://localhost:8080
OR
http://SERVER_IP:8080


Test Web Server

Note that you can to stop the Netcat HTTP server by pressing [Ctrl+ C].

TROUBLESHOOT LINUX SERVER CONNECTION

Another useful usage of Netcat is to troubleshoot server connection issues.
Here, you can use Netcat to verify what data a server is sending in response to
commands issued by the client.

The following command retrieves the home page of example.com.

$ printf "GET / HTTP/1.0\r\n\r\n" | nc text.example.com 80


The output of the above command includes the headers sent by the web-server
which can be used for troubleshooting purposes.

FIND A SERVICE RUNNING ON PORT

You can also use Netcat to obtain port banners. In this case, it will tell you
what service is running behind a certain port. For example to know what type of
service is running behind port 22 on a specific server, run the following
command (replace 192.168.56.110 with the target server’s IP address). The -n
flag means to disable DNS or service lookups.

$ nc -v -n 192.168.56.110 80


Find Service Running on Port

CREATE A STREAM SOCKETS

Netcat also supports creation of UNIX-domain stream sockets. The following
command will create and listen on a UNIX-domain stream socket.

$ nc -lU /var/tmp/mysocket &
$ ss -lpn | grep "/var/tmp/"


Create Stream Socket in Command Line

CREATE A BACKDOOR

You can as well run Netcat as a backdoor. However, this calls for more work. If
Netcat is installed on a target server, you can use it to create a backdoor, to
get a remote command prompt.

To act a backdoor you need Netcat to listen on a chosen port (e.g port 3001) on
the target server and then you can connect to this port from your machine as
follows.

This is the command intended to run on the remote server where the -d option
disables reading from stdin, and -e specifies the command to run on the target
system.

$ nc -L -p 3001 -d -e cmd.exe 


Last but not least, Netcat can be used as a proxy for different
services/protocols including HTTP, SSH, and many more. For more information, see
its man page.

$ man nc


In this article, we have explained 8 practical Netcat command usage examples. If
you know any other practical use case(s), share with us via the feedback form
below. You can ask a question as well.

Tags nc command examples, netcat command examples
Post navigation
How to Install MariaDB Database in Debian 10
How to Install Apache with Virtual Hosts on Debian 10
If you liked this article, then do subscribe to email alerts for Linux
tutorials. If you have any questions or doubts? do ask for help in the comments
section.



IF YOU APPRECIATE WHAT WE DO HERE ON TECMINT, YOU SHOULD CONSIDER:

TecMint is the fastest growing and most trusted community site for any kind of
Linux Articles, Guides and Books on the web. Millions of people visit TecMint!
to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as
a token of appreciation.



We are thankful for your never ending support.


RELATED POSTS

How to Run a Linux Command Without Saving It in History

How to Run a Command Multiple Times in Linux

How to Disable ‘su’ Access for Sudo Users

How to Convert a /Home Directory to Partition in Linux

30 Useful Linux Commands for System Administrators

10 Wget (Linux File Downloader) Command Examples in Linux




5 THOUGHTS ON “8 NETCAT (NC) COMMAND WITH EXAMPLES”

 1. Rakshith
    June 20, 2022 at 3:17 pm
    
    The package name is not `Netcat`, its `netcat`.
    
    Reply
    
 2. Siva
    December 16, 2020 at 10:52 pm
    
    nc is failing for multiple ports & range
    
    $ nc -v -w 2 -z 10.244.169.10 1 2
    Ncat: Version 7.50 ( https://nmap.org/ncat )
    Ncat: Got more than one port specification: 1 2. QUITTING.
    
    
    Reply
    
 3. Vedant Pareek
    July 13, 2020 at 4:29 pm
    
    In the first example, you should make it -z rather than z.
    
    Thanks
    
    Reply
    
 4. Jared
    August 10, 2019 at 11:35 am
    
    You can pair netcat with tcpdump to stream a packet capture to another host.
    This is useful for capturing in real time traffic on busybox or other small
    Linux distributions.
    
    I have had luck using tcpdump for android on busybox running a security
    camera.
    
    Reply
    * Aaron Kili
      August 19, 2019 at 2:44 pm
      
      @Jared
      
      Great, I will do more research on this and practically test it. Many
      thanks for sharing.
      
      Reply
      
    


GOT SOMETHING TO SAY? JOIN THE DISCUSSION. CANCEL REPLY

Have a question or suggestion? Please leave a comment to start the discussion.
Please keep in mind that all comments are moderated and your email address will
NOT be published.

Comment

Name Email

Save my name, email, and website in this browser for the next time I comment.



Over 3,500,000+ Readers

A Beginners Guide To Learn Linux for Free [with Examples]

Red Hat RHCSA/RHCE 8 Certification Study Guide [eBooks]
Linux Foundation LFCS and LFCE Certification Study Guide [eBooks]


LEARN LINUX COMMANDS AND TOOLS

15 Tips On How to Use ‘Curl’ Command in Linux



20 Advanced Commands for Middle Level Linux Users



How to Test Website Loading Speed in Linux Terminal



Let Sudo Insult You When You Enter Incorrect Password



Gogo – Create Shortcuts to Long and Complicated Paths in Linux



DEBUGFS Command – Show File Creation Times in Linux



If You Appreciate What We Do Here On TecMint, You Should Consider:







LINUX SERVER MONITORING TOOLS

How to Monitor Ubuntu Performance Using Netdata



Limit CPU Usage of a Process in Linux with CPULimit Tool



How to Monitor System Usage, Outages and Troubleshoot Linux Servers – Part 9



Bandwhich – A Network Bandwidth Utilization Tool for Linux



How to Set Linux Process Priority Using nice and renice Commands



Install Mtop (MySQL Database Server Monitoring) in RHEL/CentOS 6/5/4, Fedora
17-12




LEARN LINUX TRICKS & TIPS

How to Convert Files to UTF-8 Encoding in Linux



How to Auto Execute Commands/Scripts During Reboot or Startup



How to Keep ‘sudo’ Password Timeout Session Longer in Linux



How to Split Large ‘tar’ Archive into Multiple Files of Certain Size



How to Block or Disable Normal User Logins in Linux



How to Find Out Top Directories and Files (Disk Space) in Linux




BEST LINUX TOOLS

5 Best Modern Linux ‘init’ Systems (1992-2015)



7 Best Command-Line Email Clients for Linux in 2020



20 Useful Security Features and Tools for Linux Admins



8 Top Open Source Reverse Proxy Servers for Linux



16 Open Source Cloud Storage Software for Linux in 2020



27 Best IDEs for C/C++ Programming or Source Code Editors on Linux


 * Donate to TecMint
 * Contact Us
 * Advertise on TecMint
 * Linux Services
 * Copyright Policy
 * Privacy Policy
 * Career
 * Sponsored Post

Tecmint: Linux Howtos, Tutorials & Guides © 2022. All Rights Reserved.

The material in this site cannot be republished either online or offline,
without our permission.

Hosting Sponsored by : Linode Cloud Hosting

Scroll back to top