≡ Menu

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

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 vivek@instance-1:~/ --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/ nginx@instance-1:/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 USER@instance-1:/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 USER@instance-1:~/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.
Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:



{ 0 comments… add one }

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">


   Tagged with: