How To Create and Connect To Linux Virtue Machine Using A Public Key
Tags: azure, linux, virtualmachine, cloudcomputing
Published: 2024-06-21 13:06:15

**Table of contents**
Step 1: Sign in to Azure Portal 
Step 2: Configure Basic Settings 
Step 3: Review and Create Virtue Machine
Step 4: Create the Virtual Machine
Step 5: Connect to virtual machine 
Step 6: Install web server
Step 7: View the web server in action

In this blog, I'll be creating an Azure Virtual Machine
Below are step-by-step instructions:

**Step 1: Sign in to Azure Portal** 
Go to the Azure Portal (portal.azure.com) and sign in using your Azure account credentials.

**Step 2: Configure Basic Settings**

- 
Search for Virtual machines. In the Virtual machines page, select “+ Create” and select Virtual machine them virtual machine page opens.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fjeztqzjs9jumng7nyi4.png)


- 
In the Basics tab, under Project details, make sure to select the right subscription and then either select an existing resource group or choose to Create new resource group.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6tebq1jl52zxz22qs33q.png)



- 
Under Instance details, enter the Virtual machine name, and choose Ubuntu Server 22.04 LTS - Gen2 for your Image. Leave the other defaults. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kgesntlbfoixul6nxv6q.png)


- 
Under Administrator account, select SSH public key. Enter azureuser as the username.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n529oov4em59ivn4q010.png)

- 
For SSH public key source, leave the default of Generate new key pair, and then enter the Key pair name. Under Inbound Ports, this is the port that we will be using to connect to our Virtual Ubuntu Machine and then select SSH (22) and HTTP (80) from the drop-down.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgh9g846tw06j7d3xsoc.png)


Leave the remaining defaults and then select the Review + create button at the bottom of the page.

**Step 3: Review and Create Virtue Machine**

Review your settings and make any changes if required.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfo48j9kpncfkxc60zea.png)


**Step 4: Create the Virtual Machine**

- 
After clicking on "Create," Azure will start deploying your virtual machine. It may take a few minutes for the deployment to complete.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yn9qhcfn931iizzeewpp.png)


- 
When the Generate new key pair window opens, select Download private key and create resource. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pxpjtg1mclu3ogqd8i2z.png)


- 
When the deployment is finished, select Go to resource.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kzkg9a3ekupi3nmovn5x.png)


- 
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/qbga0zstu2vs2gus63o8.png)


**Step 5: Connect to virtual machine **

Create an SSH connection with the VM.

- 
On your Windows computer, open a PowerShell prompt.

- 
At your prompt, open an SSH connection to your virtual machine. Copy and paste the IP address on your Virtual machine.

_Then type in the below command,_

ssh -i "/path/to/your/private-key-file.pem" Username@Public IP Address

**Step 6: Install web server**

To know if your VM is working, install the NGINX web server via either Command Prompt or Powershell Prompt. 

Type the below commands one after the other

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

When done, type exit to leave the SSH session.


**Step 7: View the web server in action**

Use any web browser of your choice to view the default NGINX welcome page. Tycopy amd paste the public IP address of the VM as the web address.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sj2qmbwhgm61xehtju41.png)