Skip to content

Linux_azure

Comprehensive Guide to Linux Administration on Azure

Section titled “Comprehensive Guide to Linux Administration on Azure”

Microsoft Azure provides virtual machines running Linux in the cloud. Azure supports most major Linux distributions including Ubuntu, Red Hat, SUSE, Debian, and CentOS.

Azure VM Types
+------------------------------------------------------------------+
| |
| VM Sizes: |
| |
| +---------------------------+----------------------------------+|
| | Family | Examples | Use Case ||
| | ------------|------------|----------------------------------|
| | A | A1-A7 | Basic dev/test ||
| | B | B1s-Bms | Burstable (cost-effective) ||
| | D | D2s-D64s_v3| General purpose ||
| | E | E2s-E64s_v3| Memory optimized ||
| | F | F2s-F64s_v2| Compute optimized ||
| | G | G1-G5 | Storage optimized ||
| | H | H8-H16 | High performance computing ||
| | L | L4s-L32s | Storage (I/O intensive) ||
| | M | M64s-M128s | Large in-memory ||
| | N | N-ND24s | GPU (NVIDIA) ||
| +---------------------------+----------------------------------+|
| |
| Storage: |
| +----------------------------------------------------------+ |
| | OS Disk: Managed disk or storage account | |
| | Data Disks: Up to 32TB per VM | |
| | Temporary Disk: Local SSD (not persistent) | |
| | Azure Files: SMB shares | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
Terminal window
# Install Azure CLI
# Option 1: Script
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
# Option 2: Package manager
# Ubuntu/Debian
curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > /etc/apt/trusted.gpg.d/microsoft.gpg
echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli focal main" | sudo tee /etc/apt/sources.list.d/azure-cli.list
sudo apt update && sudo apt install azure-cli
# RHEL/CentOS
sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo tee /etc/yum.repos.d/azure-cli.repo << EOF
[azure-cli]
name=Azure CLI
baseurl=https://packages.microsoft.com/yumrepos/azure-cli
enabled=1
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc
EOF
sudo dnf install azure-cli
# Sign in
az login
az login --use-device-code
# Set subscription
az account set --subscription "My Subscription"
# Show account info
az account show
Terminal window
# List VMs
az vm list
az vm list --resource-group mygroup
# Create VM
az vm create \
--name myvm \
--resource-group mygroup \
--image UbuntuLTS \
--size Standard_D2s_v3 \
--admin-username azureuser \
--ssh-key-value ~/.ssh/id_rsa.pub
# Create VM with custom image
az vm create \
--name myvm \
--resource-group mygroup \
--image /subscriptions/xxx/resourceGroups/mygroup/providers/Microsoft.Compute/images/myimage \
--size Standard_D2s_v3 \
--admin-username azureuser
# Start VM
az vm start --name myvm --resource-group mygroup
# Stop (deallocate)
az vm stop --name myvm --resource-group mygroup
az vm deallocate --name myvm --resource-group mygroup
# Restart
az vm restart --name myvm --resource-group mygroup
# Resize
az vm resize --name myvm --resource-group mygroup --size Standard_D4s_v3
# Delete
az vm delete --name myvm --resource-group mygroup --yes
# Get instance view
az vm get-instance-view --name myvm --resource-group mygroup
# VM status
az vm show --name myvm --resource-group mygroup
Terminal window
# SSH (using public IP)
ssh azureuser@40.x.x.x.x
# SSH with custom port
ssh -p 2222 azureuser@40.x.x.x.x
# Using Azure Bastion
# From Azure Portal or CLI:
az network bastion create \
--name mybastion \
--resource-group mygroup \
--vnet-name myvnet \
--subnet-name AzureBastionSubnet
# Connect via Bastion
# az network bastion ssh --name mybastion --resource-group mygroup --target-resource-id <vm-resource-id> --username azureuser
# Password authentication (not recommended)
az vm create \
--name myvm \
--resource-group mygroup \
--image UbuntuLTS \
--admin-password 'YourPassword123!'

