How to Create and Connect to a Linux VM using a Public Key
Published: 2024-06-19 13:44:50

**Table of Contents**

Introduction
Step 1. Login to Azure Portal
Step 2. Select/click Virtual Machine
Step 3. Create Azure Virtual Machine
Step 4. Basic Tab: Create new Resource Group
Step 5. Basic Tab: Fill all the Virtual Machine Instance Details
Step 6. Disk Tab: Fill all the Disk fields
Step 7. Validation Passed then Click Create
Step 8. Create the Linux VM
Step 9. Open Command Prompt and run some command lines
Step 10. Run IP address on browser

Introduction
Virtual Machine is one of the resources Azure has, to help organizations and individuals execute various tasks without accessing a local computer. It simply means, having a full computer system running in the cloud with lots of advantages than carrying or stationing a physical computer system. 

Connecting a Virtual Machine using SSH Public Key makes it a more secured way of connection. This helps to make a connection via command lines, run commands through your terminals to talk to server and client. 

**Step 1. Login to Azure Portal**
Open a browser, type url: portal.azure.com
Fill in your registered username and password, then process your entries. A successful login lands into Azure portal where various tasks can be executed accordingly. Also make sure you have a subscription in Azure to enable the creation of VM.

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


**Step 2. Select/click Virtual Machine**
There are 3 ways to locate Virtual Machine in the portal (via search bar/resource group/portal tab). Go to the search bar type in virtual machine, select Virtual Machine from the dropdown list.


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

**Step 3. Create Azure Virtual Machine**
Locate the create button up left, click Connect from the dropdown.


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

**Step 4. Basic Tab: Create new Resource Group**

**Notice:** There is a note on how each instance field works. This icon is found suffix each field label. Take a cursor point to the icon, then read. Also all red asterixis label field are compulsory and must be filled.

Select from already created resource group or create new one if none exists. Click New and type in the new resource group name, click ok.

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

**Step 5. Basic Tab: Fill all the Virtual Machine Instance Details**

- Virtual Machine Name: Valid name could contain alpha-numeric, alpha + some special characters excluding underscore (_). Eg. Denz-VM, Denz4 etc
- Region: Select from the dropdown according to your architectural plan.

**Step 6. Disk Tab: Fill all the Disk fields**
- Region: Select a desired data center region
- Availability options: Select how you want your availability zone to run (eg. no infrastructural redundancy required etc.)
- Availability zone: Select your zone(s) or 
- Security type: Select how you want to secure the VM. Eg. Standard
- Image: Select either Linux type of OS **(e.g Ubuntu Server 24.04LTS x64)**

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

- VM architecture: Could be on x64
- Size: Select from the dropdown as it is available in the regional zone selected.
- Enable Hibernation: Check if you want otherwise ignore
**Administrator account**
Because Linux image was selected instead of Windows, the Authentication type will show these options --SSH Public Key -- Password
**Here we are selecting SSH Public Key.**
- Username: Type in your username
- SSH public key source: You have options to Generate a new key or Use the existing key. (Select Generate new Key Pair).
- SSH Key Type: Checkbox: RSA SSH Format
- Key pair name: This key is automatically generated using the inputted VM name and must be kept safe because without this key your connection to VM will be aborted. 


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


- Public inbound ports: Allow selected ports
- Select inbound ports: Select how you want the VM to be accessed. Eg. HTTP(80), SSH(22)
- Click on Next Disk button to proceed to the next tab set-up.

**Note:**
You can leave every other field in this Tab at default then proceed.

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

**For now, accept Networking, Monitoring, Advance and Tags Tabs at default level. Click Review and Create Button.**

**Step 7. Validation Passed then Click Create**
At this point the information inputted on the Tabs are reviewed for validation. Most especially make sure you have a strong internet connection in order to experience smooth loadings.

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

**Step 8. Create the Linux VM**
Once you click on create button, it will prompt you to download a private key file which must be done if not you will not find it again. Click "Download private key and create resource", automatically the file will be downloaded into your download folder.

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44ko0zzzlinqwmc3vy6d.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4etez5fu2yqxe54r4x5x.jpg)

**Step 9. Open Command Prompt and run some command lines**
Here you type in "command prompt" in the search field of your task bar, select run as administrator.

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

To copy private key file path, open the download folder, click on the file once to select the file, click inside the address bar and copy the highlighted folder path (eg. C:\Users\dell\Downloads)


Combine and type these 3 things inside the command prompt:
1. ssh -i
2. C:\Users\dell\Downloads\DezxLVM_key.pem
3. DezAzure@20.160.81.95 (this is your Linux VM username@Ip address)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wbycblf5vaea1okir5wb.jpg)
ssh -i C:\Users\dell\Downloads\DezxLVM_key.pem DezAzure@20.160.81.95

Hit enter button on your keyboard and type yes to process further.

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

type in: sudo apt-get -y update 
Hit enter key to process further
DezAzure@DezxLVM:~$ sudo apt-get -y update

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

type in: sudo apt-get -y install nginx 
Hit enter key to process further

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

**Step 10. Run IP address on browser**
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e2296zvgl5xx7oa1vqrb.jpg)