How to Send a File Using Netcat and Then Keep the Connection Alive
Last Updated :
08 Dec, 2023
Netcat (NC) is a popular networking utility tool for reading and writing data across network connections using TCP or UDP protocols. It can be used for both attack and security, in the case of attacking. It helps us to debug the network along with investigating it. It runs on all operating systems. Netcat (NC) is a well-known systems administration application that might be utilized to trade records and hold associations open for additional verbal trade between frameworks.
Getting Started with Netcat ( NC )
Netcat (NC) is mostly used in Network Security, Network Administration, and cyber Operations while netcat has many potential uses, it sees most of its utilization in security, networking, programming, infrastructure troubleshooting, and cloud/virtualization environments. Its flexibility makes it one of the most multipurpose networking utilities available.
Prerequisites :
On the Computer Sending the File (Sender):
- Install a program called Netcat. This lets you use commands like "nc" to connect to other computers.
- Have the file you want to send saved on your computer already.
- Know the IP address of the receiving computer. This is a number that identifies it.
- Know what port number the receiving computer is listening on. Think of ports like numbered doors - you need to knock on the right one for them to answer!
On the Computer Receiving the File (Receiver):
- Install the Netcat program.
- Run a command to listen for connections on a chosen port. Like "nc -l 1234".
- Make sure that the port number can accept connections from the sender's computer through any firewalls.
- You can save whatever is received directly to a file by adding on "> received_file.txt"
For the Network:
- The port number must be open between the two computers so they can connect.
- The network connection itself must be reliable.
Other Important Things:
- The sender and receiver need to agree on which port number to use beforehand.
- Know how big the file is before sending it same so the receiver has enough disk space.
Let me know if any part needs more explanation! I tried to simplify the key points as best I could.
Required settings and configurations :
1. Firstly, you need to check whether NetCat is installed or not in your system. To check that use the following Command :
nc -h
If you see the following output then netcat is preinstalled in your system.
-min.png)
2. If NetCat is not installed in your system then you need to use the following Command :
sudo apt-get install netcat
or
sudo apt-get -y install netcat
You need to enter the password after entering the above command as you need to give root user privileges. 'sudo' command gives superuser privileges to the command.'apt-get' command is used to handle the package utility.

