DEV Community

Cover image for HOW TO CREATE AND CONNECT TO A LINUX VM USING A PUBLIC KEY
Omoniyi
Omoniyi

Posted on

HOW TO CREATE AND CONNECT TO A LINUX VM USING A PUBLIC KEY

In this blog, you will be taught how to create a Linux Virtual Machine running on Ubuntu image, how to connect to the virtual machine through a SSH protocol using a Public Key and go the extra mile of creating a web application, connect to it using the public key and lastly browse the web server.

SIGN IN TO AZURE PORTAL
To do anything on Azure portal, the first thing is to login to the portal (www.portal.azure.com) with your sign in details (Username and Password). You may need to register and sign up if you are a new user.

CREATE A VIRTUAL MACHINE ON AZURE PORTAL
After signing in, the next thing to is to create VM. The steps are as detailed below with images and annotations

  1. Enter virtual machines in the search or select "Create a Resource" or select "Resource Group". any of the 3 will guide your steps.
    Image description

  2. Select Virtual Machine
    Image description

  3. In the Virtual machines page, select Create and then Virtual machine. The Create a virtual machine page opens.
    Image description

  4. In the Basics tab, under Project details, make sure the correct subscription is selected and then choose to Create new resource group or select an existing one. I entered PRENNIEX for sample purpose. Type in the preferred virtual name for your VM (PreshlinuxVM for me) , pick the other options as in the image
    Image description

  5. Let the security type be standard and choose Ubuntu Server 22.04 LTS - Gen2 for your Image. This is one of the differences between Windows Vm and Linux VM. Leave the other defaults. Size availability and pricing are dependent on your region and subscription. If you experience any error at any stage or during validation and its referencing the Region, please, option for another region.
    Image description

  6. Choose the memory size of your VM
    Image description

  7. Under Administrator account, select SSH public key. This basically indicates that you want to connect to your VM using public keys. In Username enter your desired username (PreshlinuxVM for me). For SSH public key source, leave the default of Generate new key pair, pick other options as in the image
    Image description

  8. Under Inbound port rules > Public inbound ports, choose Allow selected ports and then select SSH (22) and HTTP (80) from the drop-down. Then choose,
    Image description
    Leave the remaining defaults and then select the Review + create button at the bottom of the page.

  9. When the Generate new key pair window opens, select Download private key and create resource. Your key file will be download as PreshlinuxVM.pem. Make sure you know where the .pem file was downloaded; you will need the path to it in the next step.

Image description

  1. When the deployment is finished, select Go to resource
    Image description

  2. On the page for your new VM, select the public IP address and copy it to your clipboard.
    Image description

CONNECT TO THE VIRTUAL MACHINE

  1. Go to windows, search for Powershell and run as administrator
    Image description

  2. At your prompt, open an SSH connection to your virtual machine. Replace the IP address with the one from your VM, and replace the complete path to the .pem ( for this sample, the path is c:/user/prenniex/Downloads/PreshlinuxVM.pem) This is the path that the key file was downloaded. For sample sake, the command is as below
    ssh -i c:/user/prenniex/Downloads/PreshlinuxVM.pem PreshlinuxVM@40.84.55.12

INSTALL A WEBSERVER
To see your VM in action, install the NGINX web server. From your SSH session, first update your package sources and then install the latest NGINX package with the commands below
**sudo apt-get -y update
sudo apt-get -y install nginx

**
Image description

BROWSE THE WEBSERVER
Use a web browser of your choice to view the default NGINX welcome page. Type the public IP address of the VM as the web address. The public IP address can be found on the VM overview page or as part of the SSH connection string you used earlier.
Image description

DO NOT FORGET TO CLEAN UP RESOURSES BY DELETING RESOURCE GROUPS AND THE RESOURCES THAT ARE NOT NEEDED.

GOODLUCK!

Top comments (0)