Wednesday, December 05, 2018

Create Ubuntu Linux Virtual Machine in the Azure Cloud using Azure PowerShell



Create Ubuntu Linux Virtual Machine in the Azure Cloud using Azure PowerShell


Microsoft Azure and Ubuntu Linux VM


We are aware, an Azure Virtual Machine (VM) is an on-demand, high-scale, secure computing resource deployed on Azure through different methods, gives more control over the computing environment. Along with the Ubuntu Server is a part of the larger set of Ubuntu products and operating system developed by Canonical and open source programmers around the world.

You can visit one of my previous post where we have been gone through the setup an Ubuntu Linux virtual machine in the Azure Cloud using Azure Portal

Now here we will talk about the same setup using Azure PowerShell. PowerShell script is more involved as it controls pretty much every characteristic of VM creation. 

If you need a bit more details about the Azure PowerShell, then visit one of my precise post about the Azure PowerShell introduction and its installation as well some hands-on activity

Pre-requisites


Before moving ahead, we need some pre-requisites to setup an Ubuntu Linux VM on top of Azure Cloud using Azure PowerShell.
  1. Azure PowerShell
  2. Azure subscription, if you don't have an account then sign up for a free Azure account - https://azure.microsoft.com/en-gb/free/
  3. SSH key pair, we will generate it on PowerShell and utilized there.

STEP – 1: Connect to Azure Account


I trust you already installed the Azure PowerShell with required version using Windows PowerShell, but still not ready, then you can visit my previous post for the same.

Execute following command to connect with Azure, it will open an interactive dialog for sign-in, sign in with your Azure credentials subsequently.

Connect-AzureRmAccount


Connect-AzureRmAccount

Connect to Azure account


Post submission of credentials, it will be connected and display your account details such as –

Connected to Azure account

STEP – 2: Create Resource Group


A resource group is a logical container where you can deploy and manage Azure Stack resources under one umbrella. Execute the following command to create a new resource group with specified location.

New-AzureRmResourceGroup -Name 'cloudResourceGroup' -Location 'eastus2'

Create resource group

STEP – 3: Create common Variables


Post creation of the resource group, create a couple of variables to store some common values like – 
  • Name of resource group
  • Name of location
  • Name of Ubuntu Linux VM

$resourceGroup = "cloudResourceGroup"

$location = "eastus2"

$vmName = "linuxCloudVM"

Create common variables

STEP – 4: Setup Credentials


Next, need to setup the credential details, execute following command to set user name as well password.

$securePassword = ConvertTo-SecureString 'clouduser@1234' -AsPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential ("clouduser", $securePassword)

Setup credentials

STEP – 5: Configure the Subnet details


We know that a subnetwork or subnet is a logical subdivision of an IP network, execute following command to configure the subnet details.

$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name cloudLinuxSubnet -AddressPrefix 192.168.2.0/24

Configure the subnet details

STEP – 6: Setup Virtual Network (VNet)


An Azure Virtual Network (VNet) is an interpretation of your own network in the cloud, something a logical separation of the Azure cloud dedicated to your subscription only. Execute following command to setup the VNet using previously created variables.

$vnet = New-AzureRmVirtualNetwork -ResourceGroupName $resourceGroup -Location $location -Name linuxCloudVNet -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig

Setup virtual network

NOTE: Ignore the breaking change warning which is something minimum PowerShell version required bumped to 5.0.

STEP – 7: Setup Public IP (random assignment)


Later on doing setup the Public IP address, a Public IP addresses allow Internet resources to communicate inbound to Azure resources as well enable Azure resources to communicate outbound to.

Execute following command to assign the random Public IP and verify the same.

$publicIp = New-AzureRmPublicIpAddress -ResourceGroupName $resourceGroup -Location $location -Name "linuxcloudpublicdns$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 4

Setup Public IP (random)

STEP – 8: Configure ACL Rule and NSG group


In fact, the Virtual Network (VNET) is the foundation of the Azure networking model and provides separation and protection, but the Network Security Group (NSG) is the main tool you need to use to enforce and control network traffic rules.

NSG contains a list of Access Control List (ACL) rules that allow or deny network traffic to your VM instances in a Virtual Network.

Since we will access the said Linux VM through SSH key so need to configure the network contact for the port 22 accordingly, execute the following commands for the same.

$nsgRuleSSH = New-AzureRmNetworkSecurityRuleConfig -Name linuxCloudNetworkSecurityGroupRuleSSH  -Protocol Tcp -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 22 -Access Allow

$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $resourceGroup -Location $location -Name linuxCloudNetworkSecurityGroup -SecurityRules $nsgRuleSSH

Configure ACL rule

Configure NSG group

STEP – 9: Setup Network Interface (NIC)


