Create and Connect to a Linux VM using a Public Key
Tags: cloudcomputing, azure, linux, virtualmachine
Published: 2024-06-19 18:36:27


Azure virtual machines (VMs) can be created through the Azure portal. This method provides a browser-based user interface to create VMs and their associated resources. This blog post shows you how to use the Azure portal to create a virtual machine (VM) that runs on Linux server and connect using Powershell.

In this blog you will see how to create and connect a Linux VM using public key.Public key authentication allows you to access a server via ssh without password.

If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/en-us/free/?WT.mc_id=A261C142F) before you begin.


**Sign in to Azure**

Sign in to the [Azure portal](https://azure.microsoft.com/en-us/get-started/azure-portal).

**Create virtual machine** 

- Enter virtual machines in the search.

- Under Services, select Virtual machines.

- Subscription: Choose the appropriate subscription.

- Resource Group: Create a new or select an existing resource group.

- Virtual machine name: Give your VM a unique name.

- Region: Choose the data center region of your choice, preferably a region closest to you.

- Availability options: Select the availability preferences.

- Choose a linux server for the Image.

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

- Select size of the memory
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8wxeqk5st1gqkj8z77t6.png)

**Security and Authentication** 

- Authenticator type - Select SSH public key
 Azure will automatically generate an SSH key pair for you to 
 store for future use. It is a fast simple and secure way to 
 connect to the VM.

- SSH Key type - Select RSA SSH Format
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/od7rp4fgozk9hogadtnf.png)

- Enter key pair name
- Public inbound ports- Select allow selected ports
- Select HTTP(80),SSH(22)
- Click Review + Create

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

- VM is validated and shows the costing information.
- Select create.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shpkywxw8ca5tm6qiw2z.png)

**Download private key and create resource**

The private key will be saved in the computer.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6trxs8ffn483m5ti9o2.png)

**Deployment is complete**

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

**VM is ready**

Here we can see all the details on the VM that was just created. Copy the public IP address because we would use it to connect.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/blpq0xs4uw5igykcea9x.png)

**Connecting to the VM using power shell**

- Navigate to Windows Powershell on a PC.
- Run this command the command below
`ssh -i "path to the private key on the pc" username@publicIP`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q9g722cv3f9kct1p1adf.png)
After connecting to the VM,we run some sudo commands to test 
`sudo apt-get -y update`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ka4vlj9tuswgmmmndu5z.png)

`sudo apt-get -y install nginx`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/psxx4u2zy29od2obg4af.png)

**Testing Public IP on a Browser**

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

**Clean Up**

After creating and deploying the Vm, since this project is just for practice.We clean up by deleting the resource group to avoid incurring cost.

- Search for resource group
- Select the resource group that was created for this VM

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

- Select all resources that were created
- Click delete
- Enter resource group name to confirm deletion

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

**Conclusion**

With a secure shell (SSH) key pair, you can create virtual machines (VMs) in Azure that use SSH keys for authentication. This article elaborates on how to quickly generate and use an SSH public-private key file pair for Linux VMs.