HOW TO CREATE A LINUX VIRTUAL MACHINE IN AZURE
Published: 2024-04-29 15:19:03

There are many reasons to create a Linux Virtual Machine (VM) in Azure; like hosting a web server, database, or many other uses for VMs. Creating a Linux virtual machine (VM) on the Azure portal is a detailed process that involves multiple steps and once created, the VM can be connected with a password-based authentication or via SSH  public key authentication, enabling you to configure it for whatever workload you need. 
In this comprehensive guide, I will walk you through each step using a public key authentication and ensure you clearly understand how to create a Linux VM on the Azure portal using Ubuntu Server.

We will also demonstrate how to prevent unexpected SSH disconnections due to idle sessions, it's important to extend the idle timeout for your IP address to prevent unexpected logouts.

Before you start, ensure you have an active Azure subscription, if you don't have one, [**Click here**](https://azure.microsoft.com/en-us/) to create a free account.

Now that you have created your Azure subscription, sign in to the Azure portal using your e-mail and password to get started.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rdlahdbz5u3ujr2tjott.PNG)

**CREATE YOUR VIRTUAL MACHINE**
a) On your Home Dashboard click on the Search bar and Search for 
   Virtual Machine 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/syop4bo5xmacnpzgq9f9.PNG)

b) Click on Create and select the Azure Virtual Machine option

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jeybr39df0pnj7s535a3.PNG)

**YOU WILL BE DIRECTED TO DIFFERENT TABS TO CONFIGURE YOUR VIRTUAL MACHINE**

**<u>Basics Tab</u>**

**In the Project Details Section**, we have the Subscription and Resource Group
_a)Subscription
To utilize Azure, a subscription to the platform is required. This subscription grants authorized access to a wide range of Azure products and services and the ability to organize resource groups effectively and manage billing. Additionally, Azure subscriptions offer a free trial and a flexible pay-as-you-go pricing model._

_b)Resource Groups
Resource Groups are essential containers that house multiple Azure resources, such as virtual machines, app services, storage accounts, and databases. They offer crucial flexibility for managing the lifecycle of all services in one central location within the Azure cloud. With a resource group, you can confidently deploy, update, and delete these services simultaneously._


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xkorf94in94vfxiya2al.PNG)


**In the Instance Details Section**

a) Enter a name in the Virtual machine name field.
b) Select a region closest to you in the Region field.

_**Regions** refer to the geographic areas where Microsoft Azure has deployed its data centers, from which users can deploy their applications and services. Each region is made up of multiple data centers that are connected by a low-latency network, enabling users to access their resources quickly and reliably_


C) Click on the drop-down to select your availability option and 
   availability Zone.

_**Availability zones** are physically separated data centers within an Azure region. Each availability zone encompasses one or more data centers that function independently with their own power, cooling, and networking infrastructure. These zones are interconnected through high-speed, private fiber-optic networks. Therefore, if one zone experiences an outage, the others remain operational. At a minimum, three distinct availability zones are situated kilometers apart._


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxut0hsccaffqmdxom7k.PNG)

d) Click the drop-down menu to select your security type

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u0eoqbubsufng58ut0eu.PNG)

e) In the Image field, click the dropdown menu and select any Ubuntu Server.

_Ubuntu is a Linux Operating System. Images are utilized to install an operating system in a new virtual machine. An image may also contain one or more data disks. To view a range of operating systems available in the Marketplace, including the latest versions of various Linux distributions and Windows Server, click on **"see all images"**_

f) In the size field, click the drop-down menu to select your size or click to see all sizes.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bbzycuxruzsp0axrp3i3.PNG)

**In the Administrator account section**

a) Select the SSH Public Key authentication type.

_**SSH public key authentication** relies on key pairs and cryptographic algorithms for secure remote access. Public key authentication offers stronger security and protection against various attacks, making it a preferred choice for secure remote connections, While **Password-based authentication** involves using a username and secret password to access a system as shown below._

However, for this tutorial, we will use SSH public key authentication.

b) Set up a unique username (Keep the username secure as we will require it later to log into our virtual machine
c) In the SSH public key source, Click the drop-down menu and select Generate new key pair
d) Create a key pair name



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t3wj30q9h4yupo92no1r.PNG)

