Open In App

How to Send a File Using Netcat and Then Keep the Connection Alive

Last Updated : 08 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

6-(1)-min

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.

5

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.

nc-(2)-(1)

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




re

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:

Screenshot-2023-12-01-121532-(1)

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.

2-(1)-min

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

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:

output

Output

As 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.

recieve

recieved.txt

Step 8: When finished, press CTRL+C on either computer to close the connection.

c

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads