Skip to content

Instantly share code, notes, and snippets.

@laidbackware
Last active November 6, 2023 11:39
Show Gist options
  • Save laidbackware/65ee73cf2c3e26e77187dcf060e57984 to your computer and use it in GitHub Desktop.
Save laidbackware/65ee73cf2c3e26e77187dcf060e57984 to your computer and use it in GitHub Desktop.
VSCode Remtee SSH with GCP Instance

Install VSCode - https://code.visualstudio.com/download Install Remote-SSH plugin inside VSCode from the extensions page

Linux/Mac

  1. If you have the GCP SDK installed on your local machine

    1. Run the following

      gcloud compute config-ssh
      Record the hostname it provides as an the SSH example.
      E.g. $ ssh jumpbox.us-central1-a.pal4pe-1109-mproud

    2. In VSCode open Remote-SSH: Open Configuration File from the command palete

      Edit your ssh config and add the ubuntu user to the end of the section for you jumpbox

      E.g.

      Host jumpbox.us-central1-a.abcdefg
          HostName 1.2.3.4
          IdentityFile /home/matt/.ssh/google_compute_engine
          UserKnownHostsFile=/home/matt/.ssh/google_compute_known_hosts
          HostKeyAlias=compute.123456789123456798
          IdentitiesOnly=yes
          CheckHostIP=no
          User ubuntu
      
  2. If you connected via the GCP cloud shell

    1. Make a connection to your jumpbox then end the connection

      If it asks you to use a region say no.

      gcloud compute ssh ubuntu@jumpbox
      No zone specified. Using zone [us-central1-a] for instance: [jumpbox].
      ....
      ubuntu@jumpbox:~$ logout
      Connection to 1.2.3.4 closed
      

      Record the IP address. 1.2.3.4 will be used as an example

    2. Run the following on the cloud shell machine

      cat ~/.ssh/google_compute_engine
      Get the contents in your clipboard

    3. On your local machine create the same file in your ssh directory

      vim ~/.ssh/google_compute_engine
      Paste in the contents of your clipboard

    4. Set the permissions on the file to be 600 chmod 600 ~/.ssh/google_compute_engine

    5. Test the key works by sshing into your machine, using the IP address from step 1. ssh -i ~/.ssh/google_compute_engine ubuntu@1.2.3.4

    6. Open Remote-SSH: Open Configuration File from the command palete Set the contents to be the following, using the IP address from step 1.

      Host jumpbox
          HostName 1.2.3.4
          IdentityFile ~/.ssh/google_compute_engine
          User ubuntu
          Port 22
      
  3. Open Remote-SSH: Connect To Host in the command palette

Select the machine which matchs your ssh config file.

Windows

  1. Ensure OpenSSH is installed
    https://docs.microsoft.com/en-gb/windows-server/administration/openssh/openssh_install_firstuse

  2. To generate an SSH key pair open powershell and run.

    mkdir $HOME/.ssh # OK if it already exists and generates an error
    ssh-keygen -b 2048 -t rsa -f $HOME\.ssh\jumpbox-pal -q -N """"
    cat $HOME\.ssh\jumpbox-pal.pub
    
  3. Connect to the jump box gcloud compute ssh ubuntu@jumpbox

  4. Add the contents of your clipboard to the end of your authorized_keys file

    vim ~/.ssh/authorized_keys

  5. End connection to the jump box and record IP address. 1.2.3.4 will be used as an example

    ubuntu@jumpbox:~$ logout
    Connection to 1.2.3.4 closed
    
  6. Test the connection by trying to SSH into the jump box.

    C:\Windows\System32\OpenSSH\ssh.exe -i $HOME\.ssh\jumpbox-pal ubuntu@1.2.3.4

  7. In VSCode open Remote-SSH: Open Configuration File from the command palete

    Add the following to the end, updating the IP address recorded in the previous step.

    Host jump_box
        HostName 1.2.3.4
        IdentityFile ~/.ssh/jumpbox-pal
        User ubuntu
        Port 22
    
  8. (Windows only) Open Remote-SSH: Settings from the command palette

    For the setting Remote.SSH: Path set:
    C:\Windows\System32\OpenSSH\ssh.exe

  9. Open Remote-SSH: Connect To Host from the command palette

    Select jump_box from the list
    Select OS type to be Linux

@pparuzel
Copy link

Exactly what I needed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment