Prerequisites
- Azure subscription
- PowerShell 7+ installed locally
- Az PowerShell module (
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
)
- SSH key pair (run
ssh-keygen -t rsa -b 4096
)
Step 1 – Sign in to Azure
Connect-AzAccount
Step 2 – Create a Resource Group
$rgName = "RG-LinuxVM"
$location = "EastUS"
New-AzResourceGroup -Name $rgName -Location $location
Step 3 – Define VM Parameters
$vmName = "MyLinuxVM"
$vmSize = "Standard_B2s"
$adminUser = "azureuser"
$sshPublicKey = Get-Content "$HOME/.ssh/id_rsa.pub"
Step 4 – Create the Linux VM
New-AzVM `
-ResourceGroupName $rgName `
-Name $vmName `
-Location $location `
-Image "Canonical:UbuntuServer:18.04-LTS:latest" `
-Size $vmSize `
-Credential (New-Object System.Management.Automation.PSCredential($adminUser,(ConvertTo-SecureString "PlaceholderPassword" -AsPlainText -Force))) `
-LinuxConfiguration @{ DisablePasswordAuthentication = $true; Ssh = @{ PublicKeys = @(@{ Path = "/home/$adminUser/.ssh/authorized_keys"; KeyData = $sshPublicKey }) } } `
-Tag @{ Project="Demo" }
Step 5 – Verify Deployment
Get-AzVM -ResourceGroupName $rgName -Name $vmName | Format-Table Name,Location,ProvisioningState
Step 6 – Connect via SSH
ssh $adminUser@$(az vm show -g $rgName -n $vmName --show-details --query publicIps -o tsv)