How to Create and Connect to a Linux VM Using a Public Key
Tags: linux, virtualmachine, ssh, publickey
Published: 2024-06-21 12:54:01

Using SSH keys, creating and connecting to a Linux VM ensures a secure and password-less login. Below is a step-by-step guide to help you set up and connect to a Linux VM using a public key.


#Create a Linux VM on Azure

##Login to Azure Portal

Click on **"Create a resource"
**

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

Click on **"Virtual machine"**

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


## Configure Basic Settings:

**Subscription:** Select your subscription.

**Resource Group:** Create a new resource group or use an existing one.

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

**Virtual Machine Name:** Enter a name for your VM.

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

**Region:** Select your preferred region.

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

**Image:** Ensure "Ubuntu Server" is selected.

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

**Size:** Choose an appropriate size for your VM.

**Administrator Account:**

**Authentication Type:** Select **"SSH public key"**.

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

**Username:** Enter a username of your choice.

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

**SSH Public Key Source:** Select **"Generate new key pair"**.

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

**SSH Key Type:** Select **"RSA SSH Format"**.

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

**Select inbound ports:** Select **"HTTP(80), SSH(22)"**.

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

**Disks:** Configure the disk settings as needed.

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

**Networking:** Configure network settings

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

Management, Advanced, and Tags: Configure additional settings if needed. Leave at default for beginner learning.

**Review + Create:** Review your settings and click **"Create"**.

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

#Connect to Your Azure VM

**Download private key:**

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

**Deployment in progress**

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

When **"Deployment is complete"** Select **"Go to resource"**.

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

**Retrieve Public IP Address:**

Go to the **"Virtual Machines"** section in the Azure portal.
Select your VM.
Copy the public IP address of your VM.

**Connect Using SSH:**

Open Terminal (Linux/Mac) or PowerShell (Windows) and connect to your VM using the private key.

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


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

**Install web server:**

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

**View of the web server:
**

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

Troubleshooting Tips
Permission Denied (Publickey): Ensure the public key is correctly added during VM creation and the username is correct.

File Not Accessible: Verify the file path and permissions of your private key.

Connection Issues: Ensure the network security group (NSG) rules allow inbound traffic on port 22.

Conclusion
Using SSH keys for authentication enhances the security of your connections to Linux VMs. By following these steps, you can easily create and connect to a Linux VM using a public key on Azure, ensuring a secure and password-less login.