Deploy and Connect to a Linux VM
This quickstart guide will walk you through the process of creating your first Azure Virtual Machine (VM) using either the Azure portal or the Azure Command-Line Interface (CLI). You'll then learn how to connect to it.
Azure Virtual Machines offer on-demand, scalable computing resources. You can deploy Windows or Linux VMs to run your applications.
Prerequisites
Before you begin, ensure you have the following:
- An active Azure account. If you don't have one, you can sign up for a free trial.
- If using the Azure CLI, you'll need to install it on your local machine or use the Azure Cloud Shell.
Create a Virtual Machine
You can create a VM using several methods. We'll cover the most common ones here.
Using the Azure Portal
The Azure portal provides a user-friendly graphical interface for managing your Azure resources.
Sign in to the Azure portal
Navigate to https://portal.azure.com/ and sign in with your Azure account credentials.
Create a resource
On the Azure portal menu, select Create a resource. In the search box, type Virtual machine and then select Virtual machine from the results.
Configure the virtual machine
On the Basics tab of the Create a virtual machine page, do the following:
- Subscription: Select your Azure subscription.
- Resource group: Select Create new and enter a name for your resource group (e.g.,
myResourceGroup). - Virtual machine name: Enter a name for your VM (e.g.,
myVM). - Region: Select a region close to you (e.g.,
East US). - Availability options: Leave as No infrastructure redundancy required for this quickstart.
- Security type: Leave as Standard.
- Image: Select Ubuntu Server 22.04 LTS (or your preferred Linux distribution).
- Size: Leave the default size for now.
- Authentication type: Select SSH public key.
- Username: Enter a username (e.g.,
azureuser). - SSH public key source: Select Generate new key pair.
- Key pair name: Enter a name for your key pair (e.g.,
myKey).
Click Next: Disks >.
Configure networking
On the Networking tab, ensure Virtual network, Subnet, Public IP, and NIC network security group are configured appropriately. For this quickstart, the defaults are usually fine. Ensure HTTP and SSH are allowed inbound ports if you plan to run web servers.
Click Next: Management >, then Next: Advanced >, then Next: Tags >.
Review and create
On the Review + create tab, review your VM configuration. If everything looks correct, click Create.
You will be prompted to download your private key. Save this file securely as it's required to connect to your VM.
Using the Azure CLI
The Azure CLI allows you to manage Azure resources from your command line.
First, log in to your Azure account:
az login
Then, create a resource group (if you don't have one):
az group create --name myResourceGroup --location eastus
Now, create the virtual machine. This command creates a VM named myVM with an SSH public key named myKey in the myResourceGroup resource group. It also creates the public IP address and network security group.
az vm create \
--resource-group myResourceGroup \
--name myVM \
--image UbuntuLTS \
--admin-username azureuser \
--generate-ssh-keys
The output will include the public IP address of your VM.
Connect to Your VM
Once your VM is deployed, you can connect to it using SSH.
Replace <publicIpAddress> with the public IP address of your VM and ~/.ssh/id_rsa with the path to your private key file.
ssh azureuser@<publicIpAddress> -i ~/.ssh/myKey
When prompted, type yes to continue if this is the first time you're connecting.
Clean Up Resources
To avoid ongoing charges, you should delete the resource group and all resources associated with it when you no longer need them.
Using the Azure Portal:
- In the Azure portal, search for and select Resource groups.
- Select the
myResourceGroupresource group from the list. - On the resource group page, click Delete resource group.
- Enter the resource group name to confirm, then click Delete.
Using the Azure CLI:
az group delete --name myResourceGroup --yes --no-wait