|
1 |
| -# Create a Linux VM and SSH On Azure |
2 |
| - |
3 |
| -## Define Environment Variables |
4 |
| - |
5 |
| -The First step in this tutorial is to define environment variables. |
6 |
| - |
7 |
| -```bash |
8 |
| -export RANDOM_ID="$(openssl rand -hex 3)" |
9 |
| -export MY_RESOURCE_GROUP_NAME="myVMResourceGroup$RANDOM_ID" |
10 |
| -export REGION=EastUS |
11 |
| -export MY_VM_NAME="myVM$RANDOM_ID" |
12 |
| -export MY_USERNAME=azureuser |
13 |
| -export MY_VM_IMAGE="Canonical:0001-com-ubuntu-minimal-jammy:minimal-22_04-lts-gen2:latest" |
14 |
| -``` |
15 |
| - |
16 |
| -# Login to Azure using the CLI |
17 |
| - |
18 |
| -In order to run commands against Azure using the CLI you need to login. This is done, very simply, though the `az login` command: |
19 |
| - |
20 |
| -# Create a resource group |
21 |
| - |
22 |
| -A resource group is a container for related resources. All resources must be placed in a resource group. We will create one for this tutorial. The following command creates a resource group with the previously defined $MY_RESOURCE_GROUP_NAME and $REGION parameters. |
23 |
| - |
24 |
| -```bash |
25 |
| -az group create --name $MY_RESOURCE_GROUP_NAME --location $REGION |
26 |
| -``` |
27 |
| - |
28 |
| -Results: |
29 |
| - |
30 |
| -<!-- expected_similarity=0.3 --> |
31 |
| -```json |
32 |
| -{ |
33 |
| - "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myVMResourceGroup", |
34 |
| - "location": "eastus", |
35 |
| - "managedBy": null, |
36 |
| - "name": "myVMResourceGroup", |
37 |
| - "properties": { |
38 |
| - "provisioningState": "Succeeded" |
39 |
| - }, |
40 |
| - "tags": null, |
41 |
| - "type": "Microsoft.Resources/resourceGroups" |
42 |
| -} |
43 |
| -``` |
44 |
| - |
45 |
| -## Create the Virtual Machine |
46 |
| - |
47 |
| -To create a VM in this resource group we need to run a simple command, here we have provided the `--generate-ssh-keys` flag, this will cause the CLI to look for an avialable ssh key in `~/.ssh`, if one is found it will be used, otherwise 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. Finally, we are deploying the latest `Ubuntu 22.04` image. |
48 |
| - |
49 |
| -All other values are configured using environment variables. |
50 |
| - |
51 |
| -```bash |
52 |
| -az vm create \ |
53 |
| - --resource-group $MY_RESOURCE_GROUP_NAME \ |
54 |
| - --name $MY_VM_NAME \ |
55 |
| - --image $MY_VM_IMAGE \ |
56 |
| - --admin-username $MY_USERNAME \ |
57 |
| - --assign-identity \ |
58 |
| - --generate-ssh-keys \ |
59 |
| - --public-ip-sku Standard |
60 |
| -``` |
61 |
| - |
62 |
| -Results: |
63 |
| - |
64 |
| -<!-- expected_similarity=0.3 --> |
65 |
| -```json |
66 |
| -{ |
67 |
| - "fqdns": "", |
68 |
| - "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM", |
69 |
| - "location": "eastus", |
70 |
| - "macAddress": "00-0D-3A-10-4F-70", |
71 |
| - "powerState": "VM running", |
72 |
| - "privateIpAddress": "10.0.0.4", |
73 |
| - "publicIpAddress": "52.147.208.85", |
74 |
| - "resourceGroup": "myVMResourceGroup", |
75 |
| - "zones": "" |
76 |
| -} |
77 |
| -``` |
78 |
| - |
79 |
| -### Enable Azure AD login for a Linux Virtual Machine in Azure |
80 |
| - |
81 |
| -The following example has deploys a Linux VM and then installs the extension to enable Azure AD login for a Linux VM. VM extensions are small applications that provide post-deployment configuration and automation tasks on Azure virtual machines. |
82 |
| - |
83 |
| -```bash |
84 |
| -az vm extension set \ |
85 |
| - --publisher Microsoft.Azure.ActiveDirectory \ |
86 |
| - --name AADSSHLoginForLinux \ |
87 |
| - --resource-group $MY_RESOURCE_GROUP_NAME \ |
88 |
| - --vm-name $MY_VM_NAME |
89 |
| -``` |
90 |
| - |
91 |
| -# Store IP Address of VM in order to SSH |
92 |
| -run the following command to get the IP Address of the VM and store it as an environment variable |
93 |
| - |
94 |
| -```bash |
95 |
| -export IP_ADDRESS=$(az vm show --show-details --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_VM_NAME --query publicIps --output tsv) |
96 |
| -``` |
97 |
| - |
98 |
| -# SSH Into VM |
99 |
| - |
100 |
| -<!--## Export the SSH configuration for use with SSH clients that support OpenSSH & SSH into the VM. |
101 |
| -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:--> |
102 |
| - |
103 |
| -<!-- |
104 |
| -```bash |
105 |
| -yes | az ssh config --file ~/.ssh/config --name $MY_VM_NAME --resource-group $MY_RESOURCE_GROUP_NAME |
106 |
| -``` |
107 |
| ---> |
108 |
| - |
109 |
| -You can now SSH into the VM by running the output of the following command in your ssh client of choice |
110 |
| - |
111 |
| -```bash |
112 |
| -ssh -o StrictHostKeyChecking=no $MY_USERNAME@$IP_ADDRESS |
113 |
| -``` |
114 |
| - |
115 |
| -# Next Steps |
116 |
| - |
117 |
| -* [VM Documentation](https://learn.microsoft.com/en-us/azure/virtual-machines/) |
118 |
| -* [Use Cloud-Init to initialize a Linux VM on first boot](https://learn.microsoft.com/en-us/azure/virtual-machines/linux/tutorial-automate-vm-deployment) |
119 |
| -* [Create custom VM images](https://learn.microsoft.com/en-us/azure/virtual-machines/linux/tutorial-custom-images) |
120 |
| -* [Load Balance VMs](https://learn.microsoft.com/en-us/azure/load-balancer/quickstart-load-balancer-standard-public-cli) |
| 1 | +# Create a Linux VM and SSH On Azure |
| 2 | + |
| 3 | +## Define Environment Variables |
| 4 | + |
| 5 | +The First step in this tutorial is to define environment variables. |
| 6 | + |
| 7 | +```bash |
| 8 | +export RANDOM_ID="$(openssl rand -hex 3)" |
| 9 | +export MY_RESOURCE_GROUP_NAME="myVMResourceGroup$RANDOM_ID" |
| 10 | +export REGION=EastUS |
| 11 | +export MY_VM_NAME="myVM$RANDOM_ID" |
| 12 | +export MY_USERNAME=azureuser |
| 13 | +export MY_VM_IMAGE="Canonical:0001-com-ubuntu-minimal-jammy:minimal-22_04-lts-gen2:latest" |
| 14 | +``` |
| 15 | + |
| 16 | +# Login to Azure using the CLI |
| 17 | + |
| 18 | +In order to run commands against Azure using the CLI you need to login. This is done, very simply, though the `az login` command: |
| 19 | + |
| 20 | +# Create a resource group |
| 21 | + |
| 22 | +A resource group is a container for related resources. All resources must be placed in a resource group. We will create one for this tutorial. The following command creates a resource group with the previously defined $MY_RESOURCE_GROUP_NAME and $REGION parameters. |
| 23 | + |
| 24 | +```bash |
| 25 | +az group create --name $MY_RESOURCE_GROUP_NAME --location $REGION |
| 26 | +``` |
| 27 | + |
| 28 | +Results: |
| 29 | + |
| 30 | +<!-- expected_similarity=0.3 --> |
| 31 | + |
| 32 | +```json |
| 33 | +{ |
| 34 | + "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myVMResourceGroup", |
| 35 | + "location": "eastus", |
| 36 | + "managedBy": null, |
| 37 | + "name": "myVMResourceGroup", |
| 38 | + "properties": { |
| 39 | + "provisioningState": "Succeeded" |
| 40 | + }, |
| 41 | + "tags": null, |
| 42 | + "type": "Microsoft.Resources/resourceGroups" |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +## Create the Virtual Machine |
| 47 | + |
| 48 | +To create a VM in this resource group we need to run a simple command, here we have provided the `--generate-ssh-keys` flag, this will cause the CLI to look for an avialable ssh key in `~/.ssh`, if one is found it will be used, otherwise 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. Finally, we are deploying the latest `Ubuntu 22.04` image. |
| 49 | + |
| 50 | +All other values are configured using environment variables. |
| 51 | + |
| 52 | +```bash |
| 53 | +az vm create \ |
| 54 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 55 | + --name $MY_VM_NAME \ |
| 56 | + --image $MY_VM_IMAGE \ |
| 57 | + --admin-username $MY_USERNAME \ |
| 58 | + --assign-identity \ |
| 59 | + --generate-ssh-keys \ |
| 60 | + --public-ip-sku Standard |
| 61 | +``` |
| 62 | + |
| 63 | +Results: |
| 64 | + |
| 65 | +<!-- expected_similarity=0.3 --> |
| 66 | + |
| 67 | +```json |
| 68 | +{ |
| 69 | + "fqdns": "", |
| 70 | + "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM", |
| 71 | + "location": "eastus", |
| 72 | + "macAddress": "00-0D-3A-10-4F-70", |
| 73 | + "powerState": "VM running", |
| 74 | + "privateIpAddress": "10.0.0.4", |
| 75 | + "publicIpAddress": "52.147.208.85", |
| 76 | + "resourceGroup": "myVMResourceGroup", |
| 77 | + "zones": "" |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +### Enable Azure AD login for a Linux Virtual Machine in Azure |
| 82 | + |
| 83 | +The following example has deploys a Linux VM and then installs the extension to enable 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 | + |
| 85 | +```bash |
| 86 | +az vm extension set \ |
| 87 | + --publisher Microsoft.Azure.ActiveDirectory \ |
| 88 | + --name AADSSHLoginForLinux \ |
| 89 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 90 | + --vm-name $MY_VM_NAME |
| 91 | +``` |
| 92 | + |
| 93 | +# Store IP Address of VM in order to SSH |
| 94 | + |
| 95 | +run the following command to get the IP Address of the VM and store it as an environment variable |
| 96 | + |
| 97 | +```bash |
| 98 | +export IP_ADDRESS=$(az vm show --show-details --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_VM_NAME --query publicIps --output tsv) |
| 99 | +``` |
| 100 | + |
| 101 | +# SSH Into VM |
| 102 | + |
| 103 | +<!--## Export the SSH configuration for use with SSH clients that support OpenSSH & SSH into the VM. |
| 104 | +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:--> |
| 105 | + |
| 106 | +<!-- |
| 107 | +```bash |
| 108 | +yes | az ssh config --file ~/.ssh/config --name $MY_VM_NAME --resource-group $MY_RESOURCE_GROUP_NAME |
| 109 | +``` |
| 110 | +--> |
| 111 | + |
| 112 | +You can now SSH into the VM by running the output of the following command in your ssh client of choice |
| 113 | + |
| 114 | +```bash |
| 115 | +ssh -o StrictHostKeyChecking=no $MY_USERNAME@$IP_ADDRESS |
| 116 | +``` |
| 117 | + |
| 118 | +# Next Steps |
| 119 | + |
| 120 | +- [VM Documentation](https://learn.microsoft.com/en-us/azure/virtual-machines/) |
| 121 | +- [Use Cloud-Init to initialize a Linux VM on first boot](https://learn.microsoft.com/en-us/azure/virtual-machines/linux/tutorial-automate-vm-deployment) |
| 122 | +- [Create custom VM images](https://learn.microsoft.com/en-us/azure/virtual-machines/linux/tutorial-custom-images) |
| 123 | +- [Load Balance VMs](https://learn.microsoft.com/en-us/azure/load-balancer/quickstart-load-balancer-standard-public-cli) |
0 commit comments