SCP from server-A to server-B
Overview of SSH Keys for Secure File Transfer:
When using SSH keys for secure file transfer between two servers, the keys are used to
authenticate the connection without needing to enter a password. The key pair consists of:
● Private Key: Kept secure on the local machine (client/server).
● Public Key: Shared with remote machines to allow authentication.
To summarize the process:
1. Public Key is copied to the remote server (the server that will be receiving the files).
2. The Private Key remains on the sending server (the machine initiating the file transfer).
Now, let's break it down further with the file transfer flow between two servers:
Step-by-Step File Transfer Using SSH Keys
Scenario: You want to transfer files from Server A to Server B.
1. Generate SSH Keys on Server A (Sender)
If you haven't already generated a key pair on Server A, do so with:
ssh-keygen
○ This creates two files: a private key (id_rsa) and a public key (id_rsa.pub),
usually located in the ~/.ssh/ directory.
2. Copy the Public Key from Server A to Server B
○ You need to copy Server A's public key (id_rsa.pub) to Server B's
~/.ssh/authorized_keys file.
You can do this using the ssh-copy-id command:
ssh-copy-id user@serverB
○ Replace user@serverB with the username and IP address (or hostname) of
Server B.
This command:
■ Adds Server A's public key to the ~/.ssh/authorized_keys file on
Server B.
■ Allows Server A to authenticate using its private key, without needing a
password when connecting to Server B.
3. Transfer Files from Server A to Server B
○ Now that the public key is on Server B, you can securely transfer files from
Server A to Server B using scp (secure copy) or rsync commands without
needing to manually input a password.
Example using scp:
scp /path/to/local/file user@serverB:/path/to/remote/directory
4. This will transfer the file from Server A to Server B, utilizing the SSH keys for
authentication.
5. Server B Receives the Files
○ Once the file transfer command completes, Server B will receive the files in the
specified directory.
Clarifying Which Public Key Goes Where:
● Public Key: The public key from the sending server (Server A) must be copied to the
receiving server (Server B) in the ~/.ssh/authorized_keys file.
● The receiving server (Server B) uses this public key to authenticate the sending server
(Server A), allowing it to connect without a password.
Key Points:
● The public key should always be copied to the remote server (the one that will receive
the files).
● The private key remains on the local machine (the one that initiates the transfer).
Example Scenario:
● Server A (Sender) wants to transfer files to Server B (Receiver).
● You:
1. Generate the key pair on Server A.
2. Copy Server A's public key to Server B.
3. Use scp or rsync from Server A to transfer files to Server B.
This setup ensures that Server A can authenticate with Server B using the key pair (without
needing a password) and send files securely.
========================================================================
The scp command should be entered on server-A, which sends the file to server-B. The
syntax of the command indicates that the file is being transferred from the local machine (in
this case, server-A) to the remote server (server-B).
Explanation of the Command:
scp /path/to/local/file user@serverB:/path/to/remote/directory
● /path/to/local/file: The file or directory you want to copy (exists on server-A).
● user@serverB: Specifies the username and hostname or IP address of the remote
server (server-B).
● /path/to/remote/directory: The destination directory on the remote server
(server-B).
Steps:
1. Log into server-A.
2. Run the scp command to transfer the file.
Example:
If you are on server-A and want to send a file /home/ubuntu/test.txt to server-B at
/home/server-B/files/, the command would be:
scp /home/ubuntu/test.txt [email protected]:/home/server-B/files/
Reverse Transfer:
If you want to copy a file from server-B to server-A, the command should be entered on
server-A as well, but the source and destination will be swapped:
scp user@serverB:/path/to/remote/file /path/to/local/directory
For example:
scp [email protected]:/home/server-B/test.txt /home/ubuntu/