You are currently viewing Azure: Creating your first Linux VM

Azure: Creating your first Linux VM

This article is focuses on how to use Azure CLI to provision a Linux virtual machine on Azure.

You would need Azure account in order to execute these script. If you don’t have one, you can create a free Azure account here.

You can install the Azure CLI tools from this link depending on which operating system you are on.

If you are not planning for any Azure certification, I would strongly recommend to at least go for AZ-203 certification which is about knowing Azure products from developer perspective.

Please refer my blog article below and it should help you get prepared for the exam.

Plan your AZ-203 certification today !

Let’s Get Started for creating a Linux VM on Azure.

Few concepts before we start…

Let’s have a look at few concepts before we start.

Resource Group – All Azure resources are organized in Resource Groups. We are going to create new resource i.e. Virtual Machine inside resource group of this name.

Location – The Azure region in which we want to place the new resource

VM Name – to specify the name of virtual machine

Let’s setup few variables to help us in VM creation:


# Update for your admin username and password
AdminUsername=azureuser

# Update information about resource group, location and machine name
ResourceGroupName=MyResourceGroup
MachineLocation=westeurope
MachineName=MyUbuntuVM

One more thing, SSH – what is it ?

You can select either to login using password or using SSH.

Password authentication is same as normal login to our laptops, you have to provide username and password and if the combination matches, then you are allowed to access the VM.

SSH is alternative method and considered to be more secure than the password method. This is based on asymmetric encryption technique. There will be a pair of keys a public key and a private key involved in the authentication process.

The public key would be available on the VM and the machine which is trying to connect with VM will have a private key.

In order to understand how the SSH and authentication works, there are a lot of resources available on internet. You can refer this article if you want to read more about this.

If you are creating VM using Azure Portal, then you will have to create SSH pair beforehand, and then will have to supply that value in the SSH key. Refer this article on MSDN to get more details on how to use Azure portal to create Linux VM.

Login to Azure

Below command would help to login to your Azure account. Once this command is executed, it would open a popup where you can enter your email id and password for logging into Azure.

# Login to Azure 
Az Login

Create Resource Group

This statement would create the resource group. The virtual machine would be placed inside this resource group.

If this resource group is deleted, all resources inside this resource groups would be deleted.

# Create a resource group
az group create \
        --name $ResourceGroupName \
        --location $MachineLocation

Create Virtual Network

The virtual network allows virtual machines to securely communicate with one another.

# Create a virtual network
az network vnet create \
       --resource-group $ResourceGroupName \
       --name myVnet \
       --subnet-name mySubnet

Create public IP

This command to reserve a static public IP address. Even if you boot multiple times, the static IP does not change. This IP can be used to connect with virtual machine.

# Create a public IP address
az network public-ip create \
            --resource-group $ResourceGroupName \
            --name myPublicIP

Create Network Security Group and NIC

The Network Security Group has rules which either allow or deny the traffic inbound to virtual network or outbound from virtual network.


# Create a network security group.
az network nsg create \
           --resource-group $ResourceGroupName \
           --name myNetworkSecurityGroup

# Create a virtual network card 
# and associate with public IP address and NSG.
az network nic create \
  --resource-group $ResourceGroupName \
  --name myNic \
  --vnet-name myVnet \
  --subnet mySubnet \
  --network-security-group myNetworkSecurityGroup \
  --public-ip-address myPublicIP

Create Virtual Machine

This command uses all the configurations done till now and creates the virtual machine.

You will have to wait around 5 minutes of time to get the virtual machine created. You can run it as a job by adding —

# Create a virtual machine. 
az vm create \
    --resource-group $ResourceGroupName \
    --name $MachineName \
    --location $MachineLocation \
    --nics myNic \
    --image UbuntuLTS \
    --admin-username $AdminUsername \
    --generate-ssh-keys

Last step for SSH Traffic

This script would open port 22 for SSH traffic.

# Open port 22 to allow SSh traffic to host.
az vm open-port --port 22 \
                --resource-group $ResourceGroupName \
                --name $MachineName 

Connect to VM using SSH…

Use below command to connect to your VM. Replace username and IP address with the admin user name you have supplied in create VM command and the Public IP address of the VM.


ssh azureuser@publicIpAddress

Once you are connected to VM, you can try installing a web server and try accessing that using the public IP URL.

Something to Remember…

If you have created this VM only for learning purpose, please note that there is cost associated with every running virtual machine. So, please do not forget to delete your newly created resource group and all its contents by running below Azure CLI command.


#Removes all resources within this resource group
az group delete --name $ResourceGroupName --yes

I hope you enjoyed this article. Please do not forget to comment and let me know if this information helped you.

Leave a Reply