Create a Virtual Machine

Learn how to create and configure virtual machines (VMs) in Azure. Azure VMs offer flexible compute solutions. You can create VMs for Windows or Linux by using the Azure portal, Azure CLI, or Azure PowerShell.

Steps to Create a Linux VM using Azure CLI

This guide demonstrates creating a Linux virtual machine using the Azure Command-Line Interface (CLI).

Prerequisites

  • An Azure account with an active subscription. If you don't have one, create a free account.
  • Install the Azure CLI.

1. Sign in to Azure

Open your terminal or command prompt and sign in to your Azure account:

az login

This command opens a browser window for you to authenticate.

2. Create a Resource Group

A resource group is a logical container into which Azure resources are deployed and managed.

az group create --name MyResourceGroup --location eastus

Replace MyResourceGroup with your desired resource group name and eastus with your preferred Azure region.

3. Create a Virtual Machine

Now, create the VM. This command creates a Ubuntu LTS VM:

az vm create \ --resource-group MyResourceGroup \ --name MyVM \ --image UbuntuLTS \ --admin-username azureuser \ --ssh-key-values ~/.ssh/id_rsa.pub
  • --resource-group: Specifies the resource group.
  • --name: The name of your VM.
  • --image: The OS image to use (e.g., UbuntuLTS, CentOS, RHEL, Debian).
  • --admin-username: The username for the administrator account.
  • --ssh-key-values: The path to your SSH public key file. If you don't have one, you can generate it using ssh-keygen.

Tip: For Windows VMs, you would typically use --image Win2019Datacenter and specify a password using --admin-password instead of SSH keys.

4. Connect to the VM

Once the VM is created, you can connect to it using SSH:

ssh azureuser@

Replace azureuser with your admin username and <VM_PUBLIC_IP_ADDRESS> with the public IP address of your VM. You can retrieve the public IP address using:

az vm show -d -g MyResourceGroup -n MyVM --query publicIps -o tsv

Note: Ensure your SSH client is configured correctly to use your private key.