[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
CentOSMay 2, 2023

How to use nc command.

How To Use nc Command

Netcat (or nc) is a command-line utility that reads and writes data across network connections, using the TCP or UDP protocols. It is one of the most powerful tools in the network and system administrators arsenal, and it as considered as a Swiss army knife of networking tools.

Netcat is cross-platform, and it is available for Linux, macOS, Windows, and BSD. You can use Netcat to debug and monitor network connections, scan for open ports, transfer data, as a proxy, and more.

The Netcat package is pre-installed on macOS and popular Linux distributions like Ubuntu, Debian or CentOS.

Netcat Syntax

The most basic syntax of the Netcat utility takes the following form:

nc [options] host port

On Ubuntu, you can use either netcat or nc. They are both symlinks to the openBSD version of Netcat.

By default, Netcat will attempt to start a TCP connection to the specified host and port. If you would like to establish a UDP connection, use the -u option:

nc -u host port

Port Scanning

Scanning ports is one of the most common uses for Netcat. You can scan a single port or a port range.

For example, to scan for open ports in the range 20-80 you would use the following command:

nc -z -v 89.45.12.1 20-80

The -z option will tell nc to only scan for open ports, without sending any data to them and the -v option to provide more verbose information.

The output will look something like this:

nc: connect to 89.45.12.1 port 20 (tcp) failed: Connection refused
nc: connect to 89.45.12.1 port 21 (tcp) failed: Connection refused
Connection to 89.45.12.1 22 port [tcp/ssh] succeeded!
nc: connect to 89.45.12.1 port 23 (tcp) failed: Connection refused
...
nc: connect to 89.45.12.1 port 79 (tcp) failed: Connection refused
Connection to 89.45.12.1 80 port [tcp/http] succeeded!

If you want to print only the lines with the open ports, you can filter the results with the grep command.

nc -z -v 89.45.12.1 20-80 2>&1 | grep succeeded
Connection to 89.45.12.1 22 port [tcp/ssh] succeeded!
Connection to 89.45.12.1 80 port [tcp/http] succeeded!

You can also use Netcat to find the server software and its version. For example, if you send an “EXIT” command to the server on the default SSH port 22 :

echo "EXIT" | nc 89.45.12.1 22

The output will look something like this:

SSH-2.0-OpenSSH_7.6p1 Ubuntu-4
Protocol mismatch.

To scan for UDP ports simply add the -u option to the command as shown below:

nc -z -v -u 89.45.12.1 20-80

Typically, Nmap is a better tool than Netcat for complex port scanning.

Sending Files through Netcat

Netcat can be used to transfer data from one host to another by creating a basic client/server model.

This works by setting the Netcat to listen on a specific port (using the -l option) on the receiving host and then establishing a regular TCP connection from the other host and sending the file over it.

On the receiving run the following command which will open the port 5555 for incoming connection and redirect the output to the file:

nc -l 5555 > file_name

From the sending host connect to the receiving host and send the file:

nc receiving.nexonhost.com 5555 < file_name

To transfer a directory you can use tar to archive the directory on the source host and to extract the archive on the destination host.

On the receiving host, set the Netcat tool to listen for an incoming connection on port 5555. The incoming data is piped to the tar command, which will extract the archive:

nc -l 5555 | tar xzvf -

On the sending host pack the directory and send the data by connecting to the listening nc process on the receiving host:

tar czvf - /path/to/dir | nc receiving.nexonhost.com 5555

You can watch the transfer progress on both ends. Once completed, type CTRL+L to close the connection.

Creating a Simple Chat Server

The procedure for creating an online chat between two or more hosts is the same as when transferring files.

On the first host start a Netcat process to listen on port 5555:

nc -l 5555

From the second host run the following command to connect to the listening port:

nc first.nexonhost.com 5555

Now, if you type a message and press ENTER it will be shown on both hosts.

To close the connection, type CTR+C.

Performing an HTTP request

Although there are much better tools for HTTP requests such as curl, you can also use Netcat to send various requests to remote servers.

For example, to retrieve the Netcat man page from the OpenBSD web site, you would type:

printf "GET /nc.1 HTTP/1.1\r\nHost: man.openbsd.org\r\n\r\n" | nc man.openbsd.org 80

The full response, including the HTTP headers and HTML code, will be printed in the terminal.

Conclusion

In this tutorial, you have learned how to use the Netcat utility to establish and test TCP and UDP connections.

Keep reading
How to use dpkg command in linux.
October 12, 2023

How to use dpkg command in linux.

How To Use Dpkg Command In Linux. This guide explores the dpkg command syntax and options through various examples. dpkg in Linux is the primary package manager for Debian and Debian-based systems, such as Ubuntu. The tool installs, builds, removes, configures, and retrieves information for Debian…

Read article
How to use cksum command in Linux.
October 11, 2023

How to use cksum command in Linux.

How To Use Cksum Command In Linux. On Linux there are so many essential commands to perform various tasks or install the applications. One of the essential commands is cksum command which you can use to display a CRC (Cyclic Redundancy Check) value, the byte size of the file to standard output. In…

Read article
How to use cpio command in Linux.
October 11, 2023

How to use cpio command in Linux.

How To Use Cpio Command In Linux. Copy in and copy out are referred to as cpio. In Linux/UNIX, it is a command-line tool to generate, list down, and extract archive files. A cpio archive file is produced by it. The entire file system can be copied to an archive file. From the standard input,…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00