Quickstart: Create a Linux virtual machine in Azure with PowerShell
The Azure PowerShell module is used to create and manage Azure resources from the PowerShell command line or in scripts. This quickstart shows you how to use the Azure PowerShell module to deploy a Linux virtual machine (VM) in Azure. This quickstart uses the Ubuntu 18.04 LTS marketplace image from Canonical. To see your VM in action, you'll also SSH to the VM and install the NGINX web server.
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. Select Copy to copy the blocks of code, paste it into the Cloud Shell, and press enter to run it.
Create SSH key pair
Use ssh-keygen to create an SSH key pair. If you already have an SSH key pair, you can skip this step.
ssh-keygen -t rsa -b 4096
You will be prompted to provide a filename for the key pair or you can hit Enter to use the default location of
/home/<username>/.ssh/id_rsa. You will also be able to create a password for the keys, if you like.
For more detailed information on how to create SSH key pairs, see How to use SSH keys with Windows.
If you create your SSH key pair using the Cloud Shell, it will be stored in a storage account that is automatically created by Cloud Shell. Don't delete the storage account, or the files share in it, until after you have retrieved your keys or you will lose access to the VM.
Create a resource group
Create an Azure resource group with New-AzResourceGroup. A resource group is a logical container into which Azure resources are deployed and managed:
New-AzResourceGroup -Name "myResourceGroup" -Location "EastUS"
Create virtual network resources
Create a virtual network, subnet, and a public IP address. These resources are used to provide network connectivity to the VM and connect it to the internet:
# Create a subnet configuration $subnetConfig = New-AzVirtualNetworkSubnetConfig ` -Name "mySubnet" ` -AddressPrefix 192.168.1.0/24 # Create a virtual network $vnet = New-AzVirtualNetwork ` -ResourceGroupName "myResourceGroup" ` -Location "EastUS" ` -Name "myVNET" ` -AddressPrefix 192.168.0.0/16 ` -Subnet $subnetConfig # Create a public IP address and specify a DNS name $pip = New-AzPublicIpAddress ` -ResourceGroupName "myResourceGroup" ` -Location "EastUS" ` -AllocationMethod Static ` -IdleTimeoutInMinutes 4 ` -Name "mypublicdns$(Get-Random)"
Create an Azure Network Security Group and traffic rule. The Network Security Group secures the VM with inbound and outbound rules. In the following example, an inbound rule is created for TCP port 22 that allows SSH connections. To allow incoming web traffic, an inbound rule for TCP port 80 is also created.
# Create an inbound network security group rule for port 22 $nsgRuleSSH = New-AzNetworkSecurityRuleConfig ` -Name "myNetworkSecurityGroupRuleSSH" ` -Protocol "Tcp" ` -Direction "Inbound" ` -Priority 1000 ` -SourceAddressPrefix * ` -SourcePortRange * ` -DestinationAddressPrefix * ` -DestinationPortRange 22 ` -Access "Allow" # Create an inbound network security group rule for port 80 $nsgRuleWeb = New-AzNetworkSecurityRuleConfig ` -Name "myNetworkSecurityGroupRuleWWW" ` -Protocol "Tcp" ` -Direction "Inbound" ` -Priority 1001 ` -SourceAddressPrefix * ` -SourcePortRange * ` -DestinationAddressPrefix * ` -DestinationPortRange 80 ` -Access "Allow" # Create a network security group $nsg = New-AzNetworkSecurityGroup ` -ResourceGroupName "myResourceGroup" ` -Location "EastUS" ` -Name "myNetworkSecurityGroup" ` -SecurityRules $nsgRuleSSH,$nsgRuleWeb
Create a virtual network interface card (NIC) with New-AzNetworkInterface. The virtual NIC connects the VM to a subnet, Network Security Group, and public IP address.
# Create a virtual network card and associate with public IP address and NSG $nic = New-AzNetworkInterface ` -Name "myNic" ` -ResourceGroupName "myResourceGroup" ` -Location "EastUS" ` -SubnetId $vnet.Subnets.Id ` -PublicIpAddressId $pip.Id ` -NetworkSecurityGroupId $nsg.Id
Create a virtual machine
To create a VM in PowerShell, you create a configuration that has settings like the image to use, size, and authentication options. Then the configuration is used to build the VM.
Define the SSH credentials, OS information, and VM size. In this example, the SSH key is stored in
# Define a credential object $securePassword = ConvertTo-SecureString ' ' -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential ("azureuser", $securePassword) # Create a virtual machine configuration $vmConfig = New-AzVMConfig ` -VMName "myVM" ` -VMSize "Standard_D1" | ` Set-AzVMOperatingSystem ` -Linux ` -ComputerName "myVM" ` -Credential $cred ` -DisablePasswordAuthentication | ` Set-AzVMSourceImage ` -PublisherName "Canonical" ` -Offer "UbuntuServer" ` -Skus "18.04-LTS" ` -Version "latest" | ` Add-AzVMNetworkInterface ` -Id $nic.Id # Configure the SSH key $sshPublicKey = cat ~/.ssh/id_rsa.pub Add-AzVMSshPublicKey ` -VM $vmconfig ` -KeyData $sshPublicKey ` -Path "/home/azureuser/.ssh/authorized_keys"
Now, combine the previous configuration definitions to create with New-AzVM:
New-AzVM ` -ResourceGroupName "myResourceGroup" ` -Location eastus -VM $vmConfig
It will take a few minutes for your VM to be deployed. When the deployment is finished, move on to the next section.
Azure provides an ephemeral IP for Azure Virtual Machines which aren't assigned a public IP address, or are in the backend pool of an internal Basic Azure Load Balancer. The ephemeral IP mechanism provides an outbound IP address that isn't configurable.
The ephemeral IP is disabled when a public IP address is assigned to the virtual machine or the virtual machine is placed in the backend pool of a Standard Load Balancer with or without outbound rules. If a Azure Virtual Network NAT gateway resource is assigned to the subnet of the virtual machine, the ephemeral IP is disabled.
For more information on outbound connections in Azure, see Using Source Network Address Translation (SNAT) for outbound connections.
Connect to the VM
Create an SSH connection with the VM using the public IP address. To see the public IP address of the VM, use the Get-AzPublicIpAddress cmdlet:
Get-AzPublicIpAddress -ResourceGroupName "myResourceGroup" | Select "IpAddress"
Using the same shell you used to create your SSH key pair, paste the the following command into the shell to create an SSH session. Replace 10.111.12.123 with the IP address of your VM.
When prompted, the login user name is azureuser. If a passphrase is used with your SSH keys, you need to enter that when prompted.
To see your VM in action, install the NGINX web server. From your SSH session, update your package sources and then install the latest NGINX package.
sudo apt-get -y update sudo apt-get -y install nginx
When done, type
exit to leave the SSH session.
View the web server in action
Use a web browser of your choice to view the default NGINX welcome page. Enter the public IP address of the VM as the web address. The public IP address can be found on the VM overview page or as part of the SSH connection string you used earlier.
Clean up resources
When no longer needed, you can use the Remove-AzResourceGroup cmdlet to remove the resource group, VM, and all related resources:
Remove-AzResourceGroup -Name "myResourceGroup"
In this quickstart, you deployed a simple virtual machine, created a Network Security Group and rule, and installed a basic web server. To learn more about Azure virtual machines, continue to the tutorial for Linux VMs.