Use VSCode and Remote SSH extension to connect to Compute Engine on Google Cloud
Get more productive with file management on a remote server using VSCode and Remote SSH extension
Table of content
- Introduction
- Install Google Cloud SDK
- Generate SSH keys
- Create config file
- Install Remote SSH extension and Add new SSH host
- Connect to host
- Links
Introduction
SFTP connection to Compute Engine on Google Cloud Platform is convenient for managing files. For sure, you could log in directly to the remote server and do most of the files and folders management via the console with shell commands like cd; ls; pwd;
But it is way more productive to view the folder structure like in any file explorer.
There are multiple SFTP clients available for this purpose. Here are two, which I find best depending on the platform you work:
- Filezilla for all platforms (free)
- Transmit 5 for macOS (paid)
However, the most satisfying SSH experience is the usage of the Remote-SSH extension for the VSCode. It gives access to the remote files directly in the developer environment and feels that you work locally.
Here is an official step-by-step tutorial to get started with VSCode and Remote-SSH. Below I summarize the steps required to connect to Compute Engine on the Google Cloud platform:
1. Install Google Cloud SDK
Once you work with Google Cloud Platform, it is wise to install gcloud SDK to execute numerous remote commands from your local console. To do this, download the SDK for your platform to any folder and follow the installation instructions mentioned on the download page. Also, don’t forget to perform the shell commands as discussed there:
./google-cloud-sdk/install.sh #To install the gcloud SDK
./google-cloud-sdk/bin/gcloud init # To initialize the gcloud SDK
2. Generate SSH keys
Generate SSH key using the gcloud commands following the complete instruction. The gcloud compute ssh
command is used to generate SSH keys for the first connection. Afterwards, this command connects to the server.
By default, gcloud
expects keys to be located at the following paths:
$HOME/.ssh/google_compute_engine
– private key$HOME/.ssh/google_compute_engine.pub
– public key
3. Create config file
The content of the configuration file $HOME/.ssh/config
might be as in the example below, where XX.XX.XX.XXX
is the External IP Address of your Compute Engine instance.
Host XX.XX.XX.XXXHostName XX.XX.XX.XXX
UseKeychain yes
AddKeysToAgent yes
IdentityFile ~/.ssh/google_compute_engine
User your_username
4. Install Remote SSH extension and Add new SSH host
Find Remote SSH extension in the VSCode extensions tab and install it.
Open VSCode and press ⌘ + Shift + P
for macOS or Ctrl + Shift + P
for Windows to open VSCode Command Palette. Type in Remote-SSH
and select Add New SSH Host…
Type in your username and External IP Address of your Compute Engine instance like your_username@XXX.XXX.XXX.XXX
Which you can find in the Google Cloud Platform following the instruction here.
Select the SSH configuration file or create a new one.
5. Connect to host
Go to the VSCode Command Palette again and select Remote-SSH: Connect to Host…
. It will launch a new VSCode window with the SSH connection like in the original instruction. Now, you will connect to any folder on your remote server as it was a local folder. This makes the work extremely productive.
Enjoy!
Links
- shell commands
- Filezilla
- Transmit 5
- Remote-SSH extension for the VSCode
- step-by-step tutorial to get started with VSCode and Remote-SSH
- download the SDK for Google Cloud
- complete instruction for connection to Google Cloud
- VSCode Command Palette
- Follow instruction here to find Google Could external IP
- original instruction for VSCode and SSH connection
For any questions please feel free to post a comment or you can connect with me on LinkedIn.