使用 cloud-init 将用户添加到 Azure 中的 Linux VMUse cloud-init to add a user to a Linux VM in Azure

本文介绍如何在 Azure 中使用 cloud-init 在预配时将用户添加到 Linux 虚拟机 (VM) 或虚拟机规模集 (VMSS)。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. Azure 预配资源后,此 cloud-init 脚本将在首次启动时运行。This cloud-init script runs on first boot once the resources have been provisioned by Azure. 有关 cloud-init 如何在 Azure 以及受支持的 Linux 发行版中本机工作的详细信息,请参阅 cloud-init 概述For more information about how cloud-init works natively in Azure and the supported Linux distros, see cloud-init overview.

使用 cloud-init 向 VM 添加用户Add a user to a VM with cloud-init

任何新 Linux VM 的首要任务之一就是,自行添加附加的用户以避免使用 rootOne of the first tasks on any new Linux VM is to add an additional user for yourself to avoid the use of root. SSH 密钥是安全性和可用性的最佳做法。SSH keys are best practice for security and usability. 使用此 cloud-init 脚本将密钥添加到“~/.ssh/authorized_keys” 文件。Keys are added to the ~/.ssh/authorized_keys file with this cloud-init script.

若要将用户添加到 Linux VM,请在当前 shell 中创建名为 cloud_init_add_user.txt 的文件并粘贴以下配置。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 on your local machine. 可使用任何想要使用的编辑器。You can use any editor you wish. 输入 sensible-editor cloud_init_add_user.txt 以创建文件并查看可用编辑器的列表。Enter sensible-editor cloud_init_add_user.txt to create the file and see a list of available editors. 选择 #1 以使用 nano 编辑器 。Choose #1 to use the nano editor. 请确保已正确复制整个 cloud-init 文件,尤其是第一行。Make sure that the whole cloud-init file is copied correctly, especially the first line. 需要提供自己的公钥(例如 ~/.ssh/id_rsa.pub 的内容)作为 ssh-authorized-keys: 的值 - 为简化示例,此处已缩短。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>

备注

#cloud-config 文件包含 - default 参数。The #cloud-config file includes the - default parameter included. 这会将用户追加到预配期间创建的现有管理员用户。This will append the user, to the existing admin user created during provisioning. 如果不 - default 参数创建用户 - 将覆盖 Azure 平台自动生成的管理员用户。If you create a user without the - default parameter - the auto generated admin user created by the Azure platform would be overwritten.

在部署此映像之前,需要使用 az group create 命令创建资源组。Before deploying this image, you need to create a resource group with the az group create command. Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed. 以下示例在“chinaeast”位置创建名为“myResourceGroup”的资源组。The following example creates a resource group named myResourceGroup in the chinaeast location.

az group create --name myResourceGroup --location chinaeast

现在,使用 az vm create 创建 VM,并通过 --custom-data cloud_init_add_user.txt 指定 cloud-init 文件,如下所示: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 centos74 \
  --image OpenLogic:CentOS:7-CI:latest \
  --custom-data cloud_init_add_user.txt \
  --generate-ssh-keys 

通过 SSH 连接到 VM 的公共 IP 地址显示在先前命令的输出中。SSH to the public IP address of your VM shown in the output from the preceding command. 按如下所示输入自己的 publicIpAddressEnter your own publicIpAddress as follows:

ssh <publicIpAddress>

若要确认已将用户添加到 VM 和指定的组,请查看“/etc/group” 文件的内容,如下所示:To confirm your user was added to the VM and the specified groups, view the contents of the /etc/group file as follows:

cat /etc/group

以下示例输出显示“cloud_init_add_user.txt” 文件中的用户已被添加至 VM 和相应群组: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:

后续步骤Next steps

有关配置更改的其他 cloud-init 示例,请参阅以下文章:For additional cloud-init examples of configuration changes, see the following: