Cloud-Init操作类 本节为您介绍镜像服务常见的cloudinit操作类问题。 安装CloudInit FAQ Cloudinit能做什么 cloudinit是一款用于初始化云主机的工具,它拥有丰富的模块,能够为云主机提供的能力有:初始化密码、扩容根分区、设置主机名、注入公钥、执行自定义脚本等等,功能十分强大。 安装cloudinit 检查是否已经安装CloudInit工具。 不同的操作系统,命令不同,以centos为例,执行以下命令查看系统是否已经安装cloudinit: plaintext rpm qa grep cloudinit 安装cloudinit 无明确版本要求,建议安装系统源上的版本(若采用旧有编译安装,则建议在大更改时重做为系统源版本)。 采用以下命令安装: plaintext yum install cloudinit cloudutilsgrowpart y 配置cloudinit 默认的cloud.cfg配置文件/etc/cloud/cloud.cfg修改如下(不同操作系统会有不同,根据实际情况来定需要设置的值): 1. 确定参数值如下:true/True/1,false/False/0 均可。 plaintext disableroot: false sshpwauth: true sshdeletekeys: false preservehostname: false 若 cloudinit 版本 > 21.1(参考 cloudinit v), 在配置文件中,preservehostname: false 另起一行(或保证缩进同级即可),加上: plaintext Preferfqdnoverhostname: true 若cloudinit 版本 < 21.1, 修改 cloudinit python 包源码 cloudinit/distros/ init .py,将: plaintext defselecthostname (self,hostname,fqdn) : Prefer the short hostname over the long fully qualified domain nameif not hostname: return fqdn return hostname 替换为: plaintext defselecthostname (self, hostname, fqdn) : if fqdn: return fqdn return hostname 说明 修改配置或代码都是为了优先使用 fqdn 从而避免像 AAA.bbb 这样的 hostname“截断”成 AAA 的问题。确保输入 hostname 命令显示的是 AAA.bbb 非“截断”主机名即可,因系统限制在终端显示的 root@AAA 可忽略。 2. 模块包括如下内容(建议参考修改)。 plaintext cloudinitmodules: migrator seed random bootcmd writefiles growpart resizefs disk setup mounts set hostname update hostname update etc hosts cacerts rsyslog usersgroups ssh 3. 修改systeminfo部分。 distro内容不做修改, defaultuser:默认用户名改为root,lockpasswd 改为False, 其余部分涉及系统 yum/apt 源的更改,建议遵循“系统其他配置修改”,保留系统默认,去除 cloudinit 配置, systeminfo下其余内容不做修改。 plaintext systeminfo: