Google Cloud Platform
Google Cloud Platform

 

 

Google Cloud offers a lot of interesting features and makes it very easy for you to get started quickly. I’ve however had a hard time accessing and making changes to my development files as the gcloud command-line tool is not always well documented. Through experimentation and trial & error I managed to find a way to access my development files and gain root permissions to edit them.

Accessing your VM instance

Step 1 – Generate an SSH key pair

To connect to your Linux VM instance you must generate an SSH key pair. An SSH key pair consists of a public SSH key file and a private SSH key file.

Follow those steps to generate a key pair:

  1. Enter ssh-keygen -t rsa in your terminal
  2. The ssh keygen utility will prompt you to indicate where to store the key. Edit the path if needed and press enter
  3. Type in a passphrase
  4. Your private and public key are generated in the .ssh directory
  5. Enter the following command in your terminal to copy your public key: cat ~/path-to-your-ssh-directory/id_rsa.pub
  6. Your public key is copied to your clipboard

Step 2 – Add your public key to your project metadata

  1. Go to your Google Cloud Platform dashboard
  2. Open the menu on the left and go to Compute Engine > Metadata
  3. Go to the 2nd tab “SSH Keys” and click Edit
  4. Click on Add item and paste your public key in

Your can now connect through SSH or SFTP to your VM instance.

Making changes

SFTP

  1. Download Filezilla (this is what I use but you can use any other FTP client)
  2. Open the app
  3. Go to the Setting page > SFTP
  4. Click on Add key file…
  5. Select your private key file

On MacOS, key files are usually located in /Users/{user name}/.ssh. Note however that dot files and dot folders are hidden by default. Make sure to hit “CMD+SHIFT+. in the Filezilla finder to make the .ssh folder appear.

SSH folder
SSH folder

Once your private key file is added to Filezilla, click OK and go back to the main interface of the application.

  • You can now enter your IP address in the host field (for ex: sftp://0.0.0.0 – 0.0.0.0 being your external IP).
  • Write the username that is at the very end of your public key file.
  • Do not enter a password
  • Write 22 in the port field
  • Click connect

You are now connected to your VM instance. To view your development files go to /var/www/html/. Some files may require root permissions to be edited. The only way to get root permission is to connect via SSH.

SSH from the browser

  1. Go to your VM instance page
  2. Click on SSH under the Connect column
  3. A popup will appear
  4. Enter ssh -i [PATH_TO_PRIVATE_KEY] [USERNAME]@[EXTERNAL_IP]
  5. Go to /var/www/html (this is where your solution resides)

Here’s what you need to do to become sudo (root):

  1. Enter “sudo passwd”
  2. Enter a password
  3. Enter “su”
  4. Enter the password you defined in step 1
  5. You’ve become root

At this point, I would recommend either making changes using the vim editor (basic vim commands) or changing the permissions of the files you want to change to allow edits through SFTP.