DEV Community

Cover image for HOW TO CREATE A VIRTUAL MACHINE WITH AZURE CLI
Olalekan Oladiran
Olalekan Oladiran

Posted on • Edited on

HOW TO CREATE A VIRTUAL MACHINE WITH AZURE CLI

What is Azure CLI?

Microsoft's cross-platform command-line tool for controlling Azure resources is called Azure CLI. It is accessible via the Azure Cloud Shell browser or on macOS, Linux, and Windows.

You may use the Azure CLI to manage Azure resources from scripts or the command line, including virtual machines and storage. Now let's use Azure Virtual Machines to create virtual machine.

How to create a Linux VM with the Azure CLI

  • Log into your Azure account
  • once logged in click CloudShell

Image description

  • Change the terminal to bash if it is on PowerShell

Image description

  • Create a Resource Group by using the code: az group create --name MyResourceGroup --location eastus

Image description
This will create a resource group labelled as MyResourceGroup where your Virtual Machine will be located. Replace MyResourceGroup with your desired resource group name and eastus with your preferred Azure region.

  • Create the Virtual Machine the following code:
    az vm create \
    --resource-group MyResourceGroup \
    --name MyVM \
    --admin-username azureuser \
    --image Ubuntu2204 \
    --generate-ssh-keys \
    --verbose
    NOTE:
    --resource-group: Specify the resource group you created in the previous step
    --name: Choose a name for your VM (e.g., MyVM).
    --image: Specify the OS image for the VM. In this example, we use Ubuntu 22.04.
    --admin-username: Set the admin username for SSH access.
    --ssh-key-value: Provide the path to your public SSH key for authentication.

  • Monitor VM Creation: The Azure CLI will provide feedback on the VM creation process, including its status. Once the VM is created, you'll see information about the VM, including its public IP address.

Image description

  • Access the Virtual Machine.

How to connect to the created VM using SSH

With the Secure Shell (ssh) tool, we can easily verify that the Linux virtual machine (VM) is operational by using its public IP address. Recall that we need to specify that because we set our admin name to azureuser. Use the public IP address that comes from the instance that is currently executing.
ssh azureuser@public-ip
Replace azureuser with your username and public-ip with the VM's public IP address.

Image description
The virtual machine system information will also be displayed

Image description
You can also confirm the Virtual Machine by logging into Azure portal and searching for the resource group the VM is located.

Top comments (0)