|
1 | 1 | ---
|
2 |
| -title: 'Quickstart: Use the Azure CLI to create a Linux VM' |
3 |
| -description: In this quickstart, you learn how to use the Azure CLI to create a Linux virtual machine |
4 |
| -author: ju-shim |
| 2 | +title: 'Quickstart: Use the Azure CLI to create a Linux Virtual Machine' |
| 3 | +description: Create a Linux virtual machine using the Azure CLI. |
| 4 | +author: chasecrum |
5 | 5 | ms.service: virtual-machines
|
6 | 6 | ms.collection: linux
|
7 | 7 | ms.topic: quickstart
|
8 |
| -ms.date: 06/01/2022 |
9 |
| -ms.author: jushiman |
10 |
| -ms.custom: mvc, devx-track-azurecli, mode-api |
| 8 | +ms.date: 02/28/2024 |
| 9 | +ms.author: chasecrum |
| 10 | +ms.reviewer: jushiman |
| 11 | +ms.custom: mvc, devx-track-azurecli, mode-api, innovation-engine, linux-related-content |
11 | 12 | ---
|
12 | 13 |
|
13 |
| -# Quickstart: Create a Linux virtual machine with the Azure CLI |
| 14 | +# Create a Linux virtual machine on Azure |
14 | 15 |
|
15 |
| -**Applies to:** :heavy_check_mark: Linux VMs |
| 16 | +[](https://portal.azure.com/?Microsoft_Azure_CloudNative_clientoptimizations=false&feature.canmodifyextensions=true#view/Microsoft_Azure_CloudNative/SubscriptionSelectionPage.ReactView/tutorialKey/CreateLinuxVMAndSSH) |
16 | 17 |
|
17 |
| -This quickstart shows you how to use the Azure CLI to deploy a Linux virtual machine (VM) in Azure. The Azure CLI is used to create and manage Azure resources via either the command line or scripts. |
| 18 | +## Define environment variables |
18 | 19 |
|
19 |
| -In this tutorial, we will be installing the latest Debian image. To show the VM in action, you'll connect to it using SSH and install the NGINX web server. |
| 20 | +The First step is to define the environment variables. |
20 | 21 |
|
21 |
| -If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
22 |
| - |
23 |
| -## Launch Azure Cloud Shell |
| 22 | +```bash |
| 23 | +export RANDOM_ID="$(openssl rand -hex 3)" |
| 24 | +export MY_RESOURCE_GROUP_NAME="myVMResourceGroup$RANDOM_ID" |
| 25 | +export REGION=EastUS |
| 26 | +export MY_VM_NAME="myVM$RANDOM_ID" |
| 27 | +export MY_USERNAME=azureuser |
| 28 | +export MY_VM_IMAGE="Canonical:0001-com-ubuntu-minimal-jammy:minimal-22_04-lts-gen2:latest" |
| 29 | +``` |
24 | 30 |
|
25 |
| -The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. It has common Azure tools preinstalled and configured to use with your account. |
| 31 | +## Log in to Azure using the CLI |
26 | 32 |
|
27 |
| -To open the Cloud Shell, just select **Try it** from the upper right corner of a code block. You can also open Cloud Shell in a separate browser tab by going to [https://shell.azure.com/bash](https://shell.azure.com/bash). Select **Copy** to copy the blocks of code, paste it into the Cloud Shell, and select **Enter** to run it. |
| 33 | +In order to run commands in Azure using the CLI, you need to log in first. This is done using the `az login` command. |
28 | 34 |
|
29 |
| -If you prefer to install and use the CLI locally, this quickstart requires Azure CLI version 2.0.30 or later. Run `az --version` to find the version. If you need to install or upgrade, see [Install Azure CLI]( /cli/azure/install-azure-cli). |
| 35 | +## Create a resource group |
30 | 36 |
|
31 |
| -## Define Environment Variables |
32 |
| -Environment variables are commonly used in Linux to centralize configuration data to improve consistency and maintainability of the system. Create the following environment variables to specify the names of resources that will be created later in this tutorial: |
| 37 | +A resource group is a container for related resources. All resources must be placed in a resource group. The following command creates a resource group with the previously defined $MY_RESOURCE_GROUP_NAME and $REGION parameters. |
33 | 38 |
|
34 |
| -```azurecli-interactive |
35 |
| -export RESOURCE_GROUP_NAME=myResourceGroup |
36 |
| -export LOCATION=eastus |
37 |
| -export VM_NAME=myVM |
38 |
| -export VM_IMAGE=debian |
39 |
| -export ADMIN_USERNAME=azureuser |
| 39 | +```bash |
| 40 | +az group create --name $MY_RESOURCE_GROUP_NAME --location $REGION |
40 | 41 | ```
|
41 | 42 |
|
42 |
| -## Create a resource group |
43 |
| - |
44 |
| -Create a resource group with the [az group create](/cli/azure/group) command. An Azure resource group is a logical container into which Azure resources are deployed and managed. |
| 43 | +Results: |
45 | 44 |
|
46 |
| -```azurecli-interactive |
47 |
| -az group create --name $RESOURCE_GROUP_NAME --location $LOCATION |
| 45 | +<!-- expected_similarity=0.3 --> |
| 46 | +```json |
| 47 | +{ |
| 48 | + "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myVMResourceGroup", |
| 49 | + "location": "eastus", |
| 50 | + "managedBy": null, |
| 51 | + "name": "myVMResourceGroup", |
| 52 | + "properties": { |
| 53 | + "provisioningState": "Succeeded" |
| 54 | + }, |
| 55 | + "tags": null, |
| 56 | + "type": "Microsoft.Resources/resourceGroups" |
| 57 | +} |
48 | 58 | ```
|
49 | 59 |
|
50 |
| -## Create virtual machine |
| 60 | +## Create the virtual machine |
51 | 61 |
|
52 |
| -Create a VM with the [az vm create](/cli/azure/vm) command. |
| 62 | +To create a VM in this resource group, we need to use the `vm create` command. In the following code example, we provided the `--generate-ssh-keys` flag, which causes the CLI to look for an available ssh key in `~/.ssh`. If one is found, it is used. If not, one is generated and stored in `~/.ssh`. We also provide the `--public-ip-sku Standard` flag to ensure that the machine is accessible via a public IP address. Finally, we deploy the latest `Ubuntu 22.04` image. |
53 | 63 |
|
54 |
| -The following example creates a VM and adds a user account. The `--generate-ssh-keys` parameter is used to automatically generate an SSH key, and put it in the default key location (*~/.ssh*). To use a specific set of keys instead, use the `--ssh-key-values` option. |
| 64 | +All other values are configured using environment variables. |
55 | 65 |
|
56 |
| -```azurecli-interactive |
| 66 | +```bash |
57 | 67 | az vm create \
|
58 |
| - --resource-group $RESOURCE_GROUP_NAME \ |
59 |
| - --name $VM_NAME \ |
60 |
| - --image $VM_IMAGE \ |
61 |
| - --admin-username $ADMIN_USERNAME \ |
62 |
| - --generate-ssh-keys \ |
63 |
| - --public-ip-sku Standard |
| 68 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 69 | + --name $MY_VM_NAME \ |
| 70 | + --image $MY_VM_IMAGE \ |
| 71 | + --admin-username $MY_USERNAME \ |
| 72 | + --assign-identity \ |
| 73 | + --generate-ssh-keys \ |
| 74 | + --public-ip-sku Standard |
64 | 75 | ```
|
65 | 76 |
|
66 |
| -It takes a few minutes to create the VM and supporting resources. The following example output shows the VM create operation was successful. |
67 |
| -<!--expected_similarity=0.18--> |
| 77 | +Results: |
| 78 | + |
| 79 | +<!-- expected_similarity=0.3 --> |
68 | 80 | ```json
|
69 | 81 | {
|
70 | 82 | "fqdns": "",
|
71 |
| - "id": "/subscriptions/<guid>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM", |
| 83 | + "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM", |
72 | 84 | "location": "eastus",
|
73 |
| - "macAddress": "00-0D-3A-23-9A-49", |
| 85 | + "macAddress": "00-0D-3A-10-4F-70", |
74 | 86 | "powerState": "VM running",
|
75 | 87 | "privateIpAddress": "10.0.0.4",
|
76 |
| - "publicIpAddress": "40.68.254.142", |
77 |
| - "resourceGroup": "myResourceGroup" |
| 88 | + "publicIpAddress": "52.147.208.85", |
| 89 | + "resourceGroup": "myVMResourceGroup", |
| 90 | + "zones": "" |
78 | 91 | }
|
79 | 92 | ```
|
80 | 93 |
|
81 |
| -Make a note of the `publicIpAddress` to use later. |
| 94 | +## Enable Azure AD Login for a Linux virtual machine in Azure |
82 | 95 |
|
83 |
| -You can retrieve and store the IP address in the variable IP_ADDRESS with the following command: |
| 96 | +The following code example deploys a Linux VM and then installs the extension to enable an Azure AD Login for a Linux VM. VM extensions are small applications that provide post-deployment configuration and automation tasks on Azure virtual machines. |
84 | 97 |
|
85 |
| -```azurecli-interactive |
86 |
| -export IP_ADDRESS=$(az vm show --show-details --resource-group $RESOURCE_GROUP_NAME --name $VM_NAME --query publicIps --output tsv) |
| 98 | +```bash |
| 99 | +az vm extension set \ |
| 100 | + --publisher Microsoft.Azure.ActiveDirectory \ |
| 101 | + --name AADSSHLoginForLinux \ |
| 102 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 103 | + --vm-name $MY_VM_NAME |
87 | 104 | ```
|
88 | 105 |
|
89 |
| -Cost information isn't presented during the virtual machine creation process for CLI like it is for the [Azure portal](quick-create-portal.md). If you want to learn more about how cost works for virtual machines, see the [Cost optimization Overview page](../plan-to-manage-costs.md). |
| 106 | +## Store IP address of VM in order to SSH |
90 | 107 |
|
91 |
| -## Install web server |
| 108 | +Run the following command to store the IP Address of the VM as an environment variable: |
92 | 109 |
|
93 |
| -To see your VM in action, install the NGINX web server. Update your package sources and then install the latest NGINX package. The following command uses run-command to run `sudo apt-get update && sudo apt-get install -y nginx` on the VM: |
94 |
| - |
95 |
| -```azurecli-interactive |
96 |
| -az vm run-command invoke \ |
97 |
| - --resource-group $RESOURCE_GROUP_NAME \ |
98 |
| - --name $VM_NAME \ |
99 |
| - --command-id RunShellScript \ |
100 |
| - --scripts "sudo apt-get update && sudo apt-get install -y nginx" |
| 110 | +```bash |
| 111 | +export IP_ADDRESS=$(az vm show --show-details --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_VM_NAME --query publicIps --output tsv) |
101 | 112 | ```
|
102 |
| -## Open port 80 for web traffic |
103 |
| - |
104 |
| -By default, only SSH connections are opened when you create a Linux VM in Azure. Use [az vm open-port](/cli/azure/vm) to open TCP port 80 for use with the NGINX web server: |
105 |
| - |
106 |
| -```azurecli-interactive |
107 |
| -az vm open-port --port 80 --resource-group $RESOURCE_GROUP_NAME --name $VM_NAME |
108 |
| -``` |
109 |
| - |
110 |
| -## View the web server in action |
111 | 113 |
|
112 |
| -Use a web browser of your choice to view the default NGINX welcome page. Use the public IP address of your VM as the web address. The following example shows the default NGINX web site: |
| 114 | +## SSH into the VM |
113 | 115 |
|
114 |
| - |
| 116 | +<!--## Export the SSH configuration for use with SSH clients that support OpenSSH & SSH into the VM. |
| 117 | +Log in to Azure Linux VMs with Azure AD supports exporting the OpenSSH certificate and configuration. That means you can use any SSH clients that support OpenSSH-based certificates to sign in through Azure AD. The following example exports the configuration for all IP addresses assigned to the VM:--> |
115 | 118 |
|
116 |
| -Alternatively, run the following command to see the NGINX welcome page in the terminal |
117 |
| - |
118 |
| -```azurecli-interactive |
119 |
| - curl $IP_ADDRESS |
120 |
| -``` |
121 |
| - |
122 |
| -The following example shows the default NGINX web site in the terminal as successful output: |
123 |
| -<!--expected_similarity=0.8--> |
124 |
| -```html |
125 |
| -<!DOCTYPE html> |
126 |
| -<html> |
127 |
| -<head> |
128 |
| -<title>Welcome to nginx!</title> |
129 |
| -<style> |
130 |
| - body { |
131 |
| - width: 35em; |
132 |
| - margin: 0 auto; |
133 |
| - font-family: Tahoma, Verdana, Arial, sans-serif; |
134 |
| - } |
135 |
| -</style> |
136 |
| -</head> |
137 |
| -<body> |
138 |
| -<h1>Welcome to nginx!</h1> |
139 |
| -<p>If you see this page, the nginx web server is successfully installed and |
140 |
| -working. Further configuration is required.</p> |
141 |
| - |
142 |
| -<p>For online documentation and support please refer to |
143 |
| -<a href="http://nginx.org/">nginx.org</a>.<br/> |
144 |
| -Commercial support is available at |
145 |
| -<a href="http://nginx.com/">nginx.com</a>.</p> |
146 |
| - |
147 |
| -<p><em>Thank you for using nginx.</em></p> |
148 |
| -</body> |
149 |
| -</html> |
| 119 | +<!-- |
| 120 | +```bash |
| 121 | +yes | az ssh config --file ~/.ssh/config --name $MY_VM_NAME --resource-group $MY_RESOURCE_GROUP_NAME |
150 | 122 | ```
|
| 123 | +--> |
151 | 124 |
|
152 |
| -## Clean up resources |
153 |
| - |
154 |
| -When no longer needed, you can use the [az group delete](/cli/azure/group) command to remove the resource group, VM, and all related resources. |
| 125 | +You can now SSH into the VM by running the output of the following command in your ssh client of choice: |
155 | 126 |
|
156 |
| -```azurecli-interactive |
157 |
| -az group delete --name $RESOURCE_GROUP_NAME --no-wait --yes --verbose |
| 127 | +```bash |
| 128 | +ssh -o StrictHostKeyChecking=no $MY_USERNAME@$IP_ADDRESS |
158 | 129 | ```
|
159 | 130 |
|
160 |
| -## Next steps |
161 |
| - |
162 |
| -In this quickstart, you deployed a simple virtual machine, opened a network port for web traffic, and installed a basic web server. To learn more about Azure virtual machines, continue to the tutorial for Linux VMs. |
163 |
| - |
| 131 | +## Next Steps |
164 | 132 |
|
165 |
| -> [!div class="nextstepaction"] |
166 |
| -> [Azure Linux virtual machine tutorials](./tutorial-manage-vm.md) |
| 133 | +* [Use Cloud-Init to initialize a Linux VM on first boot](tutorial-automate-vm-deployment.md) |
| 134 | +* [Create custom VM images](tutorial-custom-images.md) |
| 135 | +* [Load Balance VMs](../../load-balancer/quickstart-load-balancer-standard-public-cli.md) |
0 commit comments