This Azure tutorial will discuss how to create a virtual machine in Azure.
Table of Contents
How to create Virtual Machine in Azure
We can create a virtual machine (VM) in Azure for free.
Note: For Azure VM creation for free, you can sign up for a 30-day Azure free trial.
Follow the below steps to create a VM in Azure.
Here, we will discuss creating the VM with the new version of Windows Server 2019 Datacenter.
Make sure you have a subscription to Azure before starting. If you don’t have a subscription, create a free Azure account.
Assuming you have an Azure subscription now. Follow the below steps to create a Windows virtual machine.
1. Login to https://portal.azure.com.
2. Now click on the “Virtual machines” option from the left side Menu and then click on the “Create“ link.
Or Go to the “Virtual machines” option from the “Azure services” section.
One more way is to go to the same option from the “Create a resource” link from the left side menu and then select “Compute” and “Virtual machine”.
For the same option, you can also go to the “Create a resource” link from the “Azure services” section and then select “Compute” and “Virtual machine”.
You can see the option “Virtual machine’ like below:
3. Fill in the details required. A few of the options you can keep as it is, but based on your business need, you can change those.
- Subscription: Choose your correct subscription.
- Resource group: You can choose the existing resource group or create a new one by clicking the “Create New” option. It acts like a container that stores the resources related to an Azure solution.
- Virtual machine name: Provide a name for your virtual machine.
- Region: This is where you will create all the resources related to the virtual machine.
- Image: Choose the Windows Server 2019 Datacenter. You can change it based on your business needs.
- Size: The size you want to assign based on your requirements. I have chosen Standard D2s v3 as per my business requirement.
In the Administrator account section, Choose a Username and password that you will use to login to the VM once created.
Select Inbound port: Choose HTTP(80), RDP(3389). This is a very important option.
All other options you can keep as it is. Now click on the “Next:Disks >” button.
Since I do not have a Windows server license, you can select No license.
You can add your desired disk architecture in the next screen (Disks). I kept the default option as it is. You can change it based on your business needs. Click on the Next: Networking button.
Now you can fill in the below details in the Networking screen.
- Virtual Network: Choose a virtual network or create a new one by clicking the “Create New” link.
- Subnet: You can keep the default option as it is.
- Public inbound ports: choose Allow selected ports.
- Select inbound ports: This is a very important option. Choose HTTP (80) or RDP (3389) here. Once you choose this option in the Basics tab, it will automatically show the same option here. Keep the option as it is.
You can keep all the options as they are for the other tabs. Once you fill in all the above details, click the “Review + Create” button. Azure will validate the above details internally and show the message “Validation passed.”
4. Now click on the “Create” button. It will show you “Your deployment is complete”.Now click on “Go to resource.”
5. Congratulations. Now you have created the VM successfully. Click on the Connect button to connect to the VM.
How to create a Virtual Machine in Azure using PowerShell
Above, we discussed how to create an Azure VM using Azure Portal. Let’s see how to create an Azure virtual machine using PowerShell. Let us discuss the Prerequisites before starting.
Prerequisites
- You must have a valid Azure account or Azure Subscription. Suppose you have not created an Azure Account till now. Create a free Azure Account now.
- Make sure to install the Azure PowerShell module on your machine. If you have not installed it, you can install the Azure PowerShell module now.
Below are the steps to create VM in Azure using PowerShell.
Connect To Azure
Open the PowerShell ISE as Run as Administrator mode and execute the below PowerShell cmdlet to connect to Azure using the Connect-AzAccount PowerShell cmdlet.
PS C:\WINDOWS\system32> Connect-AzAccount
Once, you will execute the above PowerShell cmdlet, it will prompt you to Provide your Azure Credentials. Make sure to log in with your Global Administrator credentials.
Creating a new resource group using PowerShell
To create an Azure Virtual machine, we need to create a resource group using PowerShell as a first step. Use the below PowerShell script to create a resource group using PowerShell.
PS C:\WINDOWS\system32> New-AzResourceGroup -Name DemoVM -Location "East US"
We have executed the above PowerShell cmdlet, and you can see that the Resource group has been successfully created without any issues.
DemoVM is our resource group’s name, and the resource group’s region is East US.
Creating a new virtual network using PowerShell
Now, our resource group is ready, and as a next step, we need to create a new virtual network using PowerShell. Use the below PowerShell cmdlet to create a new virtual network using PowerShell.
$vmsnet = New-AzVirtualNetworkSubnetConfig -Name vmsubnet -AddressPrefix “10.0.2.0/24”
New-AzVirtualNetwork -Name TSINFOVN1 -ResourceGroupName DemoVM -Location “East US” -AddressPrefix “10.0.0.0/16” -Subnet $vmsnet
Once we have executed the above PowerShell script. You can able to see that the virtual network has been successfully created without any issue
Name : TSINFOVN1
ResourceGroupName : DemoVM
Location : eastus
Id : /subscriptions/1cdf###-dee5-###-9c9c-feaa72a5cbd1/resourceG
roups/DemoVM/providers/Microsoft.Network/virtualNetworks/TSIN
FOVN1
Etag : W/"8b5818b8-231f-4102-a58c-d4059303c4d6"
ResourceGuid : 16a24415-7405-4a35-9d00-0920e25bd07e
ProvisioningState : Succeeded
Tags :
AddressSpace : {
"AddressPrefixes": [
"10.0.0.0/16"
]
}
DhcpOptions : {}
Subnets : [
{
"Delegations": [],
"Name": "vmsubnet",
"Etag": "W/\"8b5818b8-231f-4102-a58c-d4059303c4d6\"",
"Id": "/subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd
1/resourceGroups/DemoVM/providers/Microsoft.Network/virtualNe
tworks/TSINFOVN1/subnets/vmsubnet",
"AddressPrefix": [
"10.0.2.0/24"
],
"IpConfigurations": [],
"ServiceAssociationLinks": [],
"ResourceNavigationLinks": [],
"ServiceEndpoints": [],
"ServiceEndpointPolicies": [],
"PrivateEndpoints": [],
"ProvisioningState": "Succeeded",
"PrivateEndpointNetworkPolicies": "Enabled",
"PrivateLinkServiceNetworkPolicies": "Enabled",
"IpAllocations": []
}
]
VirtualNetworkPeerings : []
EnableDdosProtection : false
DdosProtectionPlan : null
We have successfully created the resource group and the virtual network so far. Now, it is time to create a new Azure VM.
Creating Azure VM using PowerShell
Use the below PowerShell cmdlet to create an Azure Virtual machine.
$loginDetails = Get-Credential
New-AzVm -ResourceGroupName DemoVM -Name “TSINFOVM” -Location “East US” -VirtualNetworkName “TSINFOVN1” -SubnetName “vmsubnet” -addressprefix 10.0.2.0/24 -PublicIpAddressName “TSINFOIP1” -OpenPorts 3389 -Image win2019datacenter -Size Standard_D2s_v3 -Credential $loginDetails
Once we have executed the above PowerShell script, you can see the Azure Virtual machine has been created successfully without any issue
The output looks like the below
ResourceGroupName : DemoVM
Id : /subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourc
eGroups/DemoVM/providers/Microsoft.Compute/virtualMachines/TSINFOVM
VmId : b90fd30b-cefd-432c-a06f-81598e6dd91d
Name : TSINFOVM
Type : Microsoft.Compute/virtualMachines
Location : eastus
Tags : {}
HardwareProfile : {VmSize}
NetworkProfile : {NetworkInterfaces}
OSProfile : {ComputerName, AdminUsername, WindowsConfiguration,
Secrets, AllowExtensionOperations, RequireGuestProvisionSignal}
ProvisioningState : Succeeded
StorageProfile : {ImageReference, OsDisk, DataDisks}
FullyQualifiedDomainName : tsinfovm-a5a358.East US.cloudapp.azure.com
To ensure that the virtual machine has been created successfully, Let’s log in to the Azure Portal and check that the VM was created successfully. You can see below that the Azure VM was created successfully without any issues.
How To Create A Virtual Machine Using Azure CLI
You can also quickly create an Azure Virtual Machine using Azure CLI using the steps below.
1. Create a Resource Group using the below command in Azure CLI.
az group create --name myRsgGroup --location eastus
Once you execute the above command, you will get the output below
{
"id": "/subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/myRsgGroup",
"location": "eastus",
"managedBy": null,
"name": "myRsgGroup",
"properties": {
"provisioningState": "Succeeded"
},
"tags": null,
"type": "Microsoft.Resources/resourceGroups"
}
2. Now, run the below command to create the VM using Azure CLI.
az vm create --resource-group myRsgGroup --name TSINFOVM2 --image Win2016Datacenter --admin-username rajkishore --admin-password "Password@123" --location eastus
You can see it below
Once You will execute the above command, you will get the output below
{- Finished ..
"fqdns": "",
"id": "/subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/myRsgGroup/providers/Microsoft.Compute/virtualMachines/TSINFOVM2",
"location": "eastus",
"macAddress": "00-0D-3A-18-68-6B",
"powerState": "VM running",
"privateIpAddress": "10.0.0.4",
"publicIpAddress": "13.92.35.98",
"resourceGroup": "myRsgGroup",
"zones": ""
}
Now, if you verify on the Azure Portal, you can see the VM created successfully. Check out the below screenshot that shows the Virtual Machine has been created successfully.
How To Create Linux VM In Azure
We have discussed above how to create a Windows virtual machine in the Azure portal. Now, let’s discuss how to create a Linux Virtual Machine in Azure. Follow the below steps.
1. Follow the step-1 to Step-2 from the above section.
2. On the Create a Virtual Machine page, Provide the below details
- Subscription: Select a valid subscription that you want to use here to create a Linux virtual machine.
- Resource group: Choose the existing resource group or create a new one by clicking the “Create New” option.
- Virtual machine name: You need to provide a name for your virtual machine.
- Region: Select the region or location.
- Image: Choose the Ubuntu Server 20.04 LTS – Gen1 as the image option.
- Size: Select the size based on your business requirements. Click on the see all sizes link to check all the options available.
- Authentication Type: Select the authentication type based on your need.
- Username: Provide a username.
- SSH public key source: Select the default option. Generate a new key pair.
- Key Pair Name: Provide a name for the key pair.
- Public inbound Ports: Select the Allow Selected Ports option.
- Select Inbound Ports: Select the HTTP (80) and SSH (22) options.
Keep the other tab values as they are. Finally, click the Review + Create button.
3. Now, it will validate all the details you provided and show you ” Validation Passed. ” Click on the Create button to create the Azure Linux Virtual Machine.
4. Click on the Download private key and create resource button to download the critical details that you need while connecting to the virtual machine.
5. It will take a few minutes to show you that Your deployment is complete. Click the Go to Resource button to navigate to the Linux Virtual Machine you created.
Create Linux VM Using Azure CLI
Using the steps below, you can easily create a Linux virtual machine using Azure CLI.
1. Run the below command to create a Resource Group using Azure CLI.
az group create --name demoRsgGroup --location eastus
Once you execute the above command. You will get the output below
{
"id": "/subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/demoRsgGroup",
"location": "eastus",
"managedBy": null,
"name": "demoRsgGroup",
"properties": {
"provisioningState": "Succeeded"
},
"tags": null,
"type": "Microsoft.Resources/resourceGroups"
}
2. Now, execute the below command to create the VM using Azure CLI.
az vm create --resource-group demoRsgGroup --name TSINFOVM --image UbuntuLTS --admin-username azureuser --generate-ssh-keys
You can see it below
Once You will execute the above command, you will get the output below
{- Finished ..
"fqdns": "",
"id": "/subscriptions/1cdf4300-dee5-4518-9c9c-feaa72a5cbd1/resourceGroups/demoRsgGroup/providers/Microsoft.Compute/virtualMachines/TSINFOVM",
"location": "eastus",
"macAddress": "00-0D-3A-57-3D-13",
"powerState": "VM running",
"privateIpAddress": "10.0.0.4",
"publicIpAddress": "52.179.10.61",
"resourceGroup": "demoRsgGroup",
"zones": ""
}
Now, to verify, you can navigate to the Azure Portal and see the below Azure Linux VM created successfully. Check out the screenshot below.
You can also use the Az VM create command for creating virtual machine in Azure.
Create Azure VM (Virtual Machine) – Video Tutorial
You may like the following Azure tutorials:
- How To Create Azure Windows Virtual Machine From A Snapshot PowerShell
- Azure Virtual Machine Tutorial
- How to access Azure VM (Virtual Machine)
- Azure virtual machine auto-shutdown
Conclusion
In this Azure tutorial, we have discussed how to create a VM in Azure using Azure Portal, PowerShell, Azure CLI, etc. I hope you have enjoyed this article !!!
I am Bijay, a Microsoft MVP (10 times) having more than 17 years of experience in the software industry. During my IT career, I got a chance to share my expertise in SharePoint and Microsoft Azure, like Azure VM, Azure Active Directory, Azure PowerShell, etc. I hope you will learn from these Azure tutorials. Read more