|
| 1 | +--- |
| 2 | +title: 'Quickstart: Create a Linux VM and SSH into it' |
| 3 | +description: Learn how to create a Linux virtual machine in Azure and SSH into it using Azure CLI commands within an Exec Doc. |
| 4 | +ms.topic: quickstart |
| 5 | +ms.date: 10/12/2023 |
| 6 | +author: azureuser123 |
| 7 | +ms.author: azureuser123 |
| 8 | +ms.custom: innovation-engine, azurecli, linux-related-content |
| 9 | +--- |
| 10 | + |
| 11 | +In this Exec Doc, you will learn how to create a Linux virtual machine (VM) in Azure using the Azure CLI and then SSH into the newly created VM. All commands have been configured to run non-interactively. Remember to update any variables if necessary and note that resources are appended with a random suffix to avoid naming conflicts in subsequent runs. |
| 12 | + |
| 13 | +## Step 1: Set Environment Variables |
| 14 | + |
| 15 | +In this step, we declare the environment variables that will be used throughout the Exec Doc. A random suffix is appended to the resource group and VM names to ensure uniqueness. |
| 16 | + |
| 17 | +```bash |
| 18 | +export RANDOM_SUFFIX=$(openssl rand -hex 3) |
| 19 | +export REGION="WestUS2" |
| 20 | +export MY_RESOURCE_GROUP_NAME="MyResourceGroup$RANDOM_SUFFIX" |
| 21 | +export MY_VM_NAME="MyLinuxVM$RANDOM_SUFFIX" |
| 22 | +``` |
| 23 | + |
| 24 | +## Step 2: Create a Resource Group |
| 25 | + |
| 26 | +This step creates a new resource group in the specified region. |
| 27 | + |
| 28 | +```bash |
| 29 | +az group create --name $MY_RESOURCE_GROUP_NAME --location $REGION |
| 30 | +``` |
| 31 | + |
| 32 | +Results: |
| 33 | + |
| 34 | +<!-- expected_similarity=0.3 --> |
| 35 | + |
| 36 | +```JSON |
| 37 | +{ |
| 38 | + "id": "/subscriptions/xxxxx-xxxxx-xxxxx-xxxxx/resourceGroups/MyResourceGroupxxxxx", |
| 39 | + "location": "WestUS2", |
| 40 | + "managedBy": null, |
| 41 | + "name": "MyResourceGroupxxxxx", |
| 42 | + "properties": { |
| 43 | + "provisioningState": "Succeeded" |
| 44 | + }, |
| 45 | + "tags": null, |
| 46 | + "type": "Microsoft.Resources/resourceGroups" |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +## Step 3: Create a Linux Virtual Machine |
| 51 | + |
| 52 | +Now you will create a Linux VM using the Ubuntu LTS image. The command includes the generation of SSH keys if they do not already exist. The default administrator username is set to "azureuser". |
| 53 | + |
| 54 | +```bash |
| 55 | +az vm create --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_VM_NAME --image UbuntuLTS --admin-username azureuser --generate-ssh-keys |
| 56 | +``` |
| 57 | + |
| 58 | +Results: |
| 59 | + |
| 60 | +<!-- expected_similarity=0.3 --> |
| 61 | + |
| 62 | +```JSON |
| 63 | +{ |
| 64 | + "fqdns": "", |
| 65 | + "id": "/subscriptions/xxxxx-xxxxx-xxxxx-xxxxx/resourceGroups/MyResourceGroupxxxxx/providers/Microsoft.Compute/virtualMachines/MyLinuxVMxxxxx", |
| 66 | + "location": "WestUS2", |
| 67 | + "name": "MyLinuxVMxxxxx", |
| 68 | + "powerState": "VM running", |
| 69 | + "privateIpAddress": "10.0.0.4", |
| 70 | + "publicIpAddress": "52.xxx.xxx.xxx" |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +## Step 4: Retrieve the VM's Public IP Address |
| 75 | + |
| 76 | +Next, you retrieve the public IP address of the newly created VM. This value is stored in an environment variable for later use in the SSH command. |
| 77 | + |
| 78 | +```bash |
| 79 | +export PUBLIC_IP=$(az vm show -d --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_VM_NAME --query publicIps -o tsv) |
| 80 | +``` |
| 81 | + |
| 82 | +## Step 5: SSH into the VM |
| 83 | + |
| 84 | +Finally, use the SSH command to connect to your Linux VM. The SSH command includes the option to disable strict host key checking to avoid interactive prompts during the first connection. |
| 85 | + |
| 86 | +```bash |
| 87 | +ssh -o StrictHostKeyChecking=no azureuser@$PUBLIC_IP |
| 88 | +``` |
| 89 | + |
| 90 | +This SSH command connects you to the VM using the default administrator account "azureuser". Once connected, you can start managing your Linux environment directly. |
| 91 | + |
| 92 | +Happy learning and exploring your new Linux VM in Azure! |
0 commit comments