Creating and Connecting to a Linux Virtual Machine Using Public Key Authentication on Azure Portal
Tags: azure, virtualmachine, linux, techtalks
Published: 2024-06-21 04:24:01

Azure Virtual machines (VMs) can be created through the azure portal. The Azure portal is a browser-based user interface to create Azure resources. This article shows you how to use the Azure portal to deploy a Linux Virtual Machine (VM)using Public key Authentication.

## Step 1: Sign in to Azure Portal
1. Open your web browser and access the [Azure Portal](url).
2. Sign in using your Azure account credentials

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

## Step 2: Create a Resource Group (if not available or use an existing one)
1. **Resource Group**: If a suitable resource group for your VM deployment does not exist, create a new one.

- Click on **resource groups** in the upper left corner of the Azure portal.

- Click **+ Create** to create a new resource group.

- Assign a **Name** for your resource group, choose a **Subscription,** and select a **Region**.

- Click **Review + create** and then **Create** to create the resource group.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9icve1n8ghrkmn634z49.png)


## Step 3: Create a Virtual Machine
1. **Create VM:** Now create a new Virtual Machine. There are several methods of searching for virtual machine:

- In the Azure portal, click **+ Create a resource** at the top-left corner.

- Search for Virtual Machine and click **Create**. 


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

Alternatively, you can:

- In the search bar of the home page, type 'Virtual Machines' and select **Virtual Machines **under **Services**

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

- On the Virtual Machines page, select **Create**, this will open the Create a **virtual Machine** page

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

**1. Configure the Basics**

-  **Subscription**: Select the correct subscription you want to use
-  **Resource group**: Select the resource group you have created or create a new one if you have not (another way of creating resource group)
- **Virtual machine name**: Specify a name for your VM

- **Region:** Choose the region where you would want to deploy your VM

- **Availability options**: Choose any if needed

- **Image:** Select a Linux distribution (Ubuntu Server from the list)

- **Size:** Choose a VM size based on what you require.


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

2. **Administrator Account**

- **Authentication type**: Select **SSH public key**

- **Username:** Choose a username

- **SSH public key source:** Keep the default setting of **Generate new Key pair**

- **Key pair name**: Name mostly pops up on its own

- **Public inbound ports:** Select **Allow selected ports**

- **Select inbound ports:** Choose **HTTP (80)** and **SSH (22)** or as required.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pvzomt7cb79sar8w3fhb.png)
 
**3.Disks and other settings:**

Configure the disk, networking, management, and monitoring options according to your requirements.

**4. Review + Create:**

- Review your VM configuration

- Click **create** to start deploying your VM after validation passed.

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

## Step 4: Connect to your Linux VM

**SSH Connection through PowerShell:** Once the deployment is complete, you can connect your VM to SSH through PowerShell.

- Navigate to your VM in the Azure portal.

- Under Settings, click on Connect to get connection information.

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

 **Using PowerShell:** This can be done with your Laptop.

- Open PowerShell on your physical computer

- Use this command to connect to your Linux VM using SSH: 
[ssh username@public-ip-address](url)


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

whereby, you replace the 'username' with the username you specified during the creation of VM and 'public-ip-address' with the public address of your VM.

- If prompted to confirm the connection, type 'yes' and press Enter.
 

## Step 5: Clean Up Resources
After you've finished using your VM, it's a good practice to clean up resources to avoid unnecessary charges:

- In the Azure portal, navigate to your resource group containing the VM.

- Select the resource group and click Delete.

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

- Follow the prompts to confirm deletion. This action will delete all resources within the resource group, including the VM, disks, and network interfaces.


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

In conclusion, setting up and deploying a Linux VM on Azure, along with connecting to it via SSH using PowerShell, offers a versatile and potent environment for development and testing purposes. It's essential to regularly tidy up resources to control costs and uphold an organized Azure setup. Azure's user-friendly interface and comprehensive management tools simplify the process of efficiently handling your virtual machines.