DEV Community

Bolanle Ayo-Oyeledun
Bolanle Ayo-Oyeledun

Posted on

Step-by-Step Guide: Setting Up and Securing a Linux VM with SSH Key Authentication

Imagine having the power to spin up a Linux Virtual Machine (VM) in the cloud and securely connect to it within minutes. With Azure’s seamless integration and robust security features, creating and connecting to a Linux VM using an Azure-generated SSH key pair has never been easier. Let’s dive into the steps to get you up and running quickly!.

Step 1: Set Up Your Azure Account

Before you begin, ensure you have an Azure account. If you don’t have

  1. one, you can sign up for a free account here.

Step 2: Create a Linux Virtual Machine

  1. Log In to the Azure Portal:
    Go to the Azure Portal and log in with your credentials.

  2. Create a New Linux VM:

    • Navigate to Virtual Machines: In the left-hand menu, select Virtual Machines > Create > Azure Virtual Machine. Create New Linux
  3. Configure Basic Settings:

    • Subscription: Choose your subscription.
    • Resource Group: You can create a new resource group or select an existing one. Subscription and Resource Group
    • Virtual Machine Name: Enter a name for your VM. Virtual Machine Name
    • Region: Select a region closest to you or your user base. Region
    • Availability Options: Choose based on your requirements.
    • Image: Select the desired Linux distribution, such as Ubuntu Server. Image
    • Size: Choose a size based on your resource needs (e.g., Standard B1s for light workloads). Size
  4. Administrator Account:

    • Select "SSH public key" as the authentication type. Authentication Type
    • Username: Enter your desired username.
    • SSH Public Key: Click on "Generate New Key Pair". Azure will create an SSH key pair for you. The public key will be automatically added to your VM. SSH Public Key source
    • Key Pair Name: Provide a name for the key pair if desired, or let Azure use a default name. Key Pair Name
  5. Networking: Configure your networking settings. Ensure you allow SSH (port 22) in the inbound port rules.
    Inbound Ports

  6. Review and Create: Review your configurations and click "Create". Azure will deploy your VM. It may take a few minutes to complete your deployment.
    Review and Create

  7. Download the private key file (.pem)

when prompted. This is essential, as you will need it to connect to the VM. Azure does not store the private key, so save it securely. Follow these steps to copy Location of the (.pem) file
File Location
Copy the name of the (.pem) file
File name

Step 3: Find Your Public IP Address

Now that your VM is deployed, connect to it using the private key file Azure provided. So, navigate to the "Overview" section of your VM. Here, you’ll find the public IP address assigned to your Linux VM. Note this address, as you’ll need it to connect.
Copy Public IP Address

Step 4: Connect to Your Linux VM

  1. Open Your Terminal:
    On your local machine, open a terminal (Linux or macOS) or a PowerShell (Windows with SSH installed).

  2. Connect Using SSH:
    Open your terminal and run the SSH command with the path to your private key file:
    Use the following command to connect to your VM, replacing <username> with your VM username and <public-ip> with your VM’s public IP address:

   ssh -i /path/to/your-key-name.pem <your_username>@<public_ip_address>
Enter fullscreen mode Exit fullscreen mode

Replace /path/to/your-key-name.pem with the path to your downloaded .pem file.
Replace with the username you specified when creating the VM.
Replace with your VM's public IP address.

3.First-Time Connection:
The first time you connect, you may see a message asking if you want to continue connecting. Type yes and press Enter.
Yes

4.Access Granted:
If everything is set up correctly, you should now be logged into your Linux VM.
Access Granted. Congratulations! You are now connected to your Linux VM using the Azure-generated SSH key pair.

Step 5: Managing Your VM

Once connected, you can manage your VM using standard Linux commands. You can install software, configure settings, and perform administrative tasks as needed.
To run install Nginx, run these two commands

   sudo apt-get -y update
Enter fullscreen mode Exit fullscreen mode

and

   sudo apt-get -y install nginx
Enter fullscreen mode Exit fullscreen mode

First Command
NGINX

Conclusion

Connecting to a Linux VM in Azure using an Azure-generated SSH key pair is a secure and efficient way to manage your cloud resources. By following the steps outlined in this guide, you can easily set up your environment and start taking advantage of the capabilities Azure has to offer. Happy cloud computing!.

Top comments (0)