Skip to content

What is nc Linux command?

The Linux nc command, also known as netcat, is a versatile networking tool used for reading and writing data across network connections using TCP or UDP. It can also be used for port scanning, transferring files, as a simple network server, and much more.

nc Syntax:

Terminal window
nc [options] [destination] [port]

nc Options:

OptionDescription
-hDisplay help message and exit.
-lUsed to specify to operate in listening mode.
-pSpecify source port.
-uUse UDP instead of the default TCP.
-vIncrease verbosity level.

Parameters:

ParameterDescription
destinationHostname or IP address of the target.
portPort number to connect to.

nc Command Usage Examples:

Send Text to a Server

Terminal window
echo "Hello, world!" | nc example.com 80

Sends the text “Hello, world!” to the server at example.com on port 80.

Check if a Remote Host is Reachable

Terminal window
nc -zv example.com 443

Checks if the host example.com on port 443 is reachable using the -z and -v flags.

Transfer Files Over the Network

Terminal window
nc -l -p 1234 > received_file.txt

Starts a TCP server on port 1234 and saves the received data to the file received_file.txt.

Connect to a Remote SSH Server

Terminal window
nc -v example.com 22

Initiates a connection to the SSH server at example.com on port 22 using the -v flag for verbosity.

Port Scanning

Terminal window
nc -zv example.com 1-100

Scans ports 1 to 100 on the host example.com to check for open ports using the -z and -v flags.

How do I use nc in Linux?

To use the nc command in bash, execute the following command:

Terminal window
nc --option <value>

What is the purpose of the nc command?

The nc command, also known as netcat, is used for reading/writing data across network connections using TCP or UDP protocols.

Terminal window
nc -l -p 1234

How can I listen for incoming connections with nc?

To listen for incoming TCP connections on a specific port using nc, use the following command:

Terminal window
nc -l -p <port_number>

How can I perform a port scan using nc?

To perform a port scan on a specific target using nc, you can iterate over a range of ports with the following command:

Terminal window
nc -zv <target_ip> <start_port>-<end_port>

How do I transfer files using nc?

To transfer a file from one host to another using nc, you can accomplish this by redirecting the file content through the network connection. On the receiving end, output the content to a specified file.

Terminal window
nc -l -p 1234 > received_file.txt

How can I check if a port is open using nc?

To check if a specific port is open on a target host, you can connect to it using nc and see if the connection is successful.

Terminal window
nc -zv <target_ip> <port_number>

How do I create a simple web server using nc?

You can create a simple web server using nc to serve static files. For example, to serve a single HTML file, you can run the following command:

Terminal window
while true; do nc -l -p 8080 -q 1 < index.html; done

How can I use nc for chat-like communication between two hosts?

To establish a chat-like communication between two hosts using nc, run nc on both hosts in listening mode to read and write messages.

Terminal window
# On host A
nc -l -p 1234
# On host B
nc <host_A_IP> 1234

Applications of the nc command

  • Port scanning
  • Network debugging and exploration
  • Transferring files
  • Remote shell access
  • Chat/Messaging
  • Forwarding or redirecting ports
  • Network daemon testing
  • Proxying network connections