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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Linux Commands Cheat Sheet Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. Example of an AVL Tree:The balance factors for different nodes are : 12 :1, 8:1, 18:1, 5:1, 11:0, 17:0 and 4:0. Since all differences
4 min read