Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Se aplica a: ✔️ máquinas virtuales Linux ✔️ conjuntos de escalado flexibles
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. For this example, create the file in the Cloud Shell not on your local machine. Puede utilizar el editor que prefiera. Asegúrese de que todo el archivo cloud-init se copia correctamente, especialmente la primera línea. 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>
Nota:
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.
Before deploying this image, you need to create a resource group with the az group create command. Un grupo de recursos de Azure es un contenedor lógico en el que se implementan y se administran los recursos de Azure. En el ejemplo siguiente, se crea un grupo de recursos denominado myResourceGroup en la ubicación 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
Nota:
Reemplace los valores myResourceGroup, vmName e imageCIURN según corresponda. Asegúrese de que se elige una imagen con Cloud-init.
SSH a la dirección IP pública de la máquina virtual que se muestra en la salida del comando anterior. Escriba su propio user y publicIpAddress, como se indica a continuación:
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:
Pasos siguientes
Para ejemplos adicionales de cambios de configuración en cloud-init, vea lo siguiente:
- Incorporación de otro usuario de Linux a una máquina virtual
- Ejecución de un administrador de paquetes para actualizar los existentes durante el primer arranque
- Cambio del nombre de host de la máquina virtual local
- Instalación de un paquete de aplicación, actualización de los archivos de configuración e inserción de claves