Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


RHCSA - Operate Running Systems: Securely Transfer Files Between Systems

It is important to understand how to transfer data securely and efficiently between different systems. In this lesson, we will explore three commonly used commands for copying files between systems: rsync, scp, and sftp.

For this lesson make sure you have a second server available to practice transferring files. If you do not have a second server that is ok, you can follow along by pretending to transfer to a remote server by specifying your current servers IP address as the destination location.

rsync

rsync is used for synchronizing and transferring files between local and remote systems. It utilizes a delta transfer algorithm to efficiently update only the changed parts of files, reducing network bandwidth usage and speeding up file transfers. rsync is particularly useful for tasks like remote backups, mirroring directories, and syncing files between systems.

rsync Syntax

To utilize rsync, you can use the following command syntax:

rsync [options] source destination
  • options: Additional flags and parameters to customize the rsync operation.
  • source: The source file(s) or directory to be synced.
  • destination: The target location where the files will be synchronized.

The below table shows some common options for the rsync command:

Option Description
-v, --verbose Increases verbosity, providing more detailed output.
-r, --recursive Recursively copies directories and their contents.
-a, --archive Preserves permissions, ownership, timestamps, and symbolic links.
-z, --compress Compresses data during transfer, reducing network usage.
-n, --dry-run Performs a trial run without actually copying anything.
--progress Shows the progress of the transfer.

Practice Using rsync

Follow along with the exercises to become familiar with rsync.

Transfer a file to a remote destination:

On server 1, run the following, entering password if prompted:

rsync -avz /usr/share/man/man1/rsync.1.gz user@server2:/tmp

This will transfer the file to the /tmp directory on the target server, which can be confirmed by running the following on server 2:

ls -l /tmp/rsync.1.gz

Transfer recursively a directory to a remote destination:

The -a flag automatically includes the -r option so it does not need to be explicitly set.

On server 1, run the following, entering password if prompted:

rsync -avz /usr/share/man user@server2:/tmp/

This will transfer the man directory to the target server, which can be confirmed by running the following on server 2:

ls -lR /tmp/man

scp

Secure Copy Protocol (SCP) provides a secure and efficient method for transferring files between local and remote systems. SCP is based on the Secure Shell (SSH) protocol and offers a straightforward command-line interface for secure file transfers. It allows you to securely copy files and directories over SSH connections.

scp Syntax

To utilize SCP, you can use the following command syntax:

scp [options] source destination
  • options: Additional flags and parameters to customize the SCP operation.
  • source: The source file(s) or directory to be copied.
  • destination: The target location where the files will be copied.

The below table shows some common options for the scp command:

Option Description
-P <port> Specify the remote port number. (Default: 22)
-r Recursively copy entire directories.
-p Preserve modification times, access times, and permissions.
-v Verbose mode, shows the progress of the transfer.
-C Enable compression during the transfer.

Practice Using scp

Follow along with the exercises to become familiar with scp.

Transfer a file to a remote destination:

On server 1, run the following, entering password if prompted:

scp /usr/bin/scp user@server2:/tmp/

This will transfer the file to the /tmp directory on the target server, which can be confirmed by running the following on server 2:

ls -l /tmp/scp

Transfer recursively a directory to a remote destination:

On server 1, run the following, entering password if prompted:

scp -r /usr/share/doc/ user@server2:/tmp/

This will transfer the man directory to the target server, which can be confirmed by running the following on server 2:

ls -lR /tmp/doc

sftp

The Secure File Transfer Protocol (SFTP) provides a secure and reliable method for transferring files between local and remote systems. Built on top of the Secure Shell (SSH) protocol, SFTP allows you to securely transfer files, manage directories, and perform various file operations over an encrypted connection. It offers a command-line interface similar to traditional FTP, making it a popular choice for secure file transfers.

sftp Syntax

To connect to a remote server using SFTP, you can use the following command:

sftp user@remote_server

Once connected to the remote server, you can navigate directories using commands similar to those in a typical shell. Here are some commonly used commands:

  • ls: List files and directories in the current directory.
  • cd: Change the current directory.
  • pwd: Display the current working directory.
  • mkdir: Create a new directory.
  • rmdir: Remove a directory (must be empty).
  • rm: Remove a file.
  • get: Download a file from the remote server to the local system.
  • put: Upload a file from the local system to the remote server.

For the above commands, you can typically put an l in front of them to perform an action locally, even when connected to the remote system, for example lls or lcd to list and change directories.

Practice Using sftp

Follow along with the exercises to become familiar with sftp.

Connect to remote server and practice various commands:

Connect to server 2

sftp user@server2

Ensure you are in the remote users home directory:

pwd

List the contents of the remote directory:

ls -la

Change local directory to /tmp:

lcd /tmp

Confirm you are in the local /tmp directory:

lpwd

Retrieve the .bash_history file:

get .bash_history

Perform a local ls to confirm the file downloaded:

lls -l .bash_history

Change to /tmp directory on remote server:

cd /tmp

Upload the whole /usr/share/doc/sudo directory:

put -R /usr/share/doc/sudo

Confirm directory uploaded:

pwd
ls -l sudo

Exit from SFTP connection:

exit

Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.