Azure Storage Types
+------------------------------------------------------------------+
| |
| Managed Disks: |
| +----------------------------------------------------------+ |
| | Ultra Disks | 4GB-64TB, 160K IOPS, 2000 MB/s | |
| | Premium SSD | P1-P80, 4GB-32TB, up to 80K IOPS | |
| | Standard SSD | E1-E80, 4GB-32TB, up to 20K IOPS | |
| | Standard HDD | L1-L80, 4GB-32TB, up to 2K IOPS | |
| +----------------------------------------------------------+ |
| |
| Unmanaged Disks: |
| +----------------------------------------------------------+ |
| | Uses Storage Accounts | |
| | Page Blobs for VHDs | |
| | Less expensive but more management | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
Terminal window
# Create managed disk
az disk create \
--name mydisk \
--resource-group mygroup \
--size-gb 10 \
--sku Standard_LRS
# Attach disk to VM
az vm disk attach \
--vm-name myvm \
--resource-group mygroup \
--name mydisk
# Detach disk
az vm disk detach \
--vm-name myvm \
--resource-group mygroup \
--name mydisk
# Create snapshot
az snapshot create \
--name mysnapshot \
--resource-group mygroup \
--source mydisk
# Create VM from snapshot
az vm create \
--name newvm \
--resource-group mygroup \
--attach-os-disk mysnapshot \
--os-type linux
Terminal window
# Create storage account
az storage account create \
--name mystorageaccount \
--resource-group mygroup \
--sku Standard_LRS \
--kind StorageV2
# Get connection string
az storage account show-connection-string \
--name mystorageaccount \
--resource-group mygroup
# Create container
az storage container create \
--name mycontainer \
--account-name mystorageaccount
# Upload blob
az storage blob upload \
--file /path/to/file.txt \
--container-name mycontainer \
--name blobname.txt \
--account-name mystorageaccount
# Download blob
az storage blob download \
--container-name mycontainer \
--name blobname.txt \
--file /path/to/download.txt \
--account-name mystorageaccount
# List blobs
az storage blob list \
--container-name mycontainer \
--account-name mystorageaccount
# Copy blob
az storage blob copy start \
--destination-container mycontainer \
--destination-blob newfile.txt \
--source-uri https://source.blob.core.windows.net/container/source.txt \
--account-name mystorageaccount
Terminal window
# Create file share
az storage share-rm create \
--name myshare \
--storage-account mystorageaccount
# Install cifs-utils
sudo apt install cifs-utils
# Mount (Linux)
sudo mount -t cifs //mystorageaccount.file.core.windows.net/myshare /mnt/azurefiles \
-o vers=3.0,username=mystorageaccount,password=<storage-key>
# Or using mount.cifs with credential file
# /etc/fstab entry:
//mystorageaccount.file.core.windows.net/myshare /mnt/azurefiles cifs vers=3.0,credentials=/etc/smbcredentials/mystorageaccount.cred 0 0

Terminal window
# Create VNet
az network vnet create \
--name myvnet \
--resource-group mygroup \
--address-prefixes 10.0.0.0/16 \
--subnet-name mysubnet \
--subnet-prefix 10.0.0.0/24
# Create NSG (Network Security Group)
az network nsg create \
--name mynsg \
--resource-group mygroup
# Add NSG rule
az network nsg rule create \
--name allow-ssh \
--nsg-name mynsg \
--resource-group mygroup \
--priority 1000 \
--protocol tcp \
--direction inbound \
--source-address-prefixes '*' \
--source-port-ranges '*' \
--destination-address-prefixes '*' \
--destination-port-ranges 22 \
--access allow
# Associate NSG with subnet
az network vnet subnet update \
--vnet-name myvnet \
--name mysubnet \
--resource-group mygroup \
--network-security-group mynsg
# Create public IP
az network public-ip create \
--name myip \
--resource-group mygroup \
--allocation-method Dynamic
# Create NIC
az network nic create \
--name mynic \
--resource-group mygroup \
--vnet-name myvnet \
--subnet mysubnet \
--public-ip-address myip \
--nsg mynsg

Terminal window
# List subscriptions
az account list -o table
# Show current subscription
az account show
# Create service principal
az ad sp create-for-rbac \
--name myserviceprincipal \
--role Contributor \
--scope /subscriptions/<subscription-id>/resourceGroups/mygroup
# Get service principal credentials
az ad sp credential reset \
--name myserviceprincipal
# Assign role
az role assignment create \
--assignee user@domain.com \
--role Reader \
--resource-group mygroup
# List role assignments
az role assignment list \
--resource-group mygroup
# Azure AD authentication for VMs
az vm identity assign \
--name myvm \
--resource-group mygroup
# Get MSI token
curl -H "Metadata: true" "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com"