Hooray !! You have Successfully installed the NetCat (NC) tool in your System.
How to send a file using netcat and then keep the connection alive :
Here is a step-by-step explanation on how to send a file using netcat and keep the connection alive in a simple understandable form:
Note: I performed this task on my local server, which is why I used the IP address 127.0.0.1. If you want to perform this task to share the files onto a different system, you must know the IP address of that system. The process will be the same - you only have to paste the IP address in place of 127.0.0.1. Avoid using Ports like 8888, 8080 etc. Because many servers & services use this as Default Port number.
On Reciever System:
Step 1: Before starting the port, create the file or folder that will store the received data coming from another computer. To do that, follow the below procedure and commands :
touch Recieved.txt
Now, you can see the file is created by using this command, or else, you can create the file manually too.
touch Recieved.txt
Step 2: On Computer , start netcat listening on a port with this command:
Well, you can start the listening on any Port like 4444 , 4000, 1234, 8080.
nc -lv -p 4444 > Recieved.txt
Netcat listening port
This opens port 4444 and saves any received data to received.txt, which means that any data that comes in will be going to Received.txt.
Step 3: Determine Server IP (On the Receiving Machine):
Find the Local IP Address:
ip addr show
or
ifconfig
Look for the local IP address associated with the network interface (e.g., enp0s3
for Ethernet or wlp2s0
for Wi-Fi).
On sender system:
Step 4: Now create the file on the sender system that we want to send to the server repeating the first step and add some text for the demonstration as in the file below:
Text Data
I added some data in the file.txt for the demonstration.
Step 5: Choose the Server IP and Port Number:
Use the IP address obtained from the step 3 (127.0.0.1) and the chosen port number (4444).
Step 6: Send the file to the server.
The file.txt is the file that we want to send to another computer. It will be directly saved in received.txt. Received.txt is the file that we used to open the port. Any data that the other computer sends will be stored in Received.txt.
nc -v 127.0.0.1 4444 < file.txt
Here, 127.0.0.1 is the IP address that we obtained from the receiver system.
Ip Address
If your file did not get sent or any error occurred, you just need to give the path of the file that you want to send like this:
nc -v 127.0.0.1 4444 < '/home/sk/Desktop/file.txt'
Simply , Just drag & drop the file like this
drag the file
This connects to port 4444 on Computer 1 and sends file.zip through the connection.
Step 7: Then, in the final step, you need to start the listening on Computer 1. Then, on Computer 2, you need to give the path of the file that you want to send and just hit enter.
The final output will look like this on both of the computers:
OutputAs you can see the file was successsfully received in our computer by the name of recieved.txt that we set in the first Command on the receiver's Computer & you can also see the data in the file.
recieved.txt
Step 8: When finished, press CTRL+C on either computer to close the connection.
Connection Closed
Conclusion
Netcat is a flexible networking device that may be used to effortlessly switch documents between computer structures on the identical time as additionally preserving the session chronic for in addition verbal exchange. By starting a listening netcat example on the receiving give up and connecting through a sender netcat way, a record may be fast sent over the community. At this factor, the open socket can be used to ship typed messages or distinct information via method of actually inputting it within the sending terminal. When completed communicating, urgent CTRL + C will terminate the session. In just a few clean steps, netcat lets in file transfers and persistent terminal-based definitely networking with each different device.
Similar Reads
How to Create a File in the Linux Using the Terminal?
In this article, we will learn to create a file in the Linux/Unix system using the terminal. In the Linux/Unix system, there are the following ways available to creating files. Using the touch commandUsing the cat commandUsing redirection operatorUsing the echo commandUsing the heredocUsing the dd c
4 min read
How to Download a File Using Node.js?
Downloading files from the internet is a common task in many Node.js applications, whether it's fetching images, videos, documents, or any other type of file. In this article, we'll explore various methods for downloading files using Node.js, ranging from built-in modules to external libraries.Using
3 min read
How To Send Files Using Python Built-In Http Server
Python's built-in HTTP server offers a straightforward way to share files over a local network or the internet without the need for complex setups. In this tutorial, we'll walk through the step-by-step process of using Python's built-in HTTP server to send files to clients. Setting Up the HTTP Serve
2 min read
Run a Command Conditionally with netcat and grep
In this article, we will explore the combination of Netcat (nc) and Grep commands in a Unix-like environment to execute commands conditionally based on specific search criteria. Netcat enables network communication, serving as a tool for sending and receiving data across networks. Meanwhile, Grep ex
4 min read
How to Use the telnet Command in Windows for Network Diagnostics?
The telnet command in Windows is a useful tool for performing basic network diagnostics. While it might not be the most commonly used command, it allows you to test network connectivity and troubleshoot various services. This guide will help you understand how to use telnet in Windows, including ena
6 min read
Calling Web Service Using Curl With Telnet Connection
Prerequisite: Telnet, Curl Terminal Network as the name suggests is an application layer protocol used to connect with hosts (remote machines) over a TCP/IP network. It is based on a client-server architecture where the telnet client makes a connection with the telnet server and once the connection
3 min read
How to Download and Upload Files in FTP Server using Python?
Prerequisite: FTP, ftplib Here, we will learn how to Download and Upload Files in FTP Server Using Python. Before we get started, first we will understand what is FTP. FTP(File Transfer Protocol) File Transfer Protocol(FTP) is an application layer protocol that moves files between local and remote f
3 min read
How to Connect to Telnet Server from Node.js ?
Connecting to a Telnet server from Node.js involves creating a client that can communicate using the Telnet protocol. Telnet is a protocol used to establish a connection to a remote host to execute commands and transfer data. In Node.js, you can use various packages to facilitate the connection to a
4 min read
How to use SSH to connect to a remote server in Linux | ssh Command
Secure Shell, commonly known as SSH, is like a super-secure way to talk to faraway computers, called servers. It's like a secret tunnel on the internet that keeps your conversations safe and private. Imagine you're sending a letter, and instead of sending it openly, you put it in a magic envelope th
8 min read
How to Fix - Wget: Failed: Connection Timed Out
In Linux, while downloading files using the wget command, encountering the "Wget: Failed: Connection Timed Out" error is common, often due to network instability, server unavailability, or firewall restrictions. To address this, adjusting timeout settings, implementing retry options, or utilizing mi
3 min read