Search code examples
google-cloud-platformgoogle-compute-engine

Transfer files from one VM to another in GCP CE


I am trying to move a file from one VM instance to another using scp method. However I had understood that it is not possible to do the direct transfer from the source instance. e.g scp db.sql user@instance-src:/home/data/db.sql

I tried using the gcloud scp command but this allows me to do a transfer from local to remote only. This means I need to first download the file from the source to local.

Is there a better way to do this task?


Solution

  • First you need to create public key on the source-vm using the command ssh-keygen then you can see the public key using the command cat .ssh/id_rsa.pub. Now add the public key of source VM instance to the /home/user-name/.ssh/authorized_keys of destination vm instance. Now, you can transfer the files from the source-vm to the destination-vm as @Johnhanley said.

    Alternatively, using the below command you can transfer the file from one VM to another VM using below gcloud command:

    gcloud compute ssh source-vm --command='scp db.sql user@destination-vm:/home/file-transfer-location --zone=us-central1-a'