DEV Community

Cover image for Step Guide on how to create and connect to a Linux VM using a Public key
Ikponmwonsa Okundigie
Ikponmwonsa Okundigie

Posted on

Step Guide on how to create and connect to a Linux VM using a Public key

Creating a Linux virtual machine (VM) on Microsoft Azure involves several steps. Here’s a comprehensive guide to help you set up an Azure Linux VM:

Step-by-Step Guide:
If you don't have an Azure subscription, create a free account before you begin.
Sign in to Azure Portal:
Go to portal.azure.com and sign in with your Azure account.

Create a Resource Group (Optional):
A resource group is a logical container for grouping your Azure resources. If you don’t have one already, you can create it:

In the Azure Portal, click on Resource groups in the left-hand menu.

Image description

Click on + Add to create a new resource group.
Provide a name, choose a subscription, and select a region. Click Review + create, then Create.
Image description

Create a Virtual Machine:

Search for and select Virtual machine.
Navigate to the Virtual machines page, click on Create, and then select Virtual machine. This action opens the Create a virtual machine page then select Azure virtual machine.
Image description

On the Basics tab, ensure the appropriate Azure subscription is selected under Project details. Opt to create a new resource group name or select an existing one.

Image description

In the Instance details section, enter a name for your VM as the name for the Virtual machine.

Region: Select the region where your VM will be deployed.
Image: Choose your Linux distribution (Ubuntu Server 20.04 LTS - x64 Gen2 (free services eligible)
Security type: Select “Standard”.
VM architecture: x64
Size: Select an appropriate VM size based on your requirements and budget.

Image description

Choose SSH public key under Administrator account.
Enter azureuser as the default Username.
Keep the default setting for SSH public key source, which is "Generate new key pair", and name the Key pair TanikLinux_key which is auto generated.

If using SSH public key: Enter your SSH public key data or browse to upload your public key file.
If using password: Enter a username and password for SSH access.

Image description

In the Inbound port rules section under
Public inbound ports, opt for "Allow selected ports". Then, from the drop-down menu, select SSH (22) and HTTP (80).
Review the settings you’ve configured for your VM, then click the Review + create button located at the bottom of the page to validate the configuration.
Image description

Maintain the default settings for other options. Note that the default size and pricing displayed are illustrative; actual size availability and pricing vary depending on your region and subscription.

After Validation passed, on the Create a virtual machine page, review the details of the VM you are about to create. When ready, click on the Create button to deploy your Azure Linux VM.
Azure will now provision and deploy your VM based on the settings you specified.
Once the Generate new key pair window appears, choose Download private key and create resource. Your key file will download as TanikLinuxVM_key.pem. Ensure you note the download location of the .pem file, as you will need its path in the next step.
After the deployment completes, click on
Go to resource to access your VM via SSH.
Image description

On the page for your new VM, select the public IP address and copy it to your clipboard.

Connecting to Virtual Machine:
Establish an SSH connection to the virtual machine:
On a Windows machine, search for a command prompt and click Run as administrator.
Image description

Click on your download to get your key file and ensure you note the download location of the .PEM file, then you right click on your .PEM File and select on Properties to locate your file path as you will need its path in the next step.

Image description

For connection you will need your command prompt, username and the IP address to run on your terminal. In your command prompt, initiate an SSH connection to your virtual machine and substitute the VM's IP address for the placeholder, and replace the file path with the actual path where the key file was downloaded.(ssh -i "/path/to/your/private-key-file.pem" username@your-vm-public-ip)

Image description

Tip
You can reuse the SSH key you created for future Azure VMs by selecting Use a key stored in Azure for SSH public key source. Since you already have the private key on your computer, there's no need to download anything.

Install web server
To see your VM in action, install the NGINX web server. From your SSH session, update your package sources and then install the latest NGINX package.

  1. sudo apt-get -y update
  2. sudo apt-get -y install nginx When done, type exit to leave the SSH session.

Image description

Image description

View the web server in action
To view the default NGINX welcome page, use any web browser to access the VM's public IP address as the web address. You can find the public IP address on the VM overview page or in the SSH connection string you used earlier.

Image description

Clean up resources
Delete resources
When no longer needed, delete the resource group, virtual machine, and all related resources:

  • On the Overview page of the VM, click on the Resource group link.
  • At the top of the resource group page, 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.

Auto-shutdown
If you are still in need of the VM, Azure provides an Auto-shutdown feature for VMs to help manage costs and to avoid been billed for unutilized resources:

  1. On the Operations section for the VM, select the Auto-shutdown option.

  2. Configure the auto-shutdown time on the following page by selecting On to enable and setting a suitable time that is workable by you.

  3. Once configured, click Save at the top to activate the Auto-shutdown settings.

Additional Information:
Be mindful of Azure VM pricing, especially the cost implications of VM size and usage. Always follow best practices for securing your Azure VM, such as using SSH keys instead of passwords where possible, configuring network security groups, and applying OS updates regularly.

Top comments (0)