Use cloud-init to set hostname for a Linux VM in Azure

Şunlar için geçerlidir: ✔️ Linux VM'leri ✔️ Esnek ölçek kümeleri

This article shows you how to use cloud-init to configure a specific hostname on a virtual machine (VM) or virtual machine scale sets (VMSS) at provisioning time in Azure. Bu cloud-init komut dosyaları, kaynaklar Azure tarafından sağlandıktan sonra ilk önyükleme sırasında çalıştırılır. Cloud-init'in Azure'da yerel olarak nasıl çalıştığı ve desteklenen Linux dağıtımları hakkında daha fazla bilgi için bkz. cloud-init'e genel bakış

Set the hostname with cloud-init

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, specify the cloud-init file with the --custom-data switch.

To see upgrade process in action, create a file in your current shell named cloud_init_hostname.txt and paste the following configuration. Bu örnekte, dosyayı yerel makinenizde değil Cloud Shell'de oluşturun. İstediğiniz düzenleyiciyi kullanabilirsiniz. Cloud-init dosyasının tamamının, özellikle de ilk satırın doğru kopyalandığından emin olun.

#cloud-config
fqdn: myhostname

Bu görüntüyü dağıtmadan önce az group create komutuyla bir kaynak grubu oluşturmanız gerekir. Azure kaynak grubu, Azure kaynaklarının dağıtıldığı ve yönetildiği bir mantıksal kapsayıcıdır. Aşağıdaki örnek eastus konumunda myResourceGroup adlı bir kaynak grubu oluşturur.

az group create --name myResourceGroup --location eastus

Şimdi az vm create ile bir VM oluşturun ve cloud-init dosyasını --custom-data cloud_init_hostname.txt şu şekilde belirtin:

az vm create \
  --resource-group myResourceGroup \
  --name vmName \
  --image imageCIURN \
  --custom-data cloud_init_hostname.txt \
  --generate-ssh-keys

Uyarı

myResourceGroup, vmName ve imageCIURN değerlerini uygun şekilde değiştirin. Cloud-init içeren bir görüntünün seçildiğinden emin olun.

Once created, the Azure CLI shows information about the VM. Use the publicIpAddress to SSH to your VM. Enter your own address as follows:

ssh <user>@<publicIpAddress>

To see the VM name, use the hostname command as follows:

sudo hostname

The VM should report the hostname as that value set in the cloud-init file, as shown in the following example output:

myhostname

Sonraki adımlar

Yapılandırma değişikliklerine ilişkin ek cloud-init örnekleri için aşağıdakilere bakın: