Jannah Theme License is not validated, Go to the theme options page to validate the license, You need a single license for each domain name.
Linux

Netcat Command (nc) in Linux with Examples

What is nc Command?

Netcat, often abbreviated as ‘nc,’ is a versatile networking utility that has been a favorite tool among system administrators, security professionals, and enthusiasts alike. Linux Netcat command’s flexibility and simplicity make it an invaluable asset in various scenarios, from basic network troubleshooting to complex data transfer tasks. With this blog, we delve into the world of networking and explore the powerful capabilities of the nc command in Linux. 

The Netcat command serves as a swiss army knife for networking, offering a plethora of functionalities. This permits users to establish connections, read and write data across networks, scan for open ports, and perform network testing and analysis. Its wide-ranging applications make it a go-to choice for seasoned professionals and beginners seeking to explore the depths of networking.

In this blog, we are going to take you through the fundamentals of Netcat, understand its syntax, and grasp its multiple modes of operation. Furthermore, we will demonstrate practical examples showcasing how to utilize the Linux Netcat command in real-world situations. Whether you aim to send files between systems, conduct banner grabbing, or create backdoors for remote access, Netcat has got you covered. Continue reading and back yourself with the insights and skills to harness its full potential.

Netcat Command Syntax

The fundamental structure of the Linux NC command involves specifying the target host and port to establish a connection. The syntax is as follows:

nc [options] host port

In this command:

  • nc: The Netcat command itself.
  • [options]: Optional flags that modify the Netcat behavior.
  • host: Refers to the IP address or hostname of the target system you want to connect to.
  • port: Denotes the specific port number on which the connection will be established.

File Transfers: Netcat enables seamless file transfers between systems. The syntax for sending and receiving files is as follows:

  • Sending: nc [options] host port < file
  • Receiving: nc -l -p port > file

Chat Mode: Netcat can be used for two-way communication in chat mode by running the following commands:

  • On the receiving side: nc -l -p port
  • On the sending side: nc host port

Netcat (nc) Command Options

Here are some critical options under Netcat command in Linux: 

Verbose Output (-v):

The -v option enables verbose mode, providing detailed feedback during connections. It displays informative messages, including connection status, data transfer statistics, and error notifications. This option is invaluable for monitoring and troubleshooting network interactions, giving you real-time insights into the communication process.

Listening Mode (-l):

The -l option activates Netcat’s listening mode, allowing it to accept incoming connections. It turns your system into a server, ready to receive data or commands from remote clients. Listening mode is especially useful when setting up servers for remote access or waiting for connections from multiple clients.

Specifying Local Port (-p):

With the -p option, you can designate a specific local port for the connection. This is helpful when you want to ensure a connection through a particular port, is useful for services that require specific port configurations.

Scanning for Open Ports (-z):

The -z option lets you scan for open ports on a target system without initiating an actual connection. This aids in identifying potential entry points or assessing network security. Netcat quickly checks for open ports and gives a summary of available services.

TCP/UDP Mode (-u):

The -u option instructs Netcat to use UDP (User Datagram Protocol) instead of the default TCP (Transmission Control Protocol). This option is beneficial for working with UDP-based services, such as DNS or DHCP, where connectionless communication is required.

Source Port Selection (-s):

The -s option allows you to specify the source port for the connection. This can be useful in cases where the target system requires data from a specific source port or to bypass certain firewall rules.

Timeout Configuration (-w):

The -w option sets a connection timeout value, specifying the time in seconds that Netcat will wait for a response from the target system. This ensures that the command does not hang indefinitely if the connection attempt is unsuccessful.

Also Read: 14 Tar Command in Linux

nc Command Examples

Client/Server Connection

To establish a basic client/server connection using nc command in Linux, follow these steps:

On the server side (listening for connections):

  1. Open a terminal or command prompt.
  2. Run the mentioned command for listening to incoming connections on a particular port (e.g., port 12345): nc -lv -p 1234

