Tech Monger

Programming, Web Development and Computer Science.

Skip to main content| Skip to information by topic

SSH Into Compute Engine via Public Key from Linux

If you have spun Linux compute engine instance on google cloud and want to connect it from some other linux machine then you can do it by configuring your public key with google cloud vm. In this article we will learn how to setup public key based ssh connection with your google compute instance and connect it using remote ubuntu machine.

Steps to configure Public Key with Google Cloud VM

  1. Generate Key Pair at Local Ubuntu Machine Using ssh-keygen
  2. techmonger@ubuntu:~$ cd ~/.ssh/
    
    techmonger@ubuntu:~/.ssh$ ssh-keygen 
    
    
    Generating public/private rsa key pair.
    Enter file to save the key (/home/techmonger/.ssh/id_rsa): id_rsa_google
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in id_rsa_google.
    Your public key has been saved in id_rsa_google.pub.
    

    Above will generate private and public keys inside files id_rsa_google and id_rsa_google.pub respectively. Note that we have not provided any input for pass phrase.

  3. Navigate to Compute Engine
  4. Open google cloud console and from left bar select compute engine. Select instance for which you want to create ssh connection.

    Edit Google Compute Engine Instance
    F1 Micro VM Instance Google Cloud Console
  5. Edit Instance and Add Public Key in SSH Box
  6. Copy paste the content of generated public key file id_rsa_google.pub in the box below SSH Keys.

    Configure Public Key Google Cloud VM
    SSH Key Configuration F1 Micro

    Scroll down and save once Key has been added.

  7. Get the external IP address of VM
  8. Scroll up in VM details section and from Network interfaces section check the External IP of the vm. In this example external ip is 93.184.216.34

  9. Test SSH Connection from Ubuntu Machine
  10. techmonger@ubuntu:~$ ssh 93.184.216.34
    
    The programs included with the Debian GNU/Linux system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    
    Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
    permitted by applicable law.
    Last login: Sun Aug 12 10:34:49 2018 from (ip-address)
    
    techmonger@google-vm:~$ 
    

How this SSH connection Works

  • When you add your public key in ssh box (step 3) it creates file authorized_keys inside home directory of vm user under .ssh and put the content of file inside it.
  • When you connect via your ubuntu machine having above public key inside .ssh folder, it establishes ssh connectivity with that public key.
  • Since that public key is now authorized, vm instance allows you to login.

Project Level Configuration

To login inside any VM instance of your project on google cloud you can configure public key at global level.

Select Compute Instance from sidebar and click on any VM instance in your project. Select Metadata from the sidebar and move to SSH keys tab to add project level public ssh key.

Google VM Login Global Level
Setup Project Level SSH Authentication for Google Cloud VMs

Conclusion

Google cloud console provides shell access to vm from browsers. But if you want to login from your local machine you can configure public key and directly ssh into your google vm and gain full shell access including root access. You can either setup public key login for specific vm or can configure project level login.

Tagged Under : Google Cloud Linux Ubuntu