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
- Generate Key Pair at Local Ubuntu Machine Using
ssh-keygen
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
andid_rsa_google.pub
respectively. Note that we have not provided any input for pass phrase. - Navigate to Compute Engine
Open google cloud console and from left bar select compute engine. Select instance for which you want to create ssh connection.
- Edit Instance and Add Public Key in SSH Box
Copy paste the content of generated public key file
id_rsa_google.pub
in the box below SSH Keys.Scroll down and save once Key has been added.
- Get the external IP address of VM
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
- Test SSH Connection from Ubuntu Machine
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.
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.