Google Compute Engine scp Files on a Linux or Unix or Mac OS X

Posted on in Categories , , , last updated September 13, 2015

Scp command copies files between hosts on a network using ssh for data transfer. How do I use scp toefacilitate the remote file transfer using Google Compute Engine virtual machines on a Linux, OS X or Unix-like system?

You need to use gcloud compute command to copy files between a Google virtual machine instance and your local machine powered by OSX/Linux or Unix-like system.

Syntax

The basic syntax is as follows to copy file from your local system to remote Google VM:

gcloud compute copy-files local-file-name {instance-name-here}:/path/ --zone {zone-name-here}

The basic syntax is as follows to copy file from remote Google VM to your local system

gcloud compute copy-files {instance-name-here}:/remote/path /local/dir/ --zone {zone-name-here}
gcloud compute copy-files {instance-name-here}:/~filename /local/dir/ --zone {zone-name-here}

Examples

To list your Google compute VM, enter:
$ gcloud compute instances list
Sample outputs:

Fig.01: Note down the Google instance NAME and ZONE
Fig.01: Note down the Google instance NAME and ZONE

To copy local /etc/hosts file to remote vm called instance-1 hosted in us-central1-b zone, enter:
$ gcloud compute copy-files /etc/hosts [email protected]:~/ --zone us-central1-b
To copy local ~/webapp/ directory to remote vm called instance-1 hosted in us-central1-b zone, enter:
$ gcloud compute copy-files ~/webapp/ [email protected]:/var/www/nginx/ --zone us-central1-b
To copy remote /foo/ directory from remote vm called instance-1 hosted in us-central1-b zone, enter:
$ gcloud compute copy-files [email protected]:/foo/ ~/backups/ --zone us-central1-b
To copy remote file ~/db.conf.py from remote vm called instance-1 hosted in us-central1-b zone, enter:
$ gcloud compute copy-files [email protected]:~/db.conf.py $HOME/data/ --zone us-central1-b

Options

You can pass the following options:

--dry-run : If provided, prints the command that would be run to standard out instead of executing it.
 
--plain : Suppresses the automatic addition of ssh(1)/scp(1) flags. This flag is useful if you want to take care of authentication yourself or re-enable strict host checking.
 
--ssh-key-file SSH_KEY_FILE : The path to the SSH key file. By default, this is ~/.ssh/google_compute_engine.
 
--zone ZONE : The zone of the instance to copy files to/from. If omitted, you will be prompted to select a zone.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.