Quickstart: Use the Azure CLI to create a Linux Virtual Machine - Azure Virtual Machines (2024)

  • Article

Applies to: ✔️ Linux VMs

Quickstart: Use the Azure CLI to create a Linux Virtual Machine - Azure Virtual Machines (1)

This quickstart shows you how to use the Azure CLI to deploy a Linux virtual machine (VM) in Azure. The Azure CLI is used to create and manage Azure resources via either the command line or scripts.

If you don't have an Azure subscription, create a free account before you begin.

Launch Azure Cloud Shell

The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. It has common Azure tools preinstalled and configured to use with your account.

To open the Cloud Shell, just select Try it from the upper right corner of a code block. You can also open Cloud Shell in a separate browser tab by going to https://shell.azure.com/bash. Select Copy to copy the blocks of code, paste it into the Cloud Shell, and select Enter to run it.

If you prefer to install and use the CLI locally, this quickstart requires Azure CLI version 2.0.30 or later. Run az --version to find the version. If you need to install or upgrade, see Install Azure CLI.

Log in to Azure using the CLI

In order to run commands in Azure using the CLI, you need to log in first. Log in using the az login command.

Create a resource group

A resource group is a container for related resources. All resources must be placed in a resource group. The az group create command creates a resource group with the previously defined $MY_RESOURCE_GROUP_NAME and $REGION parameters.

export RANDOM_ID="$(openssl rand -hex 3)"export MY_RESOURCE_GROUP_NAME="myVMResourceGroup$RANDOM_ID"export REGION=EastUSaz group create --name $MY_RESOURCE_GROUP_NAME --location $REGION

Results:

{ "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myVMResourceGroup", "location": "eastus", "managedBy": null, "name": "myVMResourceGroup", "properties": { "provisioningState": "Succeeded" }, "tags": null, "type": "Microsoft.Resources/resourceGroups"}

Create the virtual machine

To create a VM in this resource group, use the vm create command.

The following example creates a VM and adds a user account. The --generate-ssh-keys parameter causes the CLI to look for an available ssh key in ~/.ssh. If one is found, that key is used. If not, one is generated and stored in ~/.ssh. The --public-ip-sku Standard parameter ensures that the machine is accessible via a public IP address. Finally, we deploy the latest Ubuntu 22.04 image.

All other values are configured using environment variables.

export MY_VM_NAME="myVM$RANDOM_ID"export MY_USERNAME=azureuserexport MY_VM_IMAGE="Canonical:0001-com-ubuntu-minimal-jammy:minimal-22_04-lts-gen2:latest"az vm create \ --resource-group $MY_RESOURCE_GROUP_NAME \ --name $MY_VM_NAME \ --image $MY_VM_IMAGE \ --admin-username $MY_USERNAME \ --assign-identity \ --generate-ssh-keys \ --public-ip-sku Standard

It takes a few minutes to create the VM and supporting resources. The following example output shows the VM create operation was successful.

Results:

{ "fqdns": "", "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM", "location": "eastus", "macAddress": "00-0D-3A-10-4F-70", "powerState": "VM running", "privateIpAddress": "10.0.0.4", "publicIpAddress": "52.147.208.85", "resourceGroup": "myVMResourceGroup", "zones": ""}

Enable Azure AD Login for a Linux virtual machine in Azure

The following code example deploys a Linux VM and then installs the extension to enable an Azure AD Login for a Linux VM. VM extensions are small applications that provide post-deployment configuration and automation tasks on Azure virtual machines.

az vm extension set \ --publisher Microsoft.Azure.ActiveDirectory \ --name AADSSHLoginForLinux \ --resource-group $MY_RESOURCE_GROUP_NAME \ --vm-name $MY_VM_NAME

Store IP address of VM in order to SSH

Run the following command to store the IP address of the VM as an environment variable:

export IP_ADDRESS=$(az vm show --show-details --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_VM_NAME --query publicIps --output tsv)

SSH into the VM

You can now SSH into the VM by running the output of the following command in your ssh client of choice:

ssh -o StrictHostKeyChecking=no $MY_USERNAME@$IP_ADDRESS

Next Steps

Quickstart: Use the Azure CLI to create a Linux Virtual Machine - Azure Virtual Machines (2024)
Top Articles
Healing From Emotional Abuse | Charlie Health
Dutch National Holidays and traditions
Sdn Md 2023-2024
Valley Fair Tickets Costco
Routing Number 041203824
Roblox Character Added
Housing Intranet Unt
Miami Valley Hospital Central Scheduling
ATV Blue Book - Values & Used Prices
Oxford House Peoria Il
Marion County Wv Tax Maps
VMware’s Partner Connect Program: an evolution of opportunities
Grab this ice cream maker while it's discounted in Walmart's sale | Digital Trends
Bx11
Craigslist Free Stuff Greensboro Nc
Self-Service ATMs: Accessibility, Limits, & Features
Village
Speedstepper
Pixel Combat Unblocked
Dexter Gomovies
Craigslist Brandon Vt
They Cloned Tyrone Showtimes Near Showbiz Cinemas - Kingwood
Miles City Montana Craigslist
Greater Orangeburg
Current Time In Maryland
Max 80 Orl
Human Unitec International Inc (HMNU) Stock Price History Chart & Technical Analysis Graph - TipRanks.com
Cars And Trucks Facebook
Ixl Lausd Northwest
Bee And Willow Bar Cart
Helloid Worthington Login
Back to the Future Part III | Rotten Tomatoes
Omnistorm Necro Diablo 4
Bimar Produkte Test & Vergleich 09/2024 » GUT bis SEHR GUT
Body Surface Area (BSA) Calculator
Pp503063
Deshuesadero El Pulpo
R/Moissanite
Jack In The Box Menu 2022
Anhedönia Last Name Origin
More News, Rumors and Opinions Tuesday PM 7-9-2024 — Dinar Recaps
Puretalkusa.com/Amac
Samantha Lyne Wikipedia
Gravel Racing
Unitedhealthcare Community Plan Eye Doctors
Yakini Q Sj Photos
Doe mee met ons loyaliteitsprogramma | Victoria Club
Rovert Wrestling
Dolce Luna Italian Restaurant & Pizzeria
Houston Primary Care Byron Ga
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 5944

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.