How to Use the nc Command in Linux

Updated on November 15, 2024
How to Use the nc Command in Linux header image

Introduction

The nc (Netcat) command in Linux is a versatile networking utility for reading and writing data between networks. The nc command is also referred to as the "Swiss army knife" of networking because it performs various tasks. These networking tasks include transferring files, scanning ports, encrypting data, opening shells, and more.

This article explains how to use the nc command in Linux to administer a system network.

Prerequisites

Before you begin:

The nc Command Syntax

The following is a basic nc command syntax:

nc [options] [hostname] [port]

In the above nc command:

  • [options]: Includes optional flags that modify the command's behavior
  • [hostname]: Specifies the target host.
  • [port]: Specifies the target port to connect.

The nc command in Linux works in two modes:

  • Connect mode: Netcat works as a client in connect mode and requires the host and port parameters.
  • Listen mode: Netcat works as a server in listen mode. Netcat listens on all available addresses and the specified port when you omit the host option.

Explore the egrep command in Linux to efficiently search for specific text patterns in files.

Use Common nc Command Options

You can use the following command options when working with the nc command:

Option Description
-l Listen mode. Sets up a listening socket.
-p Local port. Specifies the target port number to connect to.
-u UDP mode. Enables UDP instead of TCP.
-z Zero-I/O mode. Scans and detects open ports on the target host.
-v Verbose mode. Provides detailed command output.
-w Timeout. Sets a timeout value for connections.
-e Execute. Runs a specified command after establishing a connection.
-n Numeric-only mode. Disables DNS resolution.
-k Keep open. Keeps the connection open when the client disconnects.
-c Shell command. Executes a command as a single argument to the shell.

Use the nc Command to Perform Tasks in Linux

Follow the steps below to use the nc command to perform basic tasks in Linux, such as creating a chat server, transferring files, and scanning open network ports.

  1. Allow network connection on the custom 12345 TCP port through the default firewall.

    console
    $ sudo ufw allow 12345
    
  2. Reload the firewall to apply changes.

    console
    $ sudo ufw reload
    
  3. Create a new chat server using the nc command on the main server which listens for connections on port 12345.

    console
    $ nc -lv 12345
    

    The above command runs the nc command in listen mode using the -l option on the TCP port 12345. The -v option enables verbose mode to display detailed output.

  4. Establish an SSH connection in the client server and connect to the chat server. Replace Server-IP with the main server's public IP address.

    console
    $ nc -v Server-IP 12345
    

    Output:

    Connection to 10.50.112.4 12345 port [tcp/*] succeeded!

    Enter Hello World in the nc command prompt to send a message to the main server.

    Hello World

    Verify that the Hello World message displays on the main server, and enter Greetings from Vultr! as a reply.

    Greetings from Vultr!

    Press Ctrl + C to exit the nc command prompt.

    Output:

    Run a Chat Server using the nc Command

  5. Create a new file1.txt file on the main server.

    console
    $ touch file1.txt
    
  6. Redirect the file to the nc command to transfer it to any connecting client.

    console
    $ nc -lv 12345 < file1.txt
    
  7. Download the file using the nc command on the client server.

    console
    $ nc -zv Server-IP 12345 > file1.txt
    

    Output:

    Connection to 10.50.112.4 12345 port [tcp/*] succeeded!
  8. List files in the working directory on the client server and verify that file1.txt file is available.

    console
    $ ls
    

    Output:

    file1.txt

    Transfer files using the nc command in Linux

  9. Scan all open ports between 20 and 25 using the z option without sending data to the main server.

    console
    $ nc -zvw 1 Server-IP 20-25
    

    Output:

    Scan Open Ports using the nc command

  10. Filter the port results using the grep command to display only open ports.

    console
    $ nc -zvw 1 [target_host] 20-25 2>&1 | grep 'succeeded'
    

    Output:

    Filtered Port Scan

  11. Create a new index.html file using a file editor such as nano to set up a basic web application on the main server.

    console
    $ nano index.html
    
  12. Add the following contents to the file.

    html
    HTTP/1.1 200 OK
    Content-Type: text/html
    
    <html>
      <head>
        <title>Test Page</title>
      </head>
      <body>
        <h1>Greetings from Vultr!</h1>
      </body>
    </html>
    

    Save and close the file.

  13. Serve the web application file using a loop and the nc command.

    console
    $ while : ; do cat index.html | nc -l -p 12345; done
    

    The above command starts an infinite while loop that listens for connections on the TCP port 12345 and serves the index.html web application file when you access the application using the server's public IP address.

    Output:

    Basic Web Server

  14. Run the web application by accessing the main server's IP address on port 12345 using a web browser such as Chrome.

    http://SERVER_IP:12345

    Output:

    Serve a Web Application using the nc Command

  15. Test access to a specific port on a domain or server IP using the nc command. For example, test access to the HTTPS port 443 on google.com and specify the z option to disable persistence.

    console
    $ nc -zv google.com 443
    

    Output:

    Port Ping

  16. Send an HTTP request to a domain using the nc command. For example, use the print command to send a request to google.com on the HTTP port 80.

    console
    $ printf "GET / HTTP/1.0\r\n\r\n" | nc -v google.com 80
    

    Output:

    HTTP Request

Explore how the top command in Linux provides real-time monitoring for system performance.

Use the nc Command with Compression

You can use the nc command with compression tools to speed up remote file transfers. Follow the steps below to use the nc command with compression to transfer files.

  1. Create a new dir directory on the main server.

    console
    $ mkdir dir
    
  2. Add new files to the dir directory.

    console
    $ touch dir/example{1..3}.txt
    
  3. Switch to the dir directory.

    console
    $ cd dir
    
  4. Compress the directory files using tar and send the contents to the nc command.

    console
    $ tar -cf - . | nc -lv 12345
    
  5. Run the following command on the client server to receive and decompress the files from the main server.

    console
    $ nc -v Server-IP 12345 | tar xfv -
    

    Output:

    Connection to 10.50.112.3 12345 port [tcp/*] succeeded!
    ./
    ./example2.txt
    ./example3.txt
    ./example1.txt

    Directory Transfer

Conclusion

You have used the nc command in Linux to perform basic networking tasks, such as transferring files, scanning open ports, and running application files. You can use the nc command to manage and troubleshoot network connections between two or more Linux hosts. For more information and options, run the man nc command to view the nc command's manual page.