On the client side (connecting to the server):

  1. Open another terminal or command prompt on a different machine.
  2. Execute the mentioned command to connect to the server’s IP address (replace “server_ip” with the actual IP address) and the same port used on the server (12345 in this example): nc -v server_ip 12345
Client/Server Connection

Ping Specific Port on Website

You can check if a specific port is open on a website using Netcat:

  1. Open a terminal or command prompt.
  2. Enter the following command, replacing “example.com” with the website’s domain and “port_number” with the port you want to check (e.g., 80 for HTTP): 
nc -vz example.com port_number

Scanning Ports

nc command in Linux can be used to scan for open ports on a target system:

  1. Open a terminal or command prompt.
  2. Run the following command, replacing “target_ip” with the IP address you want to scan and “start_port” and “end_port” with the range of ports to scan (e.g., 1-1000):
nc -zv target_ip start_port-end_port

Transfer Files

Linux nc command allows for easy file transfer between two systems:

On the receiving side:

  1. Open a command prompt or terminal
  2. Run the command below to listen on a specific port (e.g., port 12345):
nc -l -p 12345 > received_file
Transfer Files

On the sending side:

  1. Open another terminal or command prompt on the source system.
  2. Use the following command to send the file to the receiving system’s IP address (replace “receiver_ip” with the actual IP address) and the same port used for listening (12345 in this example):
nc receiver_ip 12345 < file_to_send

Transfer Directory

Netcat command in Linux can also transfer an entire directory between systems:

On the receiving side:

  1. Open a terminal or command prompt.
  2. Run the following command to listen on a specific port (e.g., port 12345):
nc -l -p 12345 | tar xvf -

On the sending side:

  1. Open another terminal or you can also open a command prompt on the source system.
  2. Use the following command to send the compressed directory to the receiving system’s IP address (replace “receiver_ip” with the actual IP address) and the same port used for listening (12345 in this example):
tar cvf - directory_to_send | nc receiver_ip 12345

Create Web Server

Netcat command in Linux can quickly set up a simple web server for testing purposes:

  1. Open a terminal or you can open a command prompt.
  2. Navigate to the directory possessing the files you wish to serve.
  3. Implement the below command for beginning the web server on a specific port (e.g., 8080):
nc -l -p 8080 < index.html

Simple Chat Server

Netcat can be used to create a basic chat server:

  1. Open a terminal or command prompt.
  2. Run the below command to start the chat server on a specific port (e.g., 12345):
nc -l -p 12345

Send HTTP Request

Netcat possesses the capability to send HTTP requests to a web server:

  1. Open a terminal or command prompt.
  2. Enter the below command, replacing “example.com” with the website’s domain and “80” with the port number:
echo -e "GET / HTTP/1.1\nHost: example.com\n\n" | nc example.com 80

These nc command examples provide a glimpse of the power and versatility of the nc command in Linux. Feel free to experiment and explore further to unleash its full potential in various networking tasks.

Also Read: Learn to use SCP Command in Linux

Conclusion

The nc command in Linux stands as a powerful and flexible tool that empowers networking enthusiasts and professionals alike. Throughout this guide, we have explored various nc command examples showcasing its vast capabilities, from basic client/server connections to complex file transfers and even creating simple web servers.

By mastering the Netcat command syntax and understanding its numerous options, you now possess a valuable skill set to tackle a wide array of networking tasks with efficiency and precision. Whether you’re a system administrator, security analyst, or curious enthusiast, Netcat has proven to be an indispensable addition to your networking arsenal.

The ability to troubleshoot connections, identify open ports, and send and receive data seamlessly opens up endless possibilities for both simple tasks and intricate network setups. The straightforward command-line interface allows for quick and direct control, making nc command in Linux an ideal choice for automation and scripting purposes.

As you continue to explore the world of networking, remember that practice is key to mastering any tool. Experiment with Netcat in controlled environments, and do not hesitate to reference this guide for assistance. Additionally, keep an eye on updates and new features in the ever-evolving networking landscape. We believe this detailed guide has rendered you with a robust base to harness the full potential of Netcat command in Linux.

Arpit Saini

He is the Director of Cloud Operations at Serverwala and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles