
# 配置Cloud-Init
在安装完Cloud-Init后，需要配置cloud.cfg文件，用于定制Cloud-Init在iMetal服务器初始化时的功能配置。
使用vi编辑器修改"/etc/cloud/cloud.cfg"配置文件，以下章节内容仅为示例，应该以实际需求为准。
本章节以cloud-init-0.7.9及以上版本配置为例进行介绍。
1. 添加如下键值对，前后各空出一行：
   ```
   no_ssh_fingerprints: true
   ```
   
2. 设置ssh_pwauth，该选项表示是否支持ssh密码登录。true为启用，false或0为禁用。
   ```
   ssh_pwauth: true
   ```
   
3. 修改disable_root为false。该参数表示是否禁用远程ssh root登录。
   ```
   disable_root: false
   ```
   
4. 添加preserve_hostname: false。
   ```
   preserve_hostname: false
   ```
   
5. 使用"#"注释掉以下语句（不存在忽略）：
   ```
   mount_default_fields: [~, ~, 'auto', 'defaults,nofail', '0', '2']
   resize_rootfs_tmp: /dev
   ssh_deletekeys:   0
   ```
   
6. 修改ssh_genkeytypes为下面语句（不存在添加）：
   ```
   ssh_genkeytypes: ['rsa', 'dsa']
   ```
   
7. 修改syslog_fix_perms为下面语句（不存在添加）：
   ```
   syslog_fix_perms: root:root
   ```
   
8. 添加下面语句：
   ```
   network:
      config: disabled
   datasource_list: [ OpenStack ]
   datasource:
     OpenStack:
       metadata_urls: ['http://169.254.169.254/clouddc']
       max_wait: 120
       timeout: 10
       retries: 5
   ```
   
9. （可选）在"/etc/cloud/cloud.cfg"中配置"apply_network_config: False"。 对于使用Cloud-Init 18.3及以上版本的用户，需执行本操作
   ```
   network:
      config: disabled
   datasource_list: [ OpenStack ]
   datasource:
     OpenStack:
       metadata_urls: ['http://169.254.169.254/clouddc']
       max_wait: 120
       timeout: 10
       retries: 5
       apply_network_config: False
   ```
   
10. 在cloud_final_modules段的"- final-message"后面添加一句：
    ```
    - power_state_change
    ```
    
11. 查看并修改system info中的信息：
    ```
    system_info:
       distro: rhel
       default_user:
         name: root   //登录操作系统使用的用户名
         lock_passwd: False   //True表示禁用密码登录方式，注意部分操作系统此处配置为1表示禁用
    ```
    其中，distro参数需要根据具体操作系统类型修改，比如distro: sles、distro: rhel、distro: ubuntu、distro: debian、dustro: fedora等。
    
12. （可选）对于SUSE 12 SP1和SUSE 12 SP2操作系统，还需要修改"/usr/lib/systemd/system/cloud-init-local.service"配置文件的\[Unit\]配置信息。 **vi /usr/lib/systemd/system/cloud-init-local.service**
    保持\[Unit\]配置项与下面内容一致：
    ```
    [Unit]
    Description=Initial cloud-init job (pre-networking)
    DefaultDependencies=no
    Wants=network-pre.target
    Wants=local-fs.target
    After=local-fs.target
    Before=network-pre.target
    Before=shutdown.target
    Before=basic.target
    Conflicts=shutdown.target
    # Other distros use Before=sysinit.target. There is not a clearly identified
    # reason for usage of basic.target instead.
    ```
    
13. （可选）对于Ubuntu 16.04操作系统，还需要执行以下命令，配置OpenStack源。 **dpkg-reconfigure cloud-init**
    ![](https://support.huaweicloud.com/usermanual-clouddc/zh-cn_image_0000002154598316.png)
    执行命令**vim /etc/cloud/cloud.cfg.d/90_dpkg.cfg**打开配置文件，检查配置项是否正确。
    ```
    # to update this file, run dpkg-reconfigure cloud-init
    datasource_list: [ OpenStack ]
    ~
    ~
    ~
    ```
    配置文件和以上回显信息一致，表示配置成功。
    
 
