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.
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.
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.
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.
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: 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.
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.
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.
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”.
Then Click "Review + create". This will show validation passed as highlighted below.
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.
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”
Next, click on “Connect” and then “Connect”. This will take you to a page with other options to connect your VM.
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
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.
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.
Connect via SSH Key Authentication: Open your Command Line Interface on Windows or Terminal in case of macOS/Linux.
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
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
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.
That is it. You have successfully created a Linux virtual machine on Azure and connected to it via SSH key authentication.
Top comments (0)