Quickstart: Create a Linux virtual machine with Azure PowerShell
This quickstart guide walks you through the steps to create a Linux virtual machine (VM) in Azure using Azure PowerShell.
Prerequisites
Before you begin, ensure you have the following:
- An Azure account. If you don't have one, create a free account before you begin.
- The Azure PowerShell module installed. If you need to install or upgrade it, see Install the Azure PowerShell module.
- Connect to Azure. Run
Connect-AzAccount
and follow the prompts to authenticate.
Sign in to Azure
Before you can work with Azure resources, you need to sign in to your Azure account using the Connect-AzAccount cmdlet:
Connect-AzAccount
The cmdlet opens a browser window for you to enter your Azure account credentials.
Create a resource group
A resource group is a logical container into which Azure resources are deployed and managed. Use the New-AzResourceGroup
cmdlet to create a resource group.
Create a resource group named myResourceGroup in the EastUS location.
PowerShellNew-AzResourceGroup -Name "myResourceGroup" -Location "EastUS"
Create a virtual machine
Use the New-AzVM
cmdlet to create a VM. This cmdlet simplifies the process by creating the necessary associated resources like the virtual network, public IP address, and network interface.
Create a VM named myVM with the Standard_DS1_v2 size, using UbuntuLTS as the OS image, and generating new SSH keys for authentication.
PowerShellNew-AzVm -ResourceGroupName "myResourceGroup" -Name "myVM" -Location "EastUS" -VirtualNetworkType "New" -SubnetId (Get-AzVirtualNetwork -ResourceGroupName "myResourceGroup" -Name "myVM").Subnets[0].Id -ImageName UbuntuLTS -Size Standard_DS1_v2 -OpenPorts 22
This command will prompt you for a username and SSH public key path (or create them if they don't exist).
The -OpenPorts 22
parameter opens port 22 to allow SSH access to the VM. For production environments, it's recommended to restrict access to only necessary ports and IP addresses.
View the virtual machine
You can view the details of your VM using the Get-AzVM
cmdlet:
Get-AzVM -ResourceGroupName "myResourceGroup" -Name "myVM" -Status
The output will include the VM's provisioning state and its public IP address, which you'll need to connect to the VM.
Connect to the virtual machine
Once the VM is created and the SSH port is open, you can connect to it using an SSH client. Replace
with the public IP address of your VM.
ssh <your-username>@<PUBLIC_IP_ADDRESS>
You will be prompted to accept the SSH key fingerprint if this is your first time connecting.
Clean up resources
When you no longer need the virtual machine and related resources, you can delete the resource group using the Remove-AzResourceGroup
cmdlet.
Remove-AzResourceGroup -Name "myResourceGroup"
This command deletes the resource group and all the resources contained within it.