Introduction
In this article you will learn how to create your own virtual machine in the Google Cloud step by step.
Virtual machines are also called “instances” in the Google Cloud and you can run such instances with the operating system of your choice, of which more later.
Tutorial
Create a Google Account
In order to create a virtual machine in the Google Cloud we first need to have a Google Account which is linked to the Google Cloud.
- Go to https://console.cloud.google.com
- Signin or register an account there. Once you signup you will get free credits to run services in the Google Cloud for a trial period of a few months, which is very neat.
Setup a virtual machine
Then it’s pretty easy to setup a virtual machine there:
- Go to Compute Engine —> VM instances.
- Click on “Create instance” at the top.
- In the new window, type in a name for your virtual machine instance, pick a region (where the virtual machine should be hosted geographically) and pick a machine type (“small” should be enough for our demo purpose).
- In the section “Boot disk” click on the “change” button and choose an operating system which should run on your virtual machine. I usually pick Ubuntu.
- Click “create”.
- The you should see an overview screen which indicates that the new virtual machine is up and running (after a short start up period).
- You can connect to that machine by clicking on the “SSH” button on the right side of the virtual machine.
- Voila, you created your first virtual machine which is running in the Google Cloud.
Access the virtual machine from your terminal
If you want to connect to the virtual machine from your terminal, follow these instructions (valid for a computer with a Linux or OSX distribution on it).
1. If you don’t have an existing private SSH key file, then create a new one with the following commands.
- In your terminal type in:
ssh-keygen -t rsa -f ~/.ssh/ssh-key -C YOUR_USERNAME
This command creates a private and a public SSH key file which are named ssh-key and ssh-key.pub
- Restrict access to your private SSH key file so that nobody else except for you can access it.
chmod 400 ~/.ssh/ssh-key
2. Locate your public SSH key file and type in your terminal
cd ~/.ssh/
3. Then find the name of your public SSH key file (in our case it’s ssh-key.pub) and output the file content into the terminal.
cat ssh-key.pub
4. The output should start with “ssh-rsa” followed by a lot of numbers and characters and ends with your username.
5. Copy the whole output.
6. Click on your Google Cloud instance name (under Compute Engine —> VM instances) and then on the “Edit” pencil at the top of the page.
7. If you scroll down you will see something like “You have 0 SSH keys”.
8. Click on “Show and edit” and paste your file content into the text area that has just appeared.
9. Next click on “Save” at the bottom of the page.
10. Go into your local terminal and type in
ssh -i ~/.ssh/ssh-key YOUR_USERNAME@IP_ADDRESS_OF_YOUR_VIRTUAL_MACHINE
11. You can find the IP address in the VM instances overview (Compute Engine —> VM instances) in the section “External IP”.
12. Once it’s connecting, accept the ECDSA key fingerprint by typing “yes” and you should be then connected to your virtual machine.
13. Congrats you made it!
Hint: Don’t forget to delete the instance afterwards since it’s draining your credits while it’s running.
Final thoughts
As you have just seen it is super easy and smooth to create your own virtual machine in the Google Cloud. Have fun running your own virtual machines in the Google Cloud!