How do I scp between two remote hosts?

To scp between two remote hosts, you typically need to be logged into one of the hosts and execute the scp command there.

The general format is like this:

scp <user>@<source_host>:<source_file_path> <user>@<destination_host>:<destination_file_path>

Suppose, you are logged into host1, and you want to copy a file from host2 to host3.
First, make sure that the key-based ssh authentication is set up for host2 -> host1 and host1 -> host3. Then, on host1, you can execute:

scp user@host2:/path/to/source/file.txt user@host3:/path/to/destination/

This will copy file.txt from host2 to host3.

Keep in mind this command requires you to have proper SSH access and permissions for both source and destination hosts. If you do not have the necessary authentication set up, the command will ask for the password for each machine.

Set up key-based SSH authentication

To set up key-based SSH authentication, you’ll need to generate a key pair on host1, then copy the public key to host2 and host3. Here’s how you can do it:

  1. Step One — Create the RSA Key Pair on host1:

Open a terminal and run the following command:

ssh-keygen -t rsa

You will be asked to specify the file location and passphrase (optional). If you just press Enter through those prompts, it will create an RSA key pair with default settings.

  1. Step Two — Store the Keys and Passphrase:

When you are prompted to “Enter a file in which to save the key,” you can press Enter. This accepts the default file location.

At the prompt, type a secure passphrase or press enter to proceed without a passphrase.
After completing these steps, your new keys are available in your user home folder ~/.ssh/id_rsa for your private key and ~/.ssh/id_rsa.pub for your public key.

  1. Step Three — Copy the Public Key to host2 and host3:

Next, you’ll copy your public key to your host2 and host3 using the ssh-copy-id command. Like this:

ssh-copy-id user@host2
ssh-copy-id user@host3

Replace user with your username, and host2 or host3 with the IP address or hostname of your second and third machines. You will be prompted for the user password for host2 and host3 to copy the public key.

That’s it! You have set up the key-based ssh authentication. Now you can log into host2 and host3 from host1 without a password:

ssh user@host2

or

ssh user@host3

This method applies to any Linux or Unix system that uses SSH. Please refer to the documentation for Windows servers or any other non-Unix systems. Also note that the user must have ssh and shell access.

Warning: Be careful with your private key (~/.ssh/id_rsa). Don’t share your private key with anyone! In production environments, it’s a common practice to protect private keys with a strong passphrase.

Note: The scp command is not installed by default on some systems. You can install it using your system package manager (like apt, yum, etc.). Alternatively, you can use rsync or sftp depending on the systems and permissions involved.

Important: Remember about data security. Always ensure safe and secure data transfer, especially when dealing with sensitive data. Use encrypted channels for such transfers (which scp does by utilizing SSH). Make sure the user whose credentials are used for the transfer has only the necessary permissions and nothing more.

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.