DEV Community

Cover image for Linux VM Access Simplified: Connect Securely with SSH Keys
Oluwatobiloba Akinbobola
Oluwatobiloba Akinbobola

Posted on

Linux VM Access Simplified: Connect Securely with SSH Keys

Introduction

Securely connecting to cloud resources like Linux VMs can often feel complex, but SSH key authentication offers a smooth, password-free way to access your virtual machines. By eliminating passwords, SSH keys provide a major boost to security, protecting your VMs from common threats while simplifying access.

In the Azure portal, you will find quick links to all the tools you need to manage resources, configure services, and get started with secure SSH access in minutes. Using SSH keys lets you manage multiple machines with ease, making it the go-to method for both security and efficiency. In short, it is a smarter, safer way to connect.

Skill 1: Use the Azure portal to create a virtual machine
Steps to create and deploy a Linux virtual machine using the portal.

  1. Sign in to the Azure portal: https://portal.azure.com.

  2. Search and select Virtual machines.

  3. Click + Create, then select Azure virtual machine in the drop-down.
    Azure virtual machine

  4. Complete the Basics configuration tab
    VM configuration tab
    VM configuration tab2

  5. Click Next: Disks >, specify the following settings (leave others with their default values)
    VM Disk

  6. Click Next: Networking > and make a few changes.
    Networking

  7. Click Next: Management > and check the following settings (leave others with their default values)

  8. Click Next: Monitoring > and specify the following settings (leave others with their default values)

  9. Click Review + Create.
    VM Review + Create

  10. After the validation passes, click Create.
    VM Validation

  11. When prompted, select Download private key and create resource.
    Private key

  12. Wait for the deployment to complete, then select Go to resource.
    VM Deploy

  13. From the Overview blade, ensure the virtual machine Status is Running.
    VM Running

Skill 2: Connect to the virtual machine and install OS updates
To establish a connection to the virtual machine via SSH. Network traffic must be permitted to port 22 to connect. After connecting, update the operating system and check for updates.
Continue in the portal on the virtual machine page.

  1. On the Overview tab, in the top menu, select Connect and Connect in the drop-down.
    VM Connect
    To view connection methods, select More ways to connect, and then select Native SSH. Note that port 22 is not set up for SSH access when you go over the SSH connection instructions. This needs to be enabled in order to continue. The Native SSH page should be closed.
    As a virtual firewall, a Network Security Group (NSG) controls all incoming and outgoing traffic to Azure resources. By default, it blocks all other incoming traffic but permits access from load balancers and other virtual computers connected to the network.

  2. Choose Network settings from the Networking section. Take note of the NSG (Network Security Group) regulations.

  3. Select Create port rule and then Inbound port rule.
    Network settings

  4. To allow port 22, select SSH in the Service drop-down, then Add the rule.
    SSH

  5. To allow port 80, select HTTP in the Service drop-down, then Add the rule.
    HTTP
    Apply Port
    Inbound port created

  6. Copy the public IP address for connecting to the virtual machine, close the Native SSH page,
    VM IP address
    open a CMD window to run the SSH connection string.
    CMD

  7. At the prompt, use SSH to connect to the VM, including the correct key path and public_ip_address (e.g., c:\users\admin\downloads\VM1_key.pem). Type yes when prompted.

ssh -i 'c:\users\admin\downloads\VM1_key.pem' adminuser@public_ip_address

Enter fullscreen mode Exit fullscreen mode

CMD IP
Ensure the command is successful and the prompt changes to adminuser@VM1.
CMD prompt

  1. Get the list of OS updates that are available, then install them. To proceed, type "yes" when asked. Every command needs to be correctly completed.
sudo apt update
Enter fullscreen mode Exit fullscreen mode
 sudo apt upgrade
Enter fullscreen mode Exit fullscreen mode

Keep the CMD window open, stay connected to the virtual computer, and move on to the next job.

Skill 3: Install and test the Nginx web service

At the CMD prompt, run each command individually to install Nginx, confirming with Y when prompted. Ensure each command completes successfully.

 sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode
  1. Start the Nginx service.
sudo systemctl start nginx
Enter fullscreen mode Exit fullscreen mode
  1. Configure Nginx to launch on boot. This is optional but good practice
sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode
  1. Check to ensure the Nginx service is active (running).
 service nginx status
Enter fullscreen mode Exit fullscreen mode

Nginx

  1. Open the Nginx welcome page by entering your virtual machine's public IP address, or visit http://public_ip_address in a browser. Nginx welcome page
 curl -m 80 public_ip_address
Enter fullscreen mode Exit fullscreen mode

Nginx welcome transfer data

Top comments (1)

Collapse
 
etiifiok_udofiah_eb450b4b profile image
etiifiok udofiah

Awesome write up. Kudos