Terminal window
# Enable boot diagnostics
az vm boot-diagnostics enable \
--name myvm \
--resource-group mygroup \
--storage https://mystorageaccount.blob.core.windows.net/
# Get boot diagnostics
az vm boot-diagnostics get-boot-log \
--name myvm \
--resource-group mygroup
# View metrics
az monitor metrics list \
--resource /subscriptions/xxx/resourceGroups/mygroup/providers/Microsoft.Compute/virtualMachines/myvm \
--metric-names "Percentage CPU"
# Create alert
az monitor metrics alert create \
--name high-cpu-alert \
--resource-group mygroup \
--condition "avg Percentage CPU > 80" \
--description "CPU usage is above 80%" \
--window-size 5m \
--evaluation-frequency 1m
# Log Analytics
# Install agent on VM
sudo wget https://raw.githubusercontent.com/Microsoft/OMS-Agent-for-Linux/master/installer/scripts/onboard_agent.sh -O onboard_agent.sh
sudo bash onboard_agent.sh -w <workspace-id> -s <workspace-key>

Terminal window
# Create Recovery Services vault
az backup vault create \
--name myvault \
--resource-group mygroup \
--location eastus
# Enable backup for VM
az backup protection enable-for-vm \
--vault-name myvault \
--resource-group mygroup \
--vm myvm
# Trigger backup
az backup protection backup-now \
--vault-name myvault \
--resource-group mygroup \
--container-name myvm \
--item-name myvm
# List backups
az backup job list \
--vault-name myvault \
--resource-group mygroup
# Restore VM
az backup restore restore-disks \
--vault-name myvault \
--resource-group mygroup \
--container-name myvm \
--item-name myvm \
--target-storage-account mystorageaccount \
--backup-storage-recovery-tier Standard

Terminal window
# Register Azure Arc providers
az provider register --namespace Microsoft.HybridCompute
az provider register --namespace Microsoft.GuestConfiguration
# Connect on-premises Linux to Azure Arc
# Download and run script from Azure Portal
azcmagent connect \
--subscription-id <id> \
--resource-group mygroup \
--location eastus \
--tenant-id <tenant> \
--service-principal-app-id <app-id> \
--service-principal-secret <secret> \
--cloud azure
# List Arc-enabled servers
az connectedmachine list \
--resource-group mygroup

  1. What is Azure?

    • Microsoft’s cloud computing platform
  2. How do you connect to an Azure VM?

    • SSH with username and key, or Azure Bastion
  3. What is Azure CLI?

    • Command-line interface for Azure management
  4. What are managed disks?

    • Simplified disk storage managed by Azure
  5. What is Azure Files?

    • Fully managed file shares using SMB protocol
  1. What is the difference between stop and deallocate?

    • Stop: VM stopped but resources allocated; Deallocate: releases resources (no charges)
  2. What is Azure Bastion?

    • Secure RDP/SSH access without public IPs
  3. What are Azure Network Security Groups?

    • Virtual firewall for controlling network traffic
  4. How do you back up Azure VMs?

    • Azure Backup service
  5. What is Azure Monitor?

    • Comprehensive monitoring solution for Azure resources
  1. How do you secure Azure VMs?

    • NSGs, Azure Defender, patching, Azure AD authentication
  2. What is the difference between Premium SSD and Ultra disks?

    • Ultra has higher performance and can be resized without restart
  3. How do you automate Azure VM deployment?

    • ARM templates, Terraform, Azure CLI scripts
  4. What is Azure Arc?

    • Hybrid cloud management extending Azure to on-premises
  5. How do you monitor Azure VM performance?

    • Azure Monitor, VM insights, Log Analytics

Quick Reference
+------------------------------------------------------------------+
| |
| Azure CLI: |
| +----------------------------------------------------------+ |
| | az login | Sign in | |
| | az vm list | List VMs | |
| | az vm create | Create VM | |
| | az vm start/stop | Control VM | |
| +----------------------------------------------------------+ |
| |
| Storage: |
| +----------------------------------------------------------+ |
| | az storage account create | Create storage | |
| | az storage blob upload | Upload to blob | |
| | az disk create | Create managed disk | |
| +----------------------------------------------------------+ |
| |
| Networking: |
| +----------------------------------------------------------+ |
| | az network vnet create | Create VNet | |
| | az network nsg create | Create NSG | |
| | az network public-ip create | Create public IP | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+