Linux netcat (nc) Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn about the Netcat (nc) command, a powerful networking tool that allows you to establish connections, transfer files, and perform various network-related tasks. You will start by installing Netcat on an Ubuntu 22.04 Docker container and then explore its basic usage, including setting up a simple server-client communication and transferring files between the two.

The lab covers the following steps:

  1. Introduction to Netcat (nc) Command
  2. Netcat Server and Client Communication
  3. Transferring Files Using Netcat

Please note that the Netcat command is a standard tool in Linux, and no additional installation is required.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/RemoteAccessandNetworkingGroup -.-> linux/telnet("`Network Connecting`") linux/RemoteAccessandNetworkingGroup -.-> linux/netstat("`Network Monitoring`") linux/RemoteAccessandNetworkingGroup -.-> linux/nc("`Networking Utility`") subgraph Lab Skills linux/cat -.-> lab-422836{{"`Linux netcat (nc) Command with Practical Examples`"}} linux/echo -.-> lab-422836{{"`Linux netcat (nc) Command with Practical Examples`"}} linux/telnet -.-> lab-422836{{"`Linux netcat (nc) Command with Practical Examples`"}} linux/netstat -.-> lab-422836{{"`Linux netcat (nc) Command with Practical Examples`"}} linux/nc -.-> lab-422836{{"`Linux netcat (nc) Command with Practical Examples`"}} end

Introduction to Netcat (nc) Command

In this step, you will learn about the Netcat (nc) command, a powerful networking tool that allows you to establish connections, transfer files, and perform various network-related tasks.

Netcat is a command-line utility that can be used as both a client and a server. It is often referred to as the "Swiss Army knife" of networking tools due to its versatility and wide range of applications.

Let's start by installing Netcat on our Ubuntu 22.04 Docker container:

sudo apt-get update
sudo apt-get install -y netcat

Example output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  netcat-openbsd
The following NEW packages will be installed:
  netcat netcat-openbsd
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.

Now that we have Netcat installed, let's explore some of its basic usage.

Netcat Server and Client Communication

In this step, you will learn how to use Netcat to establish a simple server-client communication.

First, let's start a Netcat server on one terminal:

nc -l -p 8000

This command starts a Netcat server that listens on port 8000 for incoming connections.

Now, in another terminal, let's connect to the server as a client:

nc 127.0.0.1 8000

This will connect the client to the server running on the local machine (127.0.0.1) on port 8000.

Once the connection is established, you can type a message in either the server or the client terminal, and it will be displayed on the other side. For example, type a message in the client terminal and press Enter:

Hello, server!

You should see the message appear in the server terminal:

Hello, server!

To end the communication, simply type Ctrl+C in either the server or the client terminal.

Transferring Files Using Netcat

In this step, you will learn how to use Netcat to transfer files between a server and a client.

First, let's create a sample file to transfer:

echo "This is a test file." > test_file.txt

Now, let's start the Netcat server to receive the file:

nc -l -p 8000 > received_file.txt

This command starts a Netcat server that listens on port 8000 and redirects any incoming data to a file named received_file.txt.

In another terminal, let's connect to the server as a client and send the test_file.txt file:

cat test_file.txt | nc 127.0.0.1 8000

This command reads the contents of test_file.txt and sends it to the Netcat server running on the local machine (127.0.0.1) on port 8000.

Once the file transfer is complete, you can check the received_file.txt file in the server's directory to verify that the file was transferred successfully:

cat received_file.txt

You should see the contents of the test_file.txt file.

Summary

In this lab, you learned about the Netcat (nc) command, a powerful networking tool that allows you to establish connections, transfer files, and perform various network-related tasks. You started by installing Netcat on an Ubuntu 22.04 Docker container. Then, you explored how to use Netcat to set up a simple server-client communication, where you could send messages between the server and client terminals. Finally, you learned how to use Netcat to transfer files between two systems.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like