Skip to content

Commit 1fa678b

Browse files
committed
added deploy to azure function and cleaned up language
1 parent c0d18a4 commit 1fa678b

File tree

1 file changed

+83
-114
lines changed

1 file changed

+83
-114
lines changed
Lines changed: 83 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,135 @@
11
---
22
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
3+
description: Create a Linux virtual machine using the Azure CLI
4+
author: chasecrum
55
ms.service: virtual-machines
66
ms.collection: linux
77
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.custom: mvc, devx-track-azurecli, mode-api, innovation-engine, linux-related-content
1111
---
1212

13-
# Quickstart: Create a Linux virtual machine with the Azure CLI
13+
# Create a Linux VM on Azure
1414

15-
**Applies to:** :heavy_check_mark: Linux VMs
15+
[![Deploy to Azure](https://aka.ms/deploytoazurebutton)](https://portal.azure.com/?Microsoft_Azure_CloudNative_clientoptimizations=false&feature.canmodifyextensions=true#view/Microsoft_Azure_CloudNative/SubscriptionSelectionPage.ReactView/tutorialKey/CreateLinuxVMAndSSH)
1616

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.
17+
## Define environment variables
1818

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.
19+
The First step is to define the environment variables.
2020

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
21+
```bash
22+
export RANDOM_ID="$(openssl rand -hex 3)"
23+
export MY_RESOURCE_GROUP_NAME="myVMResourceGroup$RANDOM_ID"
24+
export REGION=EastUS
25+
export MY_VM_NAME="myVM$RANDOM_ID"
26+
export MY_USERNAME=azureuser
27+
export MY_VM_IMAGE="Canonical:0001-com-ubuntu-minimal-jammy:minimal-22_04-lts-gen2:latest"
28+
```
2429

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.
30+
## Login to Azure using the CLI
2631

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.
32+
In order to run commands in Azure using the CLI you'll need to login first. This is done using the `az login` command.
2833

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).
34+
## Create a resource group
3035

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:
36+
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.
3337

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
38+
```bash
39+
az group create --name $MY_RESOURCE_GROUP_NAME --location $REGION
4040
```
4141

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.
42+
Results:
4543

46-
```azurecli-interactive
47-
az group create --name $RESOURCE_GROUP_NAME --location $LOCATION
44+
<!-- expected_similarity=0.3 -->
45+
```json
46+
{
47+
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myVMResourceGroup",
48+
"location": "eastus",
49+
"managedBy": null,
50+
"name": "myVMResourceGroup",
51+
"properties": {
52+
"provisioningState": "Succeeded"
53+
},
54+
"tags": null,
55+
"type": "Microsoft.Resources/resourceGroups"
56+
}
4857
```
4958

50-
## Create virtual machine
59+
## Create the virtual machine
5160

52-
Create a VM with the [az vm create](/cli/azure/vm) command.
61+
To create a VM in this resource group we need to use the `vm create` command. In the code example below, we have provided the `--generate-ssh-keys` flag, which will cause the CLI to look for an available ssh key in `~/.ssh`. If one is found, it will be used. If not, one will be 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.
5362

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.
63+
All other values are configured using environment variables.
5564

56-
```azurecli-interactive
65+
```bash
5766
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
67+
--resource-group $MY_RESOURCE_GROUP_NAME \
68+
--name $MY_VM_NAME \
69+
--image $MY_VM_IMAGE \
70+
--admin-username $MY_USERNAME \
71+
--assign-identity \
72+
--generate-ssh-keys \
73+
--public-ip-sku Standard
6474
```
6575

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-->
76+
Results:
77+
78+
<!-- expected_similarity=0.3 -->
6879
```json
6980
{
7081
"fqdns": "",
71-
"id": "/subscriptions/<guid>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM",
82+
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM",
7283
"location": "eastus",
73-
"macAddress": "00-0D-3A-23-9A-49",
84+
"macAddress": "00-0D-3A-10-4F-70",
7485
"powerState": "VM running",
7586
"privateIpAddress": "10.0.0.4",
76-
"publicIpAddress": "40.68.254.142",
77-
"resourceGroup": "myResourceGroup"
87+
"publicIpAddress": "52.147.208.85",
88+
"resourceGroup": "myVMResourceGroup",
89+
"zones": ""
7890
}
7991
```
8092

81-
Make a note of the `publicIpAddress` to use later.
93+
## Enable Azure AD login for a Linux virtual machine in Azure
8294

83-
You can retrieve and store the IP address in the variable IP_ADDRESS with the following command:
95+
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.
8496

85-
```azurecli-interactive
86-
export IP_ADDRESS=$(az vm show --show-details --resource-group $RESOURCE_GROUP_NAME --name $VM_NAME --query publicIps --output tsv)
97+
```bash
98+
az vm extension set \
99+
--publisher Microsoft.Azure.ActiveDirectory \
100+
--name AADSSHLoginForLinux \
101+
--resource-group $MY_RESOURCE_GROUP_NAME \
102+
--vm-name $MY_VM_NAME
87103
```
88104

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).
105+
## Store IP address of VM in order to SSH
90106

91-
## Install web server
107+
Run the following command to store the IP Address of the VM as an environment variable:
92108

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"
109+
```bash
110+
export IP_ADDRESS=$(az vm show --show-details --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_VM_NAME --query publicIps --output tsv)
101111
```
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
111112

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:
113+
## SSH into the VM
113114

114-
![Screenshot showing the N G I N X default web page.](./media/quick-create-cli/nginix-welcome-page-debian.png)
115+
<!--## Export the SSH configuration for use with SSH clients that support OpenSSH & SSH into the VM.
116+
Login 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:-->
115117

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>
118+
<!--
119+
```bash
120+
yes | az ssh config --file ~/.ssh/config --name $MY_VM_NAME --resource-group $MY_RESOURCE_GROUP_NAME
150121
```
122+
-->
151123

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.
124+
You can now SSH into the VM by running the output of the following command in your ssh client of choice
155125

156-
```azurecli-interactive
157-
az group delete --name $RESOURCE_GROUP_NAME --no-wait --yes --verbose
126+
```bash
127+
ssh -o StrictHostKeyChecking=no $MY_USERNAME@$IP_ADDRESS
158128
```
159129

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-
130+
## Next Steps
164131

165-
> [!div class="nextstepaction"]
166-
> [Azure Linux virtual machine tutorials](./tutorial-manage-vm.md)
132+
* [VM Documentation](https://learn.microsoft.com/azure/virtual-machines/)
133+
* [Use Cloud-Init to initialize a Linux VM on first boot](https://learn.microsoft.com/azure/virtual-machines/linux/tutorial-automate-vm-deployment)
134+
* [Create custom VM images](https://learn.microsoft.com/azure/virtual-machines/linux/tutorial-custom-images)
135+
* [Load Balance VMs](https://learn.microsoft.com/azure/load-balancer/quickstart-load-balancer-standard-public-cli)

0 commit comments

Comments
 (0)