In this topic, we are going to demonstrate how you can copy files from one Linux system to another in a simple yet secure manner. In the face of growing cybersecurity-related threats, security is a top priority. We will demonstrate how you can securely transfer files between Linux systems with utmost privacy and confidentiality.
1.Copy files using the SCP command
SCP is an acronym for Secure Copy. It uses SSH protocol to encrypt the file(s) being copied or transferred. Since SSH is a secure protocol, privacy and confidentiality of files is guaranteed. Let’s dive in and see a few SCP example usages.
The fundamental syntax of SCP command is as shown:
# scp /path/to/file user@remote-server-IP:/destination/path
For example, we will copy a file called linux_basics.doc located at the current directory to the /tmp directory of a remote Linux machine of IP 220.127.116.11
# scp linux_basics.doc [email protected]:/tmp
If you are connecting to the remote server for the first time, you will be required to accept the ECDSA fingerprint by typing Yes and hitting ENTER.
Next, you will be prompted for the remote host’s password. Type in the password and hit ENTER. Your file will be saved on the remote hosts /tmp directory as specified in the command.
To confirm the existence of the file, simply log in and run the ls -l command in the destination folder.
You can also copy from a remote host to the local Linux system using the syntax
# scp user@remote-Server-IP:/remote/file/path /local/file/path
For example, to copy a file called linux_permissions.doc located at the /root directory of the remote host IP 18.104.22.168, to the /tmp/docs directory of the local machine, run
# scp [email protected]:/root/linux_permissions.doc /tmp/docs
Like before, you will be prompted for the remote system’s password. Type the password and hit ENTER and the file will be copied from the remote system to the specified directory on the local system.
Other options commonly used with SCP command include:
-r: This copies folders or directories recursively
-v: Short for verbose, this option prints the output of the copying process on the terminal
-l: This option limits the bandwidth usually specified in Kbits/s
-P: Specified the port to connect to. Note that this is in UPPERCASE
2.The Rsync command
Rysnc, short for remote synchronization, is a UNIX utility that synchronizes files in directories by copying only the differences between the source directory and the destination directory. Just like SCP, it uses the SSH protocol to connect to the remote host. Next, it will invoke the remote Linux system’s rsync which determines which files need to be copied to the remote host.
Let have a look at a few rsync examples.
We will create a directory called docs and create 2 files: file1.pdf file2.pdf & file3.pdf.
# mkdir docs && cd docs
# touch file1.pdf file2.pdf file3.pdf
If the remote host is not defined, then rsync behaves as the copy command but will still sync the differences.
# rsync -avh /source/directory/path /destination/directory/path
For instance to copy the contents of docs folder to /opt/docs directory issue the command:
# rsync -avh /root/docs/ /opt/docs
As you can see, for the first time, rsync copies all the contents of a directory to another destination directory.
If we add another 2 more files in the /root/docs directory, say file4.pdf & file5.pdf, and later run the rsync command again, only the incremental or additional files (file4.pdf and file5.pdf) will be added to the destination folder.
If you are copying files to a remote host use the syntax:
# rsync -avh /source/directory user@remore-Server-IP: /destination/directory
# rsync -avh /opt/docs/ [email protected]:/opt/docs
As usual, you will be requested for the remote host’s password. Upon typing in and pressing ENTER, the files will be synchronized.
If you get the error below, then rsync is not installed on the host system
bash: rsync: command not found
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(235) [sender=3.1.2]
To install rsync on CentOS & RHEL systems, run
# yum install rsync
For Ubuntu/Debian run
# apt install rsync
Those are some of the few rsync examples. And that’s all we had in store for you. We have discussed how you can securely copy files from one Linux system to another. We have looked at both SCP example usages as well as rsync examples. We hope you found this insightful.