Guide on Creating a Linux Virtual Machine on Azure and Connecting to it via SSH Key Authentication
Tags: beginners, aws, cloud, serverless
Published: 2024-04-29 10:35:17

**Step-by-Step Guide on Creating a Linux Virtual Machine on Azure and Connecting to it via SSH Key Authentication**

This document describes how to create a Linux Virtual Machine on Azure and connecting to it via SSH key authentication. To do this, you should already have Azure account and subscription. The subscription could either be a paid subscription, free for new users, or student’s account.
 
**Sign in to Azure Portal**: Go to the Azure Portal (https://portal.azure.com) and sign in with your Azure account. After signing in, your screen will display as shown below.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnriikj7r2j8w064vw55.png)

**Navigate to Virtual Machines**: Once you're logged in, navigate to the "Virtual Machines" service as highlighted above or you can find it by searching for "Virtual Machines" in the search bar at the top. Once you click on the virtual machine, your screen will be displayed as shown below.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/46h2do713wyssrcl7uwr.png)

Then you will click on the "Create" button, anyone you click will display a drop down as shown below. You will then click on the Azure virtual machine highlighted in red arrow. This will take you to the project details where you will begin to fill the basic configuration for you to start creating your virtual machine.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfwwwy2jt9qzwv34u7nd.png)

**Create a Virtual Machine**: Here, you are going to create a virtual machine that runs on linux, you will begin by filling the basic configuration starting with the subscription.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20p4lx04drft8nell2em.png)

**Subscription**: Select your Azure subscription.
 
**Resource Group**: Create a new one or select an existing resource group. To create a new one, click on the “create new” to give name to your resource group. Resource group could also be autogenerated based on the name you asigned to your VM.

**Virtual Machine Name**: Enter a unique name for your VM.

**Region**: Choose the region where you want to deploy your VM. A region refers to a specific geographical location with data centres that host infrastructure and services. Click on the drop down to select your availability options. Here I chose (US) East US region. Next is availability zone.
 
**Availability Zone**: Many Azure regions provide availability zones, which are separated groups of datacenters within a region. Availability zones are close enough to have low-latency connections to other availability zones. However, availability zones are far enough apart to reduce the likelihood that more than one will be affected by local outages or weather. Availability zones have independent power, cooling, and networking infrastructure. They're designed so that if one zone experiences an outage, then regional services, capacity, and high availability are supported by the remaining zones. They help your data stay synchronized and accessible when things go wrong. For the purpose of this project, I selected only  zone 1 as indicated in the screenshot below. However, you could select 2 or all the 3 zones available.

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

**Image**: Select the operating system image you want to use for your VM. You will click on the drop down to select the operating system you want for your VM. Select “Ubuntu Server” as indicated above, or any other Linux distribution you prefer.  
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zq5hppizppklvjdsd0c.png)

**Authentication Type**: You will select the authentication type. Here I selected SSH public key for authentication since I intended to connect to my VM via SSH key authentication. However, you may chose the second option if you intend to connect via Password.
 
**Username**: The username above is auto generated, you may also wish to change the username to a preferred name.

**SSH Public Key Source**: Click on the SSH Public Key Source drop down to either generate a new key pair or use an existing one.
 
**Key pair Name**: You will also assign a unique name to your key pair. Next is inbound port rule.

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

**Public Inbound Ports**: This allows you to indicate which virtual machine network port are accessible from the public internet. Here I indicated that selected ports should be allowed. And my selected inbound port is SSH(22). Having done with the Basics configuration, you will then proceed to Disks.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mh9wyw76hh6iv2sjdvkj.png)

**Disks**: On the Disk page, click on the OS disk type dropdown and select any disk type of your choice, here I selected “Premium SSD”. You may leave the other columns as default. Then click “Next:Networking” or skip to “Review+Create”

**Networking, Management, monitoring, Advanced, Tags**: These pages usually displayed as default. Find their default screenshots below. However, you may wish to skip them and proceed to “Review+Create”.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6y7f3hujfni5t2mqw6o9.png)

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

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

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

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

Then Click "Review + create". This will show validation passed as highlighted below.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xavnqitwcanwziv8lw6f.png)

Then click “Create” . This will take you to the next page to generate new key pair. Then, you will click on “Download private key and create resource” as shown below. The new key pair will then be downloaded on your PC, while Azure will also provision your VM.

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

**Wait for Deployment**:  Azure will now provision your virtual machine. This process may take a few minutes. Then the next page will show that your deployment is complete as shown below, then you will click on “Go to Resource”

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

Next, click on “Connect” and then “Connect”. This will take you to a page with other options to connect your VM.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kt2637mwgiaiiyenaky6.png)

**Retrieve Public IP Address**: Once the virtual machine is deployed, note down the Public IP address on Azure portal overview page. 
**Note**: Also go to your download and save the downloaded key pair into a file for later use when connecting via SSH.

**Note**: Other options for connecting to your VM are “SSH using Azure CLI” and “Native SSH” as displayed below
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6zdqrfygaryisma20h32.png)

If you select “Native SSH”, you will see instruction on how to configure and connect in the highlighted box below. However, you are not going to connect using “Native SSH” in this project. 

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

If you select “SSH using Azure CLI”, you will see instruction on how to connect in the highlighted box below leading you to how to configure and connect. However, you are not going to connect using “SSH using Azure CLI” in this project. 
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u6lt3nthxmp9bjkoaf8q.png)

**Connect via SSH Key Authentication**: Open your Command Line Interface on Windows or Terminal in case of macOS/Linux.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kzad8ysjv40kw75bm3tx.png)

**Then enter the following command to connect to your VM**:
⁠ssh -i filepath username@public_ip_address
**Note**: filepath is the downloaded SSH keypair you earlier saved on your PC

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

**Note**: my filepath above is “/Users/a12345/Downloads/AySshVm_key.pem” without the quote. This can be retrieved from your earlier downloaded key pair, when you “copy as path” from your download or from saved document file. Also note, that mac users can copy directly by rightclicking on the download and click copy, when pasted it will appear as “file path”

Replace "username" with your VM username (usually "azureuser" for Ubuntu) and "public_ip_address" with the IP address you noted down earlier.Example: azureuser@123.456.789.012
When prompted “Are you sure you want to continue connecting (yes/no/fingerprint), type yes
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1s6j5w94rmfhil8coz7v.png)

However, you may not be prompted “Are you sure you want to continue connecting (yes/no/fingerprint), if you have made earlier attemp to connect. Therefore, once you enter the command “ssh -i filepath username@public_ip_address” and press enter, you will be connected to your Azure virtual machine via SSH key authentication in the terminal as shown below.
⁠
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1vlpweo80079qgoc9s7e.png)

That is it. You have successfully created a Linux virtual machine on Azure and connected to it via SSH key authentication.