Next, configure the network interface card (NIC), a network interface enables an Azure Virtual Machine to communicate with the internet, Azure, and on-premises resources.  

Execute following command to setup a network interface and associate with previously created Public IP and NSG rule.

$nic = New-AzureRmNetworkInterface -Name linuxCloudNic -ResourceGroupName $resourceGroup -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIp.Id -NetworkSecurityGroupId $nsg.Id

Setup network interface

STEP – 10: Configure Ubuntu Server


Now, almost all initial configuration has been done, time to configure the Ubuntu Linux virtual machine. This configuration includes the settings used when deploying the virtual machine. For example: user credentials, size, and the virtual machine image.

Execute the following command to configure Ubuntu 16.04-LTS server provided by the Canonical.

$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize "Standard_D1" | 
Set-AzureRmVMOperatingSystem -Linux -ComputerName $vmName -Credential $credential -DisablePasswordAuthentication | 
Set-AzureRmVMSourceImage -PublisherName "Canonical" -Offer "UbuntuServer" -Skus "16.04-LTS" -Version "latest" | 
Add-AzureRmVMNetworkInterface -Id $nic.Id

Setup network interface

STEP – 11: SSH Key generation and assignment


Here an SSH key pair will be used during setup the Ubuntu Linux virtual machine (VM), execute following command to generate the SSH key.

ssh-keygen -t rsa -b 2048

SSH Key generation

The above command generates public and private keys with the default name of id_rsa in the ~/.ssh directory, just hit the Enter key without specifying any file name. 

If the system asks the overwrite confirmation, then go ahead with Y.

SSH Key generation

Next asking the passphrase, if you provide it, then memorize the same.

SSH Key generation

The key has been generated and saved under the above default path, need to display the contents of the public key using cat command.

cat  C:\Users\m1045767/.ssh/id_rsa.pub

Display SSH Key

Copy the above displayed key and assign to a variable sshPublicKey.

$sshPublicKey = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDGTUWggYHqf1f8FLs0aRgdOUThS9FLtDYcMXImAa5AYNLXu+NQPVW5BNLyi+cB5hLsNwTOP2hwKWXlsDXGvB03LAri78gwMB+GY+og+7ZiwqWFRW2Y6eX8NiTTMaVsImvIGZT9nV9ILh8IUG9uX0kCgs+04AOPKJqYx9+zBDGJBLd/LAG9wGpmiiiRJGdghMDeifpLCJuoNYyvYc3Nkuh7zzO83/Ssi3MyA/fgIvdh2PMTx7xkHJAyjTudQaeu3F4ltpx5wFUBlNBbfg/7eG75+HVij3URBA0kgU5cUGag+AjeaXN9PnjZnxHXWGHYFsZR1hM5SEaBHM4DHuUww7Xn **************************'

SSH Key assignment

STEP – 12: Configure the SSH Key


Next, time to attach the generated key, need to keep the same user which you have been provided in the credential setup. Execute the following command to configure the SSH key attachment.

Add-AzureRmVMSshPublicKey -VM $vmConfig -KeyData $sshPublicKey –Path "/home/clouduser/.ssh/authorized_keys"

Configure the SSH Key

STEP – 13: Create the Ubuntu Linux VM


Finally, all setup and configuration has been done, time to create a new Ubuntu Linux VM based on above all defined formations. Execute the final command to create the VM.

New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig

Create the Linux VM

Ignore the warning, if system throws anything during the creation of virtual machine, post creation you will get an acknowledgement.

Created the Linux VM

Congratulation, Ubuntu Linux VM is created!! 😊, time to connect the Linux VM.

You can verify the same through the Azure Portal also.

Linux VM

Connect the Ubuntu Linux VM


We will connect the Linux virtual machine through the SSH, which requires an IP address the Public IP and specified user over port 22. If you scroll your console, then will get the assigned IP for the created VM, otherwise execute the following command to get the IP address.

Get-AzureRmPublicIpAddress -ResourceGroupName $resourceGroup

Get Public IP

Henceforth the connection would be something based on the format of ssh username@publicip, execute the connection.

ssh clouduser@ 137.116.91.241

Connect the Ubuntu Linux VM

You will get something ECDSA key fingerprint confirmation, enter Yes to go ahead. Next, you will be prompted for the passphrase which you had entered during the SSH key generation. 

Post correct submission the Linux Ubuntu VM will be connected successfully.

Linux VM connected

Linux list command

Congratulation, Azure Ubuntu Linux VM connected!! 😊

You can visit one of my posts to do some hands on activity in the context of Linux command as well the installation of Apache on top of the Ubuntu Linux server.

2 comments:

  1. Nice article, I followed the steps successfully created VM with Linux Ubuntu OS. Nice keep it up..

    ReplyDelete