How to create and connect to a Linux VM using a Public Key.
Published: 2024-06-21 16:08:57


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 quickstart shows you how to use the Azure portal to deploy a Linux virtual machine (VM) running Ubuntu Server 22.04 LTS. To see your VM in action, you also SSH to the VM and install the NGINX web server.


## Sign in to Azure
1 - Sign in to the Azure portal.


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

## Create virtual machine
1 - Enter virtual machines in the search.
2 - Under Services, select Virtual machines.


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

3 - In the Virtual machines page, select Create and then Virtual machine. The Create a virtual machine page opens.


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

4 - In the Basics tab, under Project details, make sure the correct subscription is selected and then choose to Create new resource group. Enter _AdeResourceGRP_ for the name.*.


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


5 - Under Instance details, enter AdeVM for the Virtual machine name, and choose Ubuntu Server 20.04 LTS - Gen2 for your Image. Leave the other defaults. The default size and pricing is only shown as an example. Size availability and pricing are dependent on your region and subscription.


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


6 - Under Administrator account, select SSH public key.
7 - In Username enter azureuser.
8 - For SSH public key source, leave the default of Generate new key pair, and then enter _AdeVM_Key_ for the Key pair name.


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

9 - Under Inbound port rules > Public inbound ports, choose Allow selected ports and then select SSH (22) and HTTP (80) from the drop-down.


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


10 - Leave the remaining defaults and then select the Review + create button at the bottom of the page.


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

11 - On the Create a virtual machine page, you can see the details about the VM you are about to create. When you are ready, select Create.


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

12 - When the Generate new key pair window opens, select Download private key and create resource. Your key file will be download as myKey.pem. Make sure you know where the .pem file was downloaded; you will need the path to it in the next step.


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


13 - When the deployment is finished, select Go to resource.


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

14 - On the page for your new VM, select the public IP address and copy it to your clipboard.



## Connect to virtual machine

Create an SSH connection with the VM.

1 - If you are on a Mac or Linux machine, open a Bash prompt and set read-only permission on the .pem file using _chmod 400 ~/Downloads/AdeVM_Key.pem_. If you are on a Windows machine, open a PowerShell prompt.


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


2  - At your prompt, open an SSH connection to your virtual machine. Replace the IP address with the one from your VM, and replace the path to the .pem with the path to where the key file was downloaded.


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


## Install web server

To see your VM in action, install the NGINX web server. From your SSH session, update your package sources and then install the latest NGINX package.


1 - Update package sources

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


2 - Install latest NGINX  package


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



## View the web server in action

Use a web browser of your choice to view the default NGINX welcome page. Type the public IP address of the VM as the web address. The public IP address can be found on the VM overview page or as part of the SSH connection string you used earlier.


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