DEV Community

Cover image for How to create and connect to a Linux VM using a Public Key
Chidera Enyelu
Chidera Enyelu

Posted on

How to create and connect to a Linux VM using a Public Key

Creating and connecting to a Linux virtual machine (VM) on Azure using SSH public key authentication involves several steps. Here’s a detailed guide to help you through the process:

Step 1: Sign in to Azure Portal

  1. Sign in: Go to the Azure portal and sign in with your Azure account.

Step 2: Create a Resource Group (if needed)

  1. Resource Group: If you don’t have a resource group where you want to deploy the VM, create one:
    • Click on Resource groups in the Azure portal's left-hand menu.
    • Click + Add to create a new resource group.
    • Provide a name, choose a region, and click Review + create then Create.

Image description

Step 3: Create a Virtual Machine

  1. Create VM: Now, create a new virtual machine:
    • In the Azure portal, click + Create a resource at the top-left corner.
    • Search for Virtual machine and click Create.

Image description

  1. Basics:
    • Subscription: Choose your Azure subscription.
    • Resource Group: Select the resource group you created or an existing one.
    • Virtual Machine Name: Provide a name for your VM.
    • Region: Choose the Azure region where you want to deploy your VM.
    • Image: Choose a Linux distribution (e.g., Ubuntu, CentOS) from the list.
    • Size: Select an appropriate VM size based on your requirements.

Image description

  1. Administrator Account:
    • Username: Choose a username for the administrator account (e.g., azureuser).
    • Authentication type: Select SSH public key.

Image description

  • SSH public key: Paste your SSH public key. If you don't have one, generate it using ssh-keygen on your local machine (ssh-keygen -t rsa -b 4096).

Image description

  1. Disks and other settings:

    • Customize the disk configuration, networking, management, and monitoring settings as per your requirements.
  2. Review + create:

    • Review your configuration settings.
    • Click Create to start deploying the VM.

Image description

Step 4: Connect to Your Virtual Machine

  1. SSH Connection: Once the VM is deployed, you can connect to it using SSH:
    • In the Azure portal, go to Virtual machines and select your VM.
    • Under Connect, click on SSH.
    • Use the SSH command provided to connect to your VM from your local terminal or SSH client.
    • Another step is to go to command prompt,run as an administrator and type the SSH command shown below to connect.

Image description
Example SSH command:

   ssh -i "/path/to/your/private-key-file.pem" username@your-vm-public-ip
Enter fullscreen mode Exit fullscreen mode

Step 5: Manage and Monitor Your VM

  1. Management: Use Azure portal or Azure CLI to manage and monitor your VM:
    • Start, stop, restart your VM.
    • Scale up or down to change VM sizes.
    • Monitor performance and set up alerts.

Step 6: Delete Your VM (if no longer needed)

  1. Clean up: If you no longer need the VM, delete it to avoid incurring charges:
    • In the Azure portal, go to Virtual machines, select your VM, and click Delete.

Additional Tips:

  • Azure CLI: You can also use Azure CLI for VM deployment and management if you prefer command-line tools.
  • Templates: Consider using Azure Resource Manager (ARM) templates for consistent and repeatable deployments.

By following these steps, you can create and connect to a Linux VM on Azure using SSH public key authentication securely. Adjust settings based on your specific needs and security requirements.

Top comments (0)