DEV Community

Tracy Chinedu
Tracy Chinedu

Posted on

Create and Connect to a Linux VM using a Public Key

Azure virtual machines (VMs) can be created through the Azure portal. This method provides a browser-based user interface to create VMs and their associated resources. This blog post shows you how to use the Azure portal to create a virtual machine (VM) that runs on Linux server and connect using Powershell.

In this blog you will see how to create and connect a Linux VM using public key.Public key authentication allows you to access a server via ssh without password.

If you don't have an Azure subscription, create a free account before you begin.

Sign in to Azure

Sign in to the Azure portal.

Create virtual machine

  • Enter virtual machines in the search.

  • Under Services, select Virtual machines.

  • Subscription: Choose the appropriate subscription.

  • Resource Group: Create a new or select an existing resource group.

  • Virtual machine name: Give your VM a unique name.

  • Region: Choose the data center region of your choice, preferably a region closest to you.

  • Availability options: Select the availability preferences.

  • Choose a linux server for the Image.

Image description
Image description

  • Select size of the memory Image description

Security and Authentication

  • Authenticator type - Select SSH public key
    Azure will automatically generate an SSH key pair for you to
    store for future use. It is a fast simple and secure way to
    connect to the VM.

  • SSH Key type - Select RSA SSH Format
    Image description

  • Enter key pair name

  • Public inbound ports- Select allow selected ports

  • Select HTTP(80),SSH(22)

  • Click Review + Create

Image description

  • VM is validated and shows the costing information.
  • Select create. Image description

Download private key and create resource

The private key will be saved in the computer.
Image description

Deployment is complete

-Select go to resource
Image description

VM is ready

Here we can see all the details on the VM that was just created. Copy the public IP address because we would use it to connect.
Image description

Connecting to the VM using power shell

  • Navigate to Windows Powershell on a PC.
  • Run this command the command below ssh -i "path to the private key on the pc" username@publicIP

Image description
After connecting to the VM,we run some sudo commands to test
sudo apt-get -y update
Image description

sudo apt-get -y installĀ nginx
Image description

Testing Public IP on a Browser

Image description

Clean Up

After creating and deploying the Vm, since this project is just for practice.We clean up by deleting the resource group to avoid incurring cost.

  • Search for resource group
  • Select the resource group that was created for this VM

Image description

  • Select all resources that were created
  • Click delete
  • Enter resource group name to confirm deletion

Image description

Conclusion

With a secure shell (SSH) key pair, you can create virtual machines (VMs) in Azure that use SSH keys for authentication. This article elaborates on how to quickly generate and use an SSH public-private key file pair for Linux VMs.

Top comments (0)