Transfer documents and files to another computer using the terminal

There are different programs in Linux to transfer documents, files, images and videos to another Computer. Here I will talk about rsync that uses the ssh protocol in the transfer. The encryption used by SSH intended to provide confidentiality and integrity of data over an unsecured network, such as the Internet.




Here I will talk about how to use 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.


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.


Install ssh and rsync


ssh

We will install ssh with OpenSSH, which comes along with other applications, such as sftp, 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, 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 as openssh here says, the SCP protocol is outdated, inflexible and not easily repaired, for this reason, I will not talk about it here.

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.


If you want to use other methods to transfer documents and files, here I tell you how to does it.







You comment is welcome: