Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Gilt für: ✔️ Linux-VMs ✔️ Flexible Skalierungsgruppen
This article shows you how to use cloud-init to add a user on a virtual machine (VM) or virtual machine scale sets (VMSS) at provisioning time in Azure. This cloud-init script runs 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.
Add a user to a VM with cloud-init
One of the first tasks on any new Linux VM is to add an additional user for yourself to avoid the use of root. SSH keys are best practice for security and usability. Keys are added to the ~/.ssh/authorized_keys file with this cloud-init script.
To add a user to a Linux VM, create a file in your current shell named cloud_init_add_user.txt and paste the following configuration. Erstellen Sie für dieses Beispiel die Datei in der Cloud Shell, nicht auf dem lokalen Computer. Dazu können Sie einen beliebigen Editor verwenden. Stellen Sie sicher, dass die gesamte cloud-init-Datei ordnungsgemäß kopiert wird, insbesondere die erste Zeile. You need to provide your own public key (such as the contents of ~/.ssh/id_rsa.pub) for the value of ssh-authorized-keys:
- it has been shortened here to simplify the example.
#cloud-config
users:
- default
- name: myadminuser
groups: sudo
shell: /bin/bash
sudo: ['ALL=(ALL) NOPASSWD:ALL']
ssh-authorized-keys:
- ssh-rsa AAAAB3<snip>
Hinweis
The #cloud-config file includes the - default
parameter included. This will append the user, to the existing admin user created during provisioning. If you create a user without the - default
parameter - the auto generated admin user created by the Azure platform would be overwritten.
Vor der Bereitstellung dieses Images müssen Sie mit dem Befehl az group create eine Ressourcengruppe erstellen. Eine Azure-Ressourcengruppe ist ein logischer Container, in dem Azure-Ressourcen bereitgestellt und verwaltet werden. Das folgende Beispiel erstellt eine Ressourcengruppe mit dem Namen myResourceGroup am Standort eastus.
az group create --name myResourceGroup --location eastus
Now, create a VM with az vm create and specify the cloud-init file with --custom-data cloud_init_add_user.txt
as follows:
az vm create \
--resource-group myResourceGroup \
--name vmName \
--image imageCIURN \
--custom-data cloud_init_add_user.txt \
--generate-ssh-keys
Hinweis
Ersetzen Sie die Werte myResourceGroup, vmName und imageCIURN entsprechend. Stellen Sie sicher, dass ein Image mit Cloud-init ausgewählt ist.
Stellen Sie eine SSH-Verbindung mit der öffentlichen IP-Adresse Ihrer VM her, die aus der Ausgabe des vorherigen Befehls hervorgeht. Geben Sie Ihren eigenen user und Ihre eigene publicIpAddress wie folgt ein:
ssh <user>@<publicIpAddress>
To confirm your user was added to the VM and the specified groups, view the contents of the /etc/group file as follows:
sudo cat /etc/group
The following example output shows the user from the cloud_init_add_user.txt file has been added to the VM and the appropriate group:
root:x:0:
<snip />
sudo:x:27:myadminuser
<snip />
myadminuser:x:1000:
Nächste Schritte
Weitere cloud-init-Beispiele für Änderungen an der Konfiguration finden Sie in den folgenden Themen: