How to transfer files and documents from a computer to another

There are different programs in Linux to transfer documents, files, images and videos to another Computer. Here I will talk about cp, netcat and rsync. The most crucial difference between cp and netcat with rsync is that the latter uses the SSH protocol in the transfer.




Here I will talk about how to use cp, netcat and rsync in Debian and its derivatives such as Ubuntu and Linux Mint. In other Linux distributions (Red Hat, Fedora, Mandriva, Suse, Gentoo, .., they use different commands to do the same.

About secure data transfer in scp, openssh says;

The scp protocol is outdated, inflexible and not readily fixed. We recommend the use of more modern protocols like sftp and rsync for file transfer instead.

For this reason, I will not talk about him here.


An important thing that you should not forget when you use one of these three programs when you want to copy something, and have a name with empty spaces my document.txt, you have to use a string, two double quotes " ", so my document.txt would be "my document.txt", if you do not use the string in these cases, it will give you an error.

cp

The cp(copy) program allows you to copy files, directories and photographs. It is advisable for security to use cp only to copy files and documents locally, that is, within your system. It is installed by default in your Linux distribution.


Example:

We want to copy the document my_document.txt from A to B, and both are on the same computer.

my_document.txt is in the Home Folder Directory

It is the Documents Directory

Syntax:

cp [origin] [destination]

The command

cp ~/home folder name/my_document.txt ~/Documents



Example:

We want to copy the private folder that is in the Documents directory that is in A, and we want to take it to B, we also want it to keep all file rights, users, groups, ..., both are on the same computer.

The private folder is on the Documents directory

It is the Directory Pictures

Syntax:

cp [-a] [origin] [destination]

By default, cp creates a new file that has the same content as the copied file and the same permissions but restricted by the umask, the copy is the date from the moment the copy was made and belonged to the user making the copy.

With -a we will get the Recursion of our private folder and the same modification time, the same access time and the same permissions as the original.

The command

cp -a ~/Documents/private ~/Pictures


If you write in your Terminal man cp, you will receive more options with which you can copy.



netcat

Netcat is a network tool that has many utilities, that's why it is called «Swiss Army knife». It can use in the terminal or scripts for communication through TCP and UDP network connections (Internet Protocol version 4 and version 6). Still, it also applies in local Unix domain sockets. Therefore it is suitable for numerous network connection applications. It is installed on Ubuntu and Linux Mint, but not on Debian, now we install it,

Update your system

sudo apt-get update

Install netcat

sudo apt-get install netcat-openbsd


Example:

We want to use netcat as Chat, computer A is our server, and computer B is the client, we will use to communicate Two terminals, one will be the server and the other the client.

The server-side always starts first...

The command:

nc -vlp 1234

nc netcat

v Show connection information.

l Listen for an incoming connection rather than initiating a connection to a remote host.

p Specify the source port nc should use.

1234 They are the ports that we open to testing, to open the ports you have to know two important things. 1 - The ports used by the TCP and UDP and that you cannot use because they use by other protocols, a list of other protocols. 2 - The Ports of our computer,which in Debian-based distributions, such as Ubuntu and Linux Mint, are closed by default. To have them, we need to install a Firewall that will allow us to open the Ports we want, we will install ufw that uses the command line and is easy to use.

Update your system

sudo apt-get update

Install ufw

sudo apt-get install uf.

Open the Ports 1234 on computer A.

sudo ufw allow 1234/tcp

When we finish our Chat, for security, close this Port.

sudo ufw deny 1234/tcp


Below I described these steps in more detail.


For computer B to know that, I write in the Terminal,

ip a

we will get something similar to this,

password para root

enp0s3 It is the name of my network interface. It is the network interface in Ethernet, by "interface", it is understood as something that stands between the computer and the network, in Ubuntu and Linux Mint is the name given by default to the network interface, in Debian it is enp31s0.

192.168.178.23 Is the IP of my computer A


The client part starts later..

The command:

nc 192.168.178.23 1234

nc netcat

192.168.178.23 It is the IP of computer A

1234 They are the ports that open by default to test.


Let's start with the Chat

We write in the Terminal of computer A, which is our server,

nc -vlp 1234

We write what we want. I write Hello, how are you? and we give the key ↵ Enter, which is how we send our message.

password para root

We write in the Terminal of computer B that is our client,

nc 192.168.178.23 1234

We write what we want, I respond well and you? And we give the ↵ Enter key, which is how we send our message.

password para root

To end our Chat, we give the keys Crtl + C.


Example:

We have the movie Matrix.mp4, and we want to make a copy, it is in Videos Directory of computer B, and we want to take it to computer A. We will use the programs netcat and tar.

The tar program is installed by default in your Linux distribution, but if it is not, install it with this command,,

Update your system

sudo apt-get update

Install tar

sudo apt-get install tar


It is where we want to transfer our movie, and it is the computer which we use as a server, therefore is why it must be the first to start.

The command:

nc -l -p 1234 | tar xvfp -

nc netcat

-l Listen for an incoming connection rather than initiating a connection to a remote host.

-p 1234, -p Specify the source port which should use computer B, 1234 the ports which we used.

| pipeline is an implementation technique where multiple instructions are overlapped in execution. The computer pipeline is divided in stages.

tar xvfp The program tar, xvfp Extract contents for the archive.tar into the current directory, computer A.

- By convention, an independent script instead of a filename would mean stdin o stdout, here would be the output, stdout.


The Matrix.mp4 movie, which is in the Videos directory.

Syntax:

~/Videos$ [tar] [cfp] [-] [|] [nc] [-w 3] [ip] [puerto]

cd Vídeos With the cd Command, we move to the Videos directory, where the Matrix.mp4 movie is.

tar The program tar.

cfp Create an archive file.

- By convention, an independent script instead of a filename would mean stdin o stdout, here would be the input, stdin.

| pipeline is an implementation technique where multiple instructions are overlapped in execution. The computer pipeline is divided in.

nc The program netcat

-w 3 timeout Connections which cannot be established or are idle timeout after timeout seconds. The -w flag has no effect on the -l option, i.e. nc will list, nc will close automatically after Three seconds.

192.168.178.23 It is the IP of the computer A, where I want to transfer the movie, to know the IP where you want to move your file, you have to write in the Terminal of the computer where you want to transfer it. Here I explain how.

1234 -p Specify the source port which should use computer A, 1234 the ports which we used.

The command:

tar cfp - Matrix.mp4 | nc -w 3 192.168.178.23 1234


We start with the transfer

Remember to open the Port on computer A...

sudo ufw allow 1234/tcp

Since computer A is our server, we start, First, I write in the Terminal,

nc -l -p 1234 | tar -xvfp - and we press the ↵ Enter key, which is how we open our server.

password para root

In computer B, which is where our movie I want to transfer is, I write in the Terminal,

tar cfp - Matrix.mp4 | nc -w 3 192.168.178.23 1234 and we press the ↵ Enter key, which is how transfer our movie.

password para root

With this we will have transferred the movie.

If you write in your Terminal man nc and man tar, you will receive more options with which you can copy.

ssh

In Ubuntu and Linux Mint comes with OpenSSH-client installed, but not OpenSSH-server, you will have to install it on the computer where you want to take the copy, we will install ssh with OpenSSH, which comes along with other applications, such as sftp. In Debian, you have to install both.

Install openssh-server on Ubuntu, Linux Mint y Debian.

Update your system

sudo apt-get update

Install openssh-server

sudo apt-get install openssh-server

Install openssh-client on Debian, in Ubuntu and Linux Mint it comes installed by default.

sudo apt-get install openssh-client


ssh uses Port 22 by default, to communicate outside your computer. For regular transfers between computers, you can leave it that way, or if you want to change it, you have to change the Port, this does by writing a lowercase p -p, for example, we want to transfer a document through Port 45, we write,

ssh -p 45 [destination_username]@[ip_destination]

As I said, changing the port number is usually a personal option, not mandatory ..., but there are some cases, wherein you need to change, for example when you have a Web server, where your Web page is stored. The Web hosting company usually gives you the option of communicating with ssh, for security reasons it gives you a port number that is not 22, which is the default port for ssh.

When you have finished transferring what you want, it is advisable for security, disconnect OpenSSH-server.

We disconnect it, with this command,

sudo service ssh stop

With this command, we stoping it in Debian, but in Linux Mint and Ubuntu, when we restart the computer, it will be activated again, so we have to disable it, we disable it with this command,

sudo systemctl disable sshd.service

When you want to activate it again, type this command,

sudo systemctl enable sshd.service

We also have to write,

sudo service ssh start

If you receive any notice after doing this, restart ssh.

sudo systemctl restart ssh


If we want to receive documents, files, videos, ... via ssh from the other computer and we want to use another port instead of the Port that comes by default in ssh, the 22, we will have to open the Ports of this computer, which in the distributions based in Debian, like Ubuntu and Linux Mint, they are close by default. To do this, we need to install a Firewall that will allow us to open the Ports we want. We will install ufw that uses the command line and is easy to use.

Update your system

sudo apt-get update

Install ufw

sudo apt-get install ufw


Example:

We want to transfer the Matrix movie from computer A to computer B, we will use the rsync program that usually uses ssh for transfers, and we will use Port 45.

We have to open Port 45 on both computers, on computer B we open it this way,

sudo ufw allow 45/tcp

We also have to start ssh, so that computer B, communicates via ssh.

sudo service ssh start

In computer A, where the movie is, we open this Port directly with rsync.

rsync -p 45 -z ~/Matrix.mp4 linux@192.168.178.25:/home/linux

An important thing that I recommend for security, close the ports you have opened when you have finished transferring whatever you want.

The ports are closed in this way,

sudo ufw deny 45/tcp

When you restart the computer you will have this port closed.

Don't also forget to disable ssh, a little higher I explain how to do it.

rsync

Rsync and scp do similar things, but for reasons I mentioned above, I will not talk about it here. A very important difference between rsync and netcat is that rsync uses the ssh protocol in its transfer through a secure channel in which all the information is encrypted, in addition it gives you many more possibilities for file transfers, documents, ... between your computer and a server.

It is not advisable to use sudo when using rsync if the file or document you want to copy does not have access privileges or rights, because your destination system can overwrite it.


Rsync is installed on Linux Mint and Ubuntu, but not on Debian, and we install it with this command,

sudo apt-get install rsync


Example:

We want to copy the movie Matrix.mp4, which is in the Videos directory of computer A and we want to take a copy to computer B.

Syntax:

rsync [option] [archive] [destination_username]@[ip_destination]:[/path_to_destination]

rsync It's how the command begins.

-z With this option, rsync compresses the file data as it is sent to the destination machine, which reduces the amount of data being transmitted -- something that is useful over a slow connec‐tion.

archive The file we want to send, we can also write the path where our archive is, such as -z ~/Videos/Matrix.mp4.

destination_username Here we write the username of the computer B.

ip_destination We write the IP of the computer B, here explain how to know the ip.

: It is the separator between the IP and the path of the directories where we want to take the copy, that is,

/path_to_destination.

The command:

rsync -z /home/linux/Vídeos/Matrix.mp4 me@192.168.178.40:/home/me


We write in the Terminal before transferring our copy from computer A

sudo service ssh start


We start with the transfer

Since computer B is our server, we start first, I write in the Terminal,

sudo service ssh start and we press the ↵ Enter key.

password para root

On computer A, which is where our movie is, we transfer it by writing to the Terminal,

rsync -z /home/linux/Videos/Matrix.mp4 me@192.168.178.40:/home/me and we press the ↵ Enter key, which is how we transfer our movie.

password para root

With this we will have transferred the movie.


Example:

We want to transfer the Private folder that is in the Documents directory from computer B to computer computer A. Within the Private folder we have some documents with different permissions, in our transfer we want rsync to save these permissions.

Syntax:

rsync [--progress] [-avz] [path_to_movie] [destination_username]@[ip_destination]:[/path_to_destination]

--progress It gives us the opportunity to see the progress of our transfer.

-avz This would recursively The files are transferred in "archive" mode, which ensures that symbolic links, devices, attributes, permissions, ownerships, etc. are preserved in the transfer. Additionally, compression will be used to reduce the size of data portions of the transfer.

The command;

rsync --progress -avz ~/Documents/Private linux@192.168.178.25:/home/linux


We write in the Terminal before transferring our copy from computer B

sudo service ssh start


We start with the transfer

Since computer A is the movie destination

sudo service ssh start and we press the ↵ Enter key.

password para root

On computer B, which is where our Private folder is, we transfer it, I write in the Terminal,

rsync --progress -avz ~/Documents/Private linux@192.168.178.25:/home/linux and we press the ↵ Enter key, which is how we transfer our movie.

password para root

With this we will have transferred the Private folder from computer B to computer A.

When you have finished transferring what you want, it is advisable for security, disconnect openssh-server, here I tell you how to do it.

If you want, you can use rsync with a graphical interface, you install it with this command,

sudo apt-get install grsync

You have to restart your computer when you have done it, in your Linux distribution, look in your directory Applications + System Tools, by Grsync.

And such a thing will come out.

password para root


If you write to your terminal, man rsync, you will receive more options to copy with rsync.









You comment is welcome: