This quickstart guide walks you through the process of creating a Linux virtual machine (VM) in Azure using the Azure CLI. The Azure CLI provides a powerful set of commands for managing Azure resources from your command line.
Prerequisites
Before you begin, ensure you have the following:
- An Azure account. If you don't have one, create a free account.
- Azure CLI installed. If you don't have it installed, follow the installation guide.
Sign in to Azure
Open your terminal or command prompt and sign in to your Azure account:
az login
This command will open a browser window for you to authenticate with your Azure credentials.
Create a Resource Group
A resource group is a logical container into which Azure resources are deployed and managed. Create a resource group using the az group create command. Replace myResourceGroup with a name for your resource group and eastus with your desired Azure region.
az group create \
--name myResourceGroup \
--location eastus
Create a Virtual Machine
Now, create a virtual machine using the az vm create command. This command will create a Linux VM, a virtual network, a public IP address, and other required resources.
Replace the placeholder values as needed. For example, myVM for the VM name, myResourceGroup for the resource group, UbuntuLTS for the image, and azureuser for the username.
az vm create \
--resource-group myResourceGroup \
--name myVM \
--image UbuntuLTS \
--admin-username azureuser \
--generate-ssh-keys
This command will output JSON containing information about the created VM. Note the publicIpAddress.
Connect to the Virtual Machine
Once the VM is created, you can connect to it using SSH. Use the publicIpAddress from the previous step and your admin username.
ssh azureuser@<PUBLIC_IP_ADDRESS>
If you chose to use a password instead of SSH keys (not recommended for production), you would be prompted for your password.
Clean up Resources
When you are finished with the virtual machine, you can delete the resource group and all its associated resources to avoid ongoing charges.
az group delete --name myResourceGroup
To confirm the deletion, you will be prompted. Type y to proceed.
Important: Deleting a resource group deletes all resources within it. Ensure you have backed up any data you wish to keep.
Next Steps
You have successfully created and connected to a Linux virtual machine in Azure. Here are some next steps you might consider:
Configure network security groups to control inbound and outbound traffic.
Attach data disks to your VM for additional storage.
Deploy applications to your Azure VM.