**Inbound Port Rules Section**

a) In this category, select SSH (22) as your inbound port to connect to the Linux server.

b) Click "Next: Disk>" to direct you to the disk page


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmuc56az9d9hmbplzl1o.PNG)

**<u>DiskS Tab</u>**
a) Leave the OS Disk Size in the image default setting or select a preferred size from the drop-down menu.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/682kn9pb342cftbil9mb.PNG)

b) Select Standard SSD or any other disk type of your choice in the OS disk type field. Leave other columns as default.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4b7afaz29l6m95hom2n.PNG)


**Leave networking, management, monitoring, and advanced tabs configurations as default. Skip to the Tag page.**

**<u>Tag Tab</u>**
a) Give your tag a name and value
_Tagging is a way to organize your Azure resources, resource groups, subscriptions, and management groups hierarchy for better filtering, monitoring, or querying a few scenarios. Each tag contains a name and a value pair._


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arh6vw9ewx8bws7vw8ih.PNG)

Review the summary of your virtual machine configuration and click Next: Review + Create

**<u>In the Review + Create tab </u>**
a) Click the Review + Create tab and click on the Create button to begin the deployment process.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h82q6xxzleqz8icg73hw.PNG)

b) While the deployment is in process, a small window will pop up,  click on the **Download private key and create resource** to generate a new key pair 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0dlhogarjnz51ftdwzd.PNG)

c) Once the new key pair has been downloaded and the deployment process is completed, click on  **Go to Resource**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pf25ehr4z2armfmw650.PNG)

**CONNECT TO YOUR VIRTUAL MACHINE**

a) Click on the connect button to SSH to your Virtual Machine


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3d771rfbp5qtzi6g362g.PNG)

b) Keep your Admin username and public IP address secure as we will require them later to log into our virtual machine


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nj43a04nn0juh72hk6ti.PNG
)

c) Open PowerShell or CMD on your computer and input the command below
        **ssh -i (private key path) username@public IP address**

d) **To copy the private key pair**, locate your downloaded private key in your computer, right-click on the file, and click properties.
Click the **Security tab** at the top of the window to copy the object name (private key path).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wqatb9878oycdzv5jfg5.PNG)

e) Input this command in your PowerShell terminal and click enter
**ssh -i C:\Users\Josh\Desktop\LinuxV_key.pem josh3k2@20.97.192.255**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fmfzxfxuq84i1y2zu1bz.PNG)

f) At this stage **my connection timed out** because of the default “idle time out” setting in the Azure portal. If you got this or **" connection closed"**, the next thing is to go back to the Azure portal and locate your virtual machine.

g) On the overview page, click on your Public IP address


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2afxh77w7o1967iyzhsv.PNG)

h) Click on the idle timeout cursor to increase the time to your desired value and click on save. In this case, I will increase it to 30 minutes.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d0lrdcqt973mhzb6i2vu.PNG)

I) Next, we go back to our Power Shell window and input our previous command again **ssh -i (private key path) username@public IP address**
Next, type **yes** to connect.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a8gpnk6dru3s1z9qdpj4.PNG)

_**Congratulation!!! You have successfully connected to your Linux Virtual Machine**_

     
  **HOW TO DISSOCIATE YOUR IP ADDRESS**
One method to reduce cost is disassociating your virtual machine from its IP address.

a) Locate your virtual machine and Click on the Public IP address associated with your virtual machine


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fncajjqse9x6mkmmftyj.PNG)

b) Click on the overview

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iigzpsdtggr7vd4qfquv.PNG)

c) On the overview page, click on the Dissociate option located at the top of the page.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxntu6q27cq8kbyrb0ar.PNG)

d) Click the Yes button to confirm the permanent dissociation of your public IP address from your virtual machine


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2y9x0o1p7xg929nqn24a.PNG)

        
  **HOW TO ASSOCIATE YOUR IP ADDRESS**
a)To associate your IP, simply click on the Associate button.

b) A page will open up, click on the drop-down menu and choose the Resource type and Network interface. Click OK.

c) Your IP will be associated back to your virtual machine.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oe64bjnj3v2phqdsdm3e.PNG)