0
0
Azurecloud~5 mins

VM commands (create, start, stop) in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Creating and managing virtual machines (VMs) lets you run computers in the cloud. You can start and stop these VMs as needed to save money and control your resources.
When you want to run a Windows or Linux server in the cloud for your app.
When you need to test software on a clean machine without affecting your computer.
When you want to save costs by stopping a VM when it's not in use.
When you want to restart a VM to apply updates or fix issues.
When you want to create multiple VMs for a project or team quickly.
Commands
This command creates a new Ubuntu virtual machine named 'example-vm' in the resource group 'example-rg'. It also sets up SSH keys for secure login.
Terminal
az vm create --resource-group example-rg --name example-vm --image UbuntuLTS --admin-username azureuser --generate-ssh-keys
Expected OutputExpected
{ "fqdns": "", "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/example-rg/providers/Microsoft.Compute/virtualMachines/example-vm", "location": "eastus", "name": "example-vm", "powerState": "VM running", "resourceGroup": "example-rg", "zones": "" }
--resource-group - Specifies the group where the VM will be created
--name - Sets the name of the VM
--image - Chooses the operating system image for the VM
This command starts the VM named 'example-vm' in the 'example-rg' resource group if it is stopped.
Terminal
az vm start --resource-group example-rg --name example-vm
Expected OutputExpected
No output (command runs silently)
--resource-group - Specifies the resource group of the VM
--name - Specifies the VM to start
This command stops the VM named 'example-vm' in the 'example-rg' resource group to save costs when not in use.
Terminal
az vm stop --resource-group example-rg --name example-vm
Expected OutputExpected
No output (command runs silently)
--resource-group - Specifies the resource group of the VM
--name - Specifies the VM to stop
This command checks the current power state of the VM to confirm if it is running or stopped.
Terminal
az vm show --resource-group example-rg --name example-vm --query instanceView.statuses[1].displayStatus -o tsv
Expected OutputExpected
VM stopped
--query - Filters output to show only the power state
-o - Formats output as plain text
Key Concept

If you remember nothing else from this pattern, remember: creating a VM sets it up, starting powers it on, and stopping powers it off to save costs.

Common Mistakes
Trying to start or stop a VM without specifying the correct resource group.
Azure needs the resource group to find the VM; without it, the command fails.
Always include the --resource-group flag with the correct group name.
Not generating or providing SSH keys when creating a Linux VM.
Without SSH keys, you cannot securely connect to the VM.
Use --generate-ssh-keys or provide your own keys during VM creation.
Assuming stopping a VM deletes it or releases all resources.
Stopping a VM only powers it off; you still pay for storage and reserved resources.
Understand stopping saves compute costs but not storage; delete VM to remove all resources.
Summary
Use 'az vm create' to make a new virtual machine with a chosen OS and login setup.
Use 'az vm start' and 'az vm stop' to control when the VM is running or powered off.
Check VM status with 'az vm show' to confirm its current power state.