Microsoft Docs

Create a Virtual Machine on Azure

This guide provides step-by-step instructions for creating a virtual machine (VM) in Azure using the Azure portal, Azure CLI, and Azure PowerShell. Virtual machines are a fundamental building block for many cloud solutions, offering flexible compute resources.

Prerequisites

Method 1: Creating a VM using the Azure Portal

The Azure portal offers a user-friendly graphical interface for creating and managing Azure resources.

1

Sign in to the Azure portal.

2

In the portal, search for "Virtual machines" and select it from the services list.

3

On the Virtual machines page, select Create > Azure virtual machine.

4

On the Basics tab:

  • Subscription: Choose your Azure subscription.
  • Resource group: Select an existing resource group or create a new one.
  • Virtual machine name: Enter a unique name for your VM.
  • Region: Select the Azure region where you want to deploy your VM.
  • Availability options: Configure availability zones or sets if needed.
  • Image: Select an operating system image (e.g., Windows Server, Ubuntu Server).
  • Size: Choose a VM size that meets your performance and cost requirements.
  • Administrator account: Set up authentication (SSH public key or password).
  • Inbound port rules: Configure network access for specific ports (e.g., RDP (3389) for Windows, SSH (22) for Linux).
5

Configure other tabs as needed: Disks, Networking, Management, Advanced, and Tags.

6

Review your settings and select Create.

Method 2: Creating a VM using Azure CLI

The Azure Command-Line Interface (CLI) allows you to manage Azure resources from your terminal.

  1. Install the Azure CLI if you haven't already.
  2. Sign in to Azure:
    az login
  3. Create a resource group (if you don't have one):
    az group create --name MyResourceGroup --location eastus
  4. Create a virtual machine:
    az vm create \
          --resource-group MyResourceGroup \
          --name MyVM \
          --image Ubuntu2204 \
          --admin-username azureuser \
          --generate-ssh-keys

    This example creates a Linux VM with Ubuntu 22.04 and uses SSH key authentication. For Windows, you would specify a different image and use password authentication:

    az vm create \
          --resource-group MyResourceGroup \
          --name MyWinVM \
          --image Win2019Datacenter \
          --admin-username azureuser \
          --admin-password "YourComplexPassword123!"

Method 3: Creating a VM using Azure PowerShell

Azure PowerShell provides cmdlets for managing Azure resources.

  1. Install Azure PowerShell if you haven't already.
  2. Connect to your Azure account:
    Connect-AzAccount
  3. Create a resource group (if you don't have one):
    New-AzResourceGroup -Name "MyResourceGroup" -Location "EastUS"
  4. Create a virtual machine:
    New-AzVm `
            -ResourceGroupName "MyResourceGroup" `
            -Name "MyVM" `
            -Location "EastUS" `
            -Image UbuntuLts `
            -VirtualNetworkType New `
            -SubnetName "MySubnet" `
            -AddressPrefix "10.0.0.0/16" `
            -PublicIpAddressType Static `
            -PublicIpAddressSku Standard `
            -Credential (Get-Credential)

    This command prompts for administrator credentials and creates a Linux VM. For Windows, you would use a different image name and ensure RDP is allowed.

Security Best Practices:

  • Always use strong passwords or SSH keys for authentication.
  • Restrict inbound port access to only necessary ports.
  • Consider using Azure Security Center for enhanced security monitoring and recommendations.

After creation, you can connect to your VM using SSH (for Linux) or RDP (for Windows) to begin running your applications.

Next: Manage Your VM