Wednesday, October 2, 2024

How to Create SSH Key on Azure Server

Connecting to virtual machines (VMs) securely without relying on passwords is crucial for managing your infrastructure efficiently. This guide will walk you through the process of generating SSH keys, creating an Azure VM configured to accept them, and finally connecting to the VM using your keys.

Prerequisites

Before we begin, ensure you have the following:

  • Bash Shell: A command-line interface for executing commands.

  • ssh-keygen: A command-line tool for generating SSH keys, available on most Linux and macOS systems. To learn more about this tool, simply run $ info ssh-keygen in your terminal.

  • Azure Subscription: You need an active Azure subscription to create and manage virtual machines.

Step 1: Generate Your SSH Keys

  1. Open a Bash Shell: Access your command-line interface.

  2. Generate a Key Pair: Run the following command to generate an RSA key with a length of 2048 bits and a comment "Ubuntu@azure-server":

          $ ssh-keygen -t rsa -b 2048 -C "Ubuntu@azure-server"
        

    You'll be prompted to:

    • Enter a filename: This will be the name of the private key file, e.g., "server-key".

    • Enter a passphrase: This is a password to protect your private key. It's recommended to choose a strong and memorable passphrase.

  3. Locate Your Keys: Two files will be generated in the same directory:

    • server-key: Your private key (keep this secure!).

    • server-key.pub: Your public key (this will be used to configure your VM).

  4. Copy the Public Key: Open the server-key.pub file and copy its contents. It will look something like this:

          ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDMlUr7PCEdBmCVZHG5RqI8i7GgYAzd2G/FZ987XXa63vnqxZmZogVmmXrTnBHeM6oDv7v7g495CiiiINhJbGR4o7t4agiHOM43egDv7BbiViTlfVr3y5AxLUvRwHnC3egl8ABVX1anfXXR73x7IS3YRNWkh6gXtlhImw8UKG04UoZEmWB9BLt53lk/9c3Hxz22YZarzImrpQYy1XEUZ096B9mK/Fe+/McH78ZHUpXEgOZBIDP5KdqPk5XKznpwUDJ4/SPXPEWWCCjQ8gOoTFcFMaiMnXp5o5Udsi/DFO1TS/t8BeCRymkr5tdPvzexjkjkjkjkjkjkjkjkjkjkjkjkjkjt Ubuntu@azure-server
        

Step 2: Creating an Azure VM with Your Public Key

  1. Log into the Azure Portal: Open your browser and navigate to the Azure Portal (https://portal.azure.com).

  2. Create a New Virtual Machine: Click "Create a resource" in the left-hand menu and search for "Ubuntu Server". Choose the appropriate version of Ubuntu (e.g., 14.04).

  3. Configure Basic Settings:

    • Name: Enter a descriptive name for your VM (e.g., "azure-server").

    • VM Disk Type: Choose the appropriate disk type for your needs.

    • User name: Enter the user account name for your VM (e.g., "Ubuntu").

    • Authentication type: Select "SSH public key".

    • SSH public key: Paste the copied content of your public key (server-key.pub).

    • Subscription: Choose your Azure subscription.

    • Resource Group: Select an existing resource group or create a new one.

    • Location: Choose a location for your VM.

  4. Choose Virtual Machine Size: Select the appropriate VM size based on your requirements. Smaller VMs are suitable for testing while larger VMs are better for production workloads.

  5. Configure Optional Features:

    • Storage account name: Choose a name for your storage account that is easy to remember.

    • Network Security group: You can configure this to control network access to your VM.

  6. Review and Create: After completing all the necessary configurations, review the summary of your VM settings and click "Create" to initiate the VM deployment.

  7. Wait for Deployment: The VM deployment process can take some time. You'll be notified when the VM is ready.

Step 3: Connecting to the VM Using Your SSH Keys

  1. Locate Your SSH Key File: Make sure you have access to the private key file (server-key) that you generated earlier.

  2. Open a Bash Shell: Access your command-line interface.

  3. Connect via SSH: Execute the following command, replacing 52.183.31.11 with the public IP address of your VM:

          $ ssh -i server-key Ubuntu@152.183.31.11 -v
        

    You can use a more general form of this command, substituting keyname, username, and ip.address:

          $ ssh -i keyname username@ip.address -v
        
    h

    The -v flag enables verbose logging, which can be helpful for troubleshooting.

  4. Authenticate: You will be prompted to enter your passphrase for the private key.

  5. Accept Certificate: You may be asked to accept a new SSH certificate.

  6. Access the VM Shell: If successful, you will be logged into the VM's command-line interface, where you can manage and operate your VM.

Conclusion

This guide has shown you how to securely connect to your Azure VM using SSH keys. This method offers a robust security measure compared to using passwords, eliminating the risk of password compromise.

Remember to keep your private key file safe and secure!

0 comments:

Post a Comment