How to Create and Connect to a Linux VM in Azure Using Public Keys

Francis ChukwuFrancis Chukwu
3 min read

You can create virtual machines (VMs) in Azure using the Azure portal, a browser-based interface for managing Azure resources. This QuickStart guide demonstrates how to deploy a Linux VM running Ubuntu Server 22.04 LTS through the Azure portal. After setting up your VM, you’ll connect to it using SSH and install the NGINX web server to see it in action.

This article provides step-by-step instructions to guide you through the entire process

Sign in to Azure

Create virtual machine

  1. In the Azure portal, enter Virtual Machines in the search bar..

  2. Under Services, select Virtual machines.

  3. On the Virtual machines page, click Create and then select Virtual machine. This opens the Create a virtual machine page.

  4. In the Basics tab, under Project details, make sure the correct subscription is selected and then choose to Create new resource group. Give the Resource Group a name.

    In the Instance details section:

    1. Enter a name for your virtual machine under Virtual machine name.

    2. For Image, select Ubuntu Server 22.04 LTS - Gen2.

    3. Leave the remaining settings as their default values.

    4. Select size, Available sizes and pricing may vary based on your region and subscription.

    5. Under Administrator account, select SSH public key.

    6. In Username, enter azureuser.

    7. For SSH public key source, keep the default option, Generate new key pair.

    8. Enter the Key pair name.

    9. In Inbound port rules, under Public inbound ports, select Allow selected ports. Then, choose SSH (22) and HTTP (80) from the drop-down menu

    10. Click Review + create at the bottom of the page

    11. On the Create a virtual machine page, review the details of the VM. Click Create.

    12. When the Generate new key pair window opens, click Download private key and create resource. The key file, named myKey.pem, will download. Be sure to note where the .pem file is saved, as you’ll need its path in the next step

    13. Once the deployment is complete, click Go to resource.

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

      Connect to virtual machine

    15. If you’re using a Mac or Linux machine, open a Bash terminal and set read-only permissions on the .pem file with the following command: chmod 400 ~/Downloads/myKey.pem.

    16. If you’re on a Windows machine, open a PowerShell prompt.

      Next, establish an SSH connection to your virtual machine. Replace <your_VM_IP> with the public IP address of your VM and update the path to the .pem file to match where you downloaded it:

      Install web server

    17. To see your VM in action, install the NGINX web server. From your SSH session, update the package sources and then install the latest version of the NGINX package. By entering the command below

      View the web server in action

    18. Open a web browser and enter the VM's public IP address in the address bar to view the default NGINX welcome page. You can find the public IP on the VM's overview page or in the SSH connection string you used earlier

Summary

In this deployment, we set up a Linux virtual machine (VM) in Azure using the Azure portal. After creating the VM with Ubuntu Server 22.04 LTS, we connected to it securely via SSH using a generated key pair. Once connected, we installed the NGINX web server to verify the VM's functionality. Finally, by entering the VM's public IP address in a web browser, we confirmed successful deployment by viewing the NGINX welcome page. This deployment showcases basic VM setup, secure access, and web server installation on Azure.

3
Subscribe to my newsletter

Read articles from Francis Chukwu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Francis Chukwu
Francis Chukwu