Rsync on GCP Compute Engine: When You Can’t Run Your Code Locally (Network Issues)

Billy Jacobson
Aug 13, 2020 · 3 min read

On my quest for learning some new technologies, I’ve stumbled into the issue of connecting to my server/instance/database from my local machine due to network issues. The way around this would be to run my code from a virtual machine on the same network, but getting my code onto the machine seemed like a frustrating process until I discovered Rsync. Rsync is a command-line application that allows you to easily transfer and synchronize files across computers or virtual machines.

Create the VM

I’ll be using Google Cloud Platform for this, but this technique should apply to any VM. I’ll create a VM and the main setting that matters is the network. You’ll want this to be the same network that your other resources are on. This will most likely just be default.

I hit create and wait for the instantiation.

Install rsync and setup SSH

SSH into your VM through the cloud console for these next steps.

Install rsync on your Ubuntu VM by running the following:

sudo apt-get install rsync

Add the public key you’re going to use to connect to the VM (I’m using ) to the VM’s file. You can create a new directory for your code if you’d like as well.

touch ~/.ssh/authorized_keys
vi ~/.ssh/authorized_keys # paste the key from your local machine in here
mkdir code_goes_here

Set up your local computer with rsync

Now, back to your local machine. Install rsync on this as well. I’m on a Mac so I use Homebrew, but use your preferred command line installer.

brew install rsync

Now, you can go into your code directory and run rsync to get your code from your local computer to your VM.

cd my_code
rsync -a $PWD/ [YOUR_IP_ADDRESS]:code_goes_here/

If you have an issue with permissions or public key being denied, try SSHing to the VM to make sure that goes okay. You can add the specific public key and username to the SSH command to see if that helps. If that does the trick, add those to your file so rsync will automatically use them when connecting.

Run your code

Finally, check back on your VM and your code should be there! Run the code and if you need to make changes, edit your local files, rsync and head back to your VM.

cd code_goes_here
ls # Check that your code is there.
mvn compile exec:java

Google Cloud - Community

Google Cloud community articles and blogs

Google Cloud - Community

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Billy Jacobson

Written by

Unapologetically Myself | @googlecloud DevRel | New Yorker (he/him)

Google Cloud - Community

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store