How to create and connect to a Linux VM using a Public Key.
Tags: devops, cloud, azure
Published: 2024-06-19 21:52:58

A Linux virtual machine (VM) can be created using the Azure portal. This guide will show you how to use the Azure portal to deploy a Linux virtual machine using Ubuntu Server 22.04 LTS as the operating system (OS) image. A connection will be established to the VM using Secure Shell (SSH) with a public key and the NGINX webserver will also be installed.

**Sign in to the Azure portal**
Sign in to the [Azure portal]((https://azure.microsoft.com/en-us/get-started/azure-portal))

**Creating a Virtual machine**

- Select "+ Create a resource" from the Azure homepage.


![Creating a resource](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1mzn3j3b2ky5ngs5rpkf.png)

- Click on the Virtual machine option

![Virtual Machine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tq9shxbpbtzybc05e4m0.png)

- In the **Basics** tab, under the **Project details**, select your Azure subscription and create a **Resource Group**

- 
![Creating a Virtual machine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxlojsqia1bnqt1lebt5.png)

- Under the **Instance details**, enter the virtual machine name, the region and select the availability options and security type as well as the image (Ubuntu server 22.04 LTS- x64 Gen2).
Next select the VM size to support the workload that you want run.
Note that the size chosen then determines such factors as processing power, memory and storage capacity.


![Creating a Virtual machine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufnke93pjjnkqz2xasxi.png)

- Under the **Administrator account**, select **SSH public key**

- Enter a **username** in the _Username field._ Leave the **SSH public key source** to its default and enter a name for the **Key pair name**.

- Next are the **Inbound port rules** Select **Allow selected ports** and choose the **selected inbound ports** which are **HTTP (80) and SSH (22)** from the drop-down.

- Leave the remaining default values and select **Review + create botton** at the bottom of the page.


![Creating a VM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pzo3v7f0y2yenwuzg3o1.png)

On the **Create a virtual machine** page, a summary of all the details of the VM to be created is displayed. Go ahead and click **Create** when you are ready to provision the VM.


![Creating a VM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6zst3r97cw0czrs5ye2.png)

- A **Generate new key pair** window will open, select _Download private key and create resource _.
A key will be downloaded as _(the name of the key with a .pem extension) ._ **myKey.pem**

- Ensure that you know where the key file is downloaded as you will need the path of the file.

- Wait for the deployment to complete then select **Go to resource**.


![key pair](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mny82z3x5q0hrtkua1kr.png)


![Deployment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92j2saaw1ci1v3nuk3ub.png)

## Connecting to the Virtual Machine

- 
To create an SSH connection with the VM, from a Windows machine, open a PowerShell prompt.

- 
Copy the path to where you have the _.pem_ file downloaded and use the public IP address generated by the VM.

```
ssh -i ~/Downloads/myKey.pem azureuser@52.233.89.56
```

![Powershell](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4xv349btovjj4y8to8g.png)

![Powershell](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2uhkm6lcblmhq0if4056.png)

```
 Tip

The SSH key you created can be used the next time you
create a VM in Azure. Just select the Use a key 
stored in Azure for SSH public key source the next 
time you create a VM. You already have the private 
key on your computer, so you won't need to download 
anything.
```

## Installing a Web server
To see what the VM is doing, install the NGINX webserver from the SSH session and then update the package sources alongside the latest NGINX package.

- Use the Linux commands below:

`sudo apt-get -y update
sudo apt-get -y install nginx`


![Linux command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/le0djsmhar73xejciniq.png)

- To view the default NGINX welcome page, type the public address of the VM as the web address in a browser of your choice.


![NGINX](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/psk1anjkhdxti5hcdcsd.png)

## Cleaning up resources
**Delete resources**
When no longer needed, you can delete the resource group, virtual machine, and all related resources.

- On the Overview page for the VM, select the Resource group link.

- 
At the top of the page for the resource group, select Delete resource group.

- A page will open warning you that you are about to delete resources. Type the name of the resource group and select Delete to finish deleting the resources and the resource group.


![Deleting resources](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hheum5kml55ukd3biazq.png)


![Deleting resources](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uyor981fvzhlv3r8fwk8.png)


![Deleting resources](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jrglcs96ob0kdfywtyr2.png)