Create a Linux Virtual Machine in Azure with a Custom VNet

This guide provides step-by-step instructions for creating a Linux virtual machine (VM) within a custom Azure Virtual Network (VNet). Utilizing a custom VNet offers enhanced control over your network topology, IP addressing, and security configurations.

Note: This article assumes you have an Azure account. If you don't, you can create a free account.

Prerequisites

Step 1: Create a Resource Group

A resource group is a logical container that holds related Azure resources. Create a new resource group for your VM and its associated resources.


az group create --name myResourceGroup --location eastus
            

Step 2: Create a Custom Virtual Network and Subnet

We'll create a virtual network with a custom address space and a subnet within it. This VNet will isolate your VM from other networks.


az network vnet create \
    --resource-group myResourceGroup \
    --name myCustomVNet \
    --address-prefix 10.0.0.0/16 \
    --subnet-name mySubnet \
    --subnet-prefix 10.0.1.0/24
            

In this command:

Step 3: Create a Network Security Group (NSG)

A Network Security Group acts as a virtual firewall, allowing or denying network traffic to your VM. We'll create an NSG and allow SSH traffic.


az network nsg create \
    --resource-group myResourceGroup \
    --name myNsg \
    --location eastus

az network nsg rule create \
    --resource-group myResourceGroup \
    --nsg-name myNsg \
    --name AllowSSH \
    --protocol tcp \
    --priority 100 \
    --destination-port-range 22 \
    --access Allow
            

Step 4: Create a Network Interface Card (NIC)

The NIC connects your VM to the virtual network. It will be associated with the VNet, subnet, and NSG we created.


az network nic create \
    --resource-group myResourceGroup \
    --name myNic \
    --vnet-name myCustomVNet \
    --subnet mySubnet \
    --network-security-group myNsg \
    --location eastus
            

Step 5: Create the Linux Virtual Machine

Now, create the Linux VM and attach the NIC to it. We'll use Ubuntu LTS as an example.


az vm create \
    --resource-group myResourceGroup \
    --name myLinuxVM \
    --nics myNic \
    --image UbuntuLTS \
    --admin-username azureuser \
    --generate-ssh-keys
            

This command:

Tip: To use an existing SSH public key, use the --ssh-key-values parameter instead of --generate-ssh-keys.

Step 6: Connect to the Virtual Machine

Once the VM is created, you can connect to it using SSH. First, get the public IP address of the VM:


az vm show --resource-group myResourceGroup --name myLinuxVM --show-details --query publicIps --output tsv
            

Then, connect using SSH:


ssh azureuser@<PUBLIC_IP_ADDRESS>
            

Replace <PUBLIC_IP_ADDRESS> with the IP address obtained in the previous step.

Step 7: Clean Up Resources (Optional)

To avoid ongoing charges, you can delete the resource group and all its resources when you are finished.


az group delete --name myResourceGroup --yes --no-wait
            
Important: Deleting the resource group is a permanent action and cannot be undone.

Next Steps

Explore more advanced Azure VM configurations, such as: