DEV Community

Cover image for How to Create and Connect to a Linux VM Using a Public Key
Florence Enenmo
Florence Enenmo

Posted on

How to Create and Connect to a Linux VM Using a Public Key

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

Click on "Virtual machine"

Image description

Configure Basic Settings:

Subscription: Select your subscription.

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

Image description

Virtual Machine Name: Enter a name for your VM.

Image description

Region: Select your preferred region.

Image description

Image: Ensure "Ubuntu Server" is selected.

Image description

Size: Choose an appropriate size for your VM.

Administrator Account:

Authentication Type: Select "SSH public key".

Image description

Username: Enter a username of your choice.

Image description

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

Image description

SSH Key Type: Select "RSA SSH Format".

Image description

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

Image description

Disks: Configure the disk settings as needed.

Image description

Networking: Configure network settings

Image description

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

Connect to Your Azure VM

Download private key:

Image description

Deployment in progress

Image description

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

Image description

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

Image description

Install web server:

Image description

*View of the web server:
*

Image description

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.

Top comments (0)