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.
Prerequisites
- An Azure subscription.
- Azure CLI installed and configured, or access to the Azure Cloud Shell.
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:
--name myCustomVNet
: The name of your virtual network.--address-prefix 10.0.0.0/16
: The address space for the VNet.--subnet-name mySubnet
: The name of the subnet.--subnet-prefix 10.0.1.0/24
: The address prefix for the subnet.
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:
--name myLinuxVM
: The name of your virtual machine.--image UbuntuLTS
: Specifies the VM image. You can choose other distributions like CentOS, Debian, etc.--admin-username azureuser
: The administrator username for the VM.--generate-ssh-keys
: Automatically creates an SSH key pair for secure access. The public key will be stored in~/.ssh/id_rsa.pub
.
--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
Next Steps
Explore more advanced Azure VM configurations, such as: