Connect to your Google Cloud Compute Engine
Configuring SSH and file transfers to a Google Cloud Compute Engine via the GCloud's CLI.
So you've taken a leap and decided to host your VPS on Google Cloud: let me be the first to congratulate you on joining the clearly superior cloud platform of our modern era. I would apologize for being so openly opinionated, but so far I've only stated objective facts.
Now that you've joined the club, you may have found yourself asking the inevitable: "how do I connect to my damn instance?" If you're like me, you're probably not the kind of person who enjoys this as their main solution:
Luckily for us, there are a few ways to interact with your Compute Engine. Let's take a look at all of them.
Set up the gcloud CLI
In order to SSH natively, we need to install the gcloud CLI on our machine. Mac users can download this here, and Windows users can download from here. Clicking the downloaded file will extract the package. With your package extracted, run the install script
install.bat for Windows) to start the installation:
Continuing the script will list the 'components' gcloud can install on your local machine, where each component is a Google Cloud product:
Your current Cloud SDK version is: 214.0.0 The latest available version is: 214.0.0 ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ Components │ ├───────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬───────────┤ │ Status │ Name │ ID │ Size │ ├───────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼───────────┤ │ Not Installed │ App Engine Go Extensions │ app-engine-go │ 152.8 MiB │ │ Not Installed │ Cloud Bigtable Command Line Tool │ cbt │ 6.3 MiB │ │ Not Installed │ Cloud Bigtable Emulator │ bigtable │ 4.3 MiB │ │ Not Installed │ Cloud Datalab Command Line Tool │ datalab │ < 1 MiB │ │ Not Installed │ Cloud Datastore Emulator │ cloud-datastore-emulator │ 17.7 MiB │ │ Not Installed │ Cloud Datastore Emulator (Legacy) │ gcd-emulator │ 38.1 MiB │ │ Not Installed │ Cloud Pub/Sub Emulator │ pubsub-emulator │ 33.4 MiB │ │ Not Installed │ Cloud SQL Proxy │ cloud_sql_proxy │ 2.5 MiB │ │ Not Installed │ Emulator Reverse Proxy │ emulator-reverse-proxy │ 14.5 MiB │ │ Not Installed │ Google Cloud Build Local Builder │ cloud-build-local │ 4.4 MiB │ │ Not Installed │ Google Container Local Builder │ container-builder-local │ 4.4 MiB │ │ Not Installed │ Google Container Registry's Docker credential helper │ docker-credential-gcr │ 1.8 MiB │ │ Not Installed │ gcloud Alpha Commands │ alpha │ < 1 MiB │ │ Not Installed │ gcloud Beta Commands │ beta │ < 1 MiB │ │ Not Installed │ gcloud app Java Extensions │ app-engine-java │ 118.6 MiB │ │ Not Installed │ gcloud app PHP Extensions │ app-engine-php │ 21.9 MiB │ │ Not Installed │ gcloud app Python Extensions │ app-engine-python │ 6.2 MiB │ │ Not Installed │ gcloud app Python Extensions (Extra Libraries) │ app-engine-python-extras │ 28.5 MiB │ │ Not Installed │ kubectl │ kubectl │ < 1 MiB │ │ Installed │ BigQuery Command Line Tool │ bq │ < 1 MiB │ │ Installed │ Cloud SDK Core Libraries │ core │ 8.3 MiB │ │ Installed │ Cloud Storage Command Line Tool │ gsutil │ 3.6 MiB │ └───────────────┴──────────────────────────────────────────────────────┴──────────────────────────┴───────────┘ To install or remove components at your current SDK version [214.0.0], run: $ gcloud components install COMPONENT_ID $ gcloud components remove COMPONENT_ID To update your SDK installation to the latest version [214.0.0], run: $ gcloud components update Modify profile to update your $PATH and enable shell command completion? Do you want to continue (Y/n)?
Once installed, run
gcloud init in your terminal. This will prompt you to login:
Pressing 'Y' will prompt a simple browser window from which you can authenticate with Google by simply selecting your Google account, as though we were using any other app with Google OAuth authentication. That's correct: you don't even need to go through the trouble of typing a password, assuming you've logged in to your Google account before (I'm guessing you have).
Next, the terminal will prompt to specify which of your projects to use. Select the project which contains your instance by entering the number seen in the resulting list:
Now you're in the clear to go nuts with the gcloud CLI:
SSH via a Native Terminal
Ugh, so now we need to go through the process of creating public and private keys etc to SSH into our instance, right? Wrong: gcloud is so mo effin' dope that there's a one-line command which will actually do this for you:
But there's no way it's that easy, right?
It really is that easy. For as long as you use your local machine, you will only ever need to used the command
gcloud compute ssh instancename to connect to your instance.
Get and Put Files
You might be thinking that the next order of business would be to config SFTP in order to manage files on your instance. Believe it or not, there's a simpler way.
Downloading Files from your Instance
gcloud comes with a built-in tool to download and upload files to your instance via the CLI. To download files from your server, check out this one-liner:
Uploading Files to your Instance
The same command can be reversed to upload as well:
When we begin to look into other methods of interacting with our Computer Engine instance, the general youth of GCP becomes apparent.
At the time of writing, Google's own documentation lacks information on how to connect via SFTP, as the only mention of SFTP is this broken anchor link. Google also provides a RDP Chrome add-on specifically for connecting to Compute instances, but my own attempts have shown this to be broken as well.
Despite these setbacks, the combination of SSH and getting/putting files should be more than enough to satisfy anybody's needs for now. Google Cloud Platform is only getting better with time, and is doing so at a pace which scare other providers.