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?

[donotprint]
Tutorial details
Difficulty level Easy
Root privileges No
Requirements gcloud sdk
Est. reading time 1m
[/donotprint]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

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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum