HOW TO CREATE AND CONNECT TO A LINUX VM USING A PUBLIC KEY
Tags: signintoazure, createavirtualmachine, connecttothevirtualmachine
Published: 2024-06-21 20:35:22

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51oi56xyjlyc1p0hyq1n.jpg)

2. Select Virtual Machine
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zla9ez1bbpoacg8pzuow.jpg)

3. In the Virtual machines page, select Create and then Virtual machine. The Create a virtual machine page opens.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aq27xq9na2wtbzxx2g8f.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0bihu1gtrrfpfquwuw35.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yz6fz4g0cl51kmqj5ks1.jpg)

6. Choose the memory size of your VM
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3kfvtukg04k8hj34ibxk.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/duz198q1g3wjve0x2vil.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e7xn0hveaxjxr341nqd6.jpg)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cdpetpb7ss4rk0wifoxz.jpg)

100. When the deployment is finished, select Go to resource
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/944ynnso1a7edksgrl3o.jpg)

110. On the page for your new VM, select the public IP address and copy it to your clipboard.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8g4kjzkj9m80x5xdumsh.jpg)

**CONNECT TO THE VIRTUAL MACHINE**
1. Go to windows, search for Powershell and run as administrator
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ddb1f6xm6rjpgt172hau.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/megy2pezfth2p3o2e3yf.jpg)


**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3t66d7d5y714gg2icllj.jpg)



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

GOODLUCK!