DEV Community

Cover image for How to create and connect to a Linux VM using a Public Key.
Olaolu' Afolami
Olaolu' Afolami

Posted on • Edited on

How to create and connect to a Linux VM using a Public Key.

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

Creating a Virtual machine

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

Creating a resource

  • Click on the Virtual machine option

Virtual Machine

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

  • Creating a Virtual machine

  • 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

  • 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

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

  • 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

Deployment

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


Enter fullscreen mode Exit fullscreen mode

Powershell

Powershell



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.

Enter fullscreen mode Exit fullscreen mode




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

  • 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

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

Deleting resources

Deleting resources

Top comments (0)