You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Removing the reference to sensible-editor, given that's not installed in all Linux distros and the sentence is already mentioning they can use the text editor of their preference. Removing also the centos7 and URN image from the sample entry for the vm create to make it Linux agnostic (they could use any image with Cloud-init), changing the last block code from bash to output. I also added sudo to the commands
Copy file name to clipboardExpand all lines: articles/virtual-machines/linux/cloudinit-update-vm-hostname.md
+16-11Lines changed: 16 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,21 +5,22 @@ author: mattmcinnes
5
5
ms.service: virtual-machines
6
6
ms.collection: linux
7
7
ms.topic: how-to
8
-
ms.date: 11/29/2017
8
+
ms.date: 03/29/2023
9
9
ms.author: mattmcinnes
10
10
ms.subservice: cloud-init
11
11
12
12
---
13
13
# Use cloud-init to set hostname for a Linux VM in Azure
14
14
15
-
**Applies to:**:heavy_check_mark: Linux VMs :heavy_check_mark: Flexible scale sets
15
+
**Applies to:**:heavy_check_mark: Linux VMs :heavy_check_mark: Flexible scale sets
16
16
17
17
This article shows you how to use [cloud-init](https://cloudinit.readthedocs.io) to configure a specific hostname on a virtual machine (VM) or virtual machine scale sets (VMSS) at provisioning time in Azure. These cloud-init scripts run on first boot once the resources have been provisioned by Azure. For more information about how cloud-init works natively in Azure and the supported Linux distros, see [cloud-init overview](using-cloud-init.md)
18
18
19
19
## Set the hostname with cloud-init
20
+
20
21
By default, the hostname is the same as the VM name when you create a new virtual machine in Azure. To run a cloud-init script to change this default hostname when you create a VM in Azure with [az vm create](/cli/azure/vm), specify the cloud-init file with the `--custom-data` switch.
21
22
22
-
To see upgrade process in action, create a file in your current shell named *cloud_init_hostname.txt* and paste the following configuration. For this example, create the file in the Cloud Shell not on your local machine. You can use any editor you wish. Enter `sensible-editor cloud_init_hostname.txt` to create the file and see a list of available editors. Choose #1 to use the **nano** editor. Make sure that the whole cloud-init file is copied correctly, especially the first line.
23
+
To see upgrade process in action, create a file in your current shell named *cloud_init_hostname.txt* and paste the following configuration. For this example, create the file in the Cloud Shell not on your local machine. You can use any editor you wish. Make sure that the whole cloud-init file is copied correctly, especially the first line.
23
24
24
25
```yaml
25
26
#cloud-config
@@ -28,42 +29,46 @@ fqdn: myhostname
28
29
29
30
Before deploying this image, you need to 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. The following example creates a resource group named *myResourceGroup* in the *eastus* location.
30
31
31
-
```azurecli-interactive
32
+
```azurecli-interactive
32
33
az group create --name myResourceGroup --location eastus
33
34
```
34
35
35
36
Now, create a VM with [az vm create](/cli/azure/vm) and specify the cloud-init file with `--custom-data cloud_init_hostname.txt` as follows:
36
37
37
-
```azurecli-interactive
38
+
```azurecli-interactive
38
39
az vm create \
39
40
--resource-group myResourceGroup \
40
-
--name centos74 \
41
-
--image OpenLogic:CentOS:7-CI:latest \
41
+
--name vmName \
42
+
--image imageCIURN \
42
43
--custom-data cloud_init_hostname.txt \
43
44
--generate-ssh-keys
44
45
```
45
46
47
+
> [!NOTE]
48
+
> Replace **myResourceGroup**, **vmName**, and **imageCIURN** values accordingly. Make sure an image with Cloud-init is chosen.
49
+
46
50
Once created, the Azure CLI shows information about the VM. Use the `publicIpAddress` to SSH to your VM. Enter your own address as follows:
47
51
48
52
```bash
49
-
ssh <publicIpAddress>
53
+
ssh <user>@<publicIpAddress>
50
54
```
51
55
52
56
To see the VM name, use the `hostname` command as follows:
53
57
54
58
```bash
55
-
hostname
59
+
sudo hostname
56
60
```
57
61
58
62
The VM should report the hostname as that value set in the cloud-init file, as shown in the following example output:
59
63
60
-
```bash
64
+
```output
61
65
myhostname
62
66
```
63
67
64
68
## Next steps
69
+
65
70
For additional cloud-init examples of configuration changes, see the following:
66
-
71
+
67
72
-[Add an additional Linux user to a VM](cloudinit-add-user.md)
68
73
-[Run a package manager to update existing packages on first boot](cloudinit-update-vm.md)
69
74
-[Change VM local hostname](cloudinit-update-vm-hostname.md)
0 commit comments