Linux Virtual Machine Setup Made Easy: A Beginner's Guide To Creating And Authenticating A VM With SSH Keys
Welcome to this beginner's guide to setting up a Linux virtual machine (VM) with SSH key authentication! Virtual machines offer an excellent solution for running multiple operating systems on a single physical machine, and SSH keys provide a secure and convenient method for accessing your VM. In this guide, you will be walked through the step-by-step process of creating and authenticating a Linux VM with SSH keys, helping you begin your journey with virtualization and security.
Sign In To Your Azure Portal
Before we begin, if you don't have an Azure subscription, you can create a free account here. Once you've created your Azure subscription, sign in to the Azure portal using your email and password to get started.
Create Your Virtual Machine
To create a Linux virtual machine on Azure, you have options like the Azure portal, Azure CLI, Terraform, and Bicep. However, this walkthrough focuses on deploying a Linux virtual machine using the Azure portal.
i. Type "Virtual Machine" into the global search box
ii. Select “Virtual Machine” from the search results.
iii. Click “Create".
iv. Select the highlighted virtual machine.
v. You will be redirected to the "Basics" page
Project Details
In the "Project Details" section, we can find the subscription and resource group.
- Subscription:
Azure subscription is like a membership that allows you to use Microsoft's cloud services. It's similar to signing up for a streaming service to watch movies or shows online. Once you have an Azure subscription, you can access and use services like virtual machines (which are like remote computers), storage (for saving files), and databases (for organizing data), just like how you can watch movies or shows once you've signed up for a streaming service.
2. Resource Group:
An Azure Resource Group is like a folder in Microsoft Azure that helps you manage everything related to a project. Think of it as a drawer in your kitchen where you keep all your baking supplies. For instance, when you're making cookies, you need flour, sugar, and chocolate chips. You can put all these ingredients in one drawer to keep them organized and easily accessible when you're baking. Similarly, you can use an Azure Resource Group to keep all your project resources organized and accessible in one place. These resources can include virtual machines, databases, storage accounts, networking components, and more.
Instance Details
i. Name your virtual machine.
On a Linux VM, you can assign a name of up to 64 characters, which serves as the computer name configured within the operating system.
ii. In the region section, choose the location for your virtual machine.
An Azure region houses Microsoft Azure data centers, positioned worldwide for accessible and compliant operations. When setting up a virtual machine, select a region to allocate resources. Placing VMs near users enhances performance and ensures compliance.
iii. click the drop-down to choose your availability option and availability zone.
In Azure, regions can have three availability zones: Zone 1, Zone 2, and Zone 3. They're strategically spaced apart to reduce the chance of multiple zones being impacted by local outages or weather events.
iv. Select your security type from the drop-down menu.
v. In the image section, choose "Ubuntu Server 20.04 LTS - x64 Gen2" from the drop-down menu.
An image is akin to a recipe card for a dish. Just as a recipe card provides all the instructions and ingredients needed to cook a specific dish, an image contains all the instructions and settings required to set up a computer system with a particular operating system and software.
vi. Select the size of your virtual machine from the drop-down menu or choose "See all sizes". Keep the other information as default.
Administrator Account
i. Choose the password authentication type.
In Linux, an SSH (Secure Shell) key provides a secure method for authenticating and connecting to a virtual machine (VM) or remote server. Unlike passwords, which can be vulnerable to attacks, an SSH key uses a pair of cryptographic keys:
Private key (kept confidential): stored on your local machine.
Public key (shared): stored on the remote server or VM.
When connecting to the remote server or VM via SSH, the public key encrypts the connection, while the private key decrypts it. This ensures a secure and encrypted connection.
If SSH keys aren't your preference, you can opt for password authentication when setting up your Linux VM. This method suffices if your VM isn't accessible from the Internet.
ii. Create a username or keep the default name, which serves as the "Admin username".
iii. Select "Generate new key pair".
iv. Name your "Key pair".
Inbound Port Rules
i. In the inbound port rules section, choose “Allow selected ports”.
ii. Select “SSH (22)”. This port enables secure remote access to servers or virtual machines, allowing users to connect and execute commands remotely in a secure manner.
iii. Click on "Next: Disks>" to proceed to the Disk page.
Disk
i. Select "Premium SSD" or your preferred disk type from the OS disk type dropdown menu on the Disk page, leaving the other columns as default.
ii. Click on "Next: Networking>" to proceed to the Networking page.
Network Interface
i. Check the box "Delete public IP and NIC when VM is deleted”. Public IP addresses and NICs persist independently of the virtual machine
Skip to the Tag page, leaving the management, monitoring, and advanced pages unchanged as they are set by default.
Tag
Tags in Azure VMs serve as labels that provide additional information about the resources, enabling better organization, management, and tracking within your Azure environment.
i. Assign a name to your tag.
ii. Click on "Review + create".
Review + create
The "Review + create function in the Azure portal confirms VM settings before deployment. Users review and adjust settings like VM size, disk type, network, and security options. Once confirmed, users create the VM. Errors or misconfigurations can be corrected before finalizing the deployment.
i. Review the summary of your virtual machine configuration, then click "Create" to begin the deployment process.
ii. Click "Download private key pair and create resource" when the "Generate new key pair" window opens.
iii. Your key file will be downloaded as a .pem file. Remember the download location for later use.
iv. After deployment, click "Go to resource”.
SSH (22) Port: Inbound Security Rules
i. Click on the "Resource group" name.
ii. Click on the “Network security group” name.
Inbound Security Rules
iii. Click on “SSH”.
Source
iv. Select "My IP address" from the dropdown menu.
v. Click "Save".
These settings ensure that only you can SSH into this machine.
Connect to Your Linux Virtual Machine
i. Click "Overview".
ii. Click "Connect".
iii. Click "Select" within the "Native SSH" box.
iv. On the right-hand pane, you'll find various connection options. However, for now, simply copy your adminusername@IPaddress into a notepad.
v. Click "Close".
vi. In your computer search box, type “Windows PowerShell”
vii. Click on "Run as Administrator”.
viii. In the PowerShell terminal, type: ssh -i (path to the .pem file) (copied adminusername@ipaddress)
ix. To copy the path: Locate the downloaded .pem file on your computer.
x. Right-click the file.
xi. Select “Copy as path”.
xii. Paste the path into the PowerShell terminal.
xiii. Open Notepad, copy the “adminusername@IPaddress”, and paste it into the PowerShell terminal.
xiv. Press the "Enter" key on your computer.
xv. Type “yes” to validate the host fingerprint.
xvi. Press the “Enter” key on your computer.
Congratulations!
Clean up:
When managing your subscription, consider it similar to maintaining your home. At the end of a project, it's like wrapping up a renovation or DIY task. Just as you wouldn't leave power tools running unnecessarily, it's essential to review and shut down any unused resources. Think of these resources as appliances left on—they continue to draw power and incur costs. You can turn off each appliance individually or switch off the main circuit breaker to cut power to the entire set of resources in one go.
Deleting resources:
i. Navigate to the three-dash menu on the left-hand side of the portal and click "Resource groups".
ii. At the top of the resource group page, click on Delete resource group.
iii. A confirmation page will appear, asking you to enter the resource group name. Type it in and click Delete to complete the process.
This comprehensive guide ensures a seamless and secure setup of a Linux VM, enabling users to harness the full potential of virtualization with confidence.