Updated on 2026-11-25 GMT+08:00

(Optional) Installing and Configuring Cloud-Init

Scenarios

To ensure that you can use the user data injection function to inject initial custom information into ECSs created from a private image (such as setting the ECS login password), install Cloud-Init on the ECS used to create the image.

  • By default, ECSs created from a public image have Cloud-Init installed. You do not need to install or configure Cloud-Init on such ECSs.
  • For ECSs created using an external image file, install and configure Cloud-Init by performing the operations in this section.
  • You need to download Cloud-Init from its official website. Therefore, you must bind an EIP to the ECS.
  • If Cloud-Init is not installed, you cannot configure an ECS. As a result, you can only use the password in the image file to log in to the created ECSs.

Cloud-Init is open-source software. If the installed version has security vulnerabilities, you are advised to upgrade it to the latest version.

Prerequisites

  • An EIP has been bound to the ECS.
  • You have logged in to the ECS.
  • The ECS uses DHCP to obtain IP addresses.

Procedure

  1. Check whether Cloud-Init has been installed.

    For details, see Check Whether Cloud-Init Has Been Installed.

  2. Install Cloud-Init.

    You can install Cloud-Init in any of the following ways:

Check Whether Cloud-Init Has Been Installed

Perform the operations provided here to check whether Cloud-Init has been installed. The methods of checking whether Cloud-Init is installed vary depending on the OSs.

Python 3 Environment (Example: Ubuntu22.04)

  • If you are in a Python 3 environment, run the following command to check whether Cloud-Init is installed (Ubuntu22.04 is used as an example):
    which cloud-init
    • If information similar to the following is displayed, Cloud-Init has been installed:
      /usr/bin/cloud-init
    • If information similar to the following is displayed, Cloud-Init is not installed:
      /usr/bin/which: no cloud-init in (/usr/local/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin)

Python 2 Environment (Example: CentOS 6)

  • If you are in a Python 2 environment, run the following command to check whether Cloud-Init is installed (CentOS 6 is used as an example):
    which cloud-init
    • If information similar to the following is displayed, Cloud-Init has been installed, and no further installation is required.
      cloud-init-0.7.5-10.el6.centos.2.x86_64
    • If no information is returned, Cloud-Init is not installed.

      To confirm Cloud-Init is really not installed, you are advised to run rpm -qa |grep cloud-init to check again. If either which cloud-init or rpm -qa |grep cloud-init shows that Cloud-Init has been installed, Cloud-Init is installed.

If Cloud-Init has been installed, perform the following operations:
  • Determine whether to continue to use the SSH certificate in the OS of this ECS. If no, delete it.
    • If the certificate is stored in a directory of user root, for example, /$path/$to/$root/.ssh/authorized_keys, run the following commands:
      cd /root/.ssh
      rm authorized_keys
    • If the certificate is not stored in a directory of user root, for example, /$path/$to/$none-root/.ssh/authorized_keys, run the following commands:
      cd /home/centos/.ssh
      rm authorized_keys
  • Delete the cache data generated by Cloud-Init to ensure that ECSs created from the private image can be logged in by using an SSH certificate:
    sudo rm -rf /var/lib/cloud/*

After the operations are complete, do not restart the ECS. Otherwise, you need to perform these operations again.

(Recommended) Install Cloud-Init from the Official Repository

The method of installing Cloud-Init on an ECS varies depending on the OS. Perform the installation operations as user root.

Cloud-Init-23.2.2 is used as an example to describe how to install Cloud-Init on CentOS, Fedora, Ubuntu, Debian, and SUSE. For other OS types, install and configure the required Cloud-Init tool.

Download the cloud-init-23.2.2.tar.gz source code package from https://launchpad.net/cloud-init/trunk/23.2.2/+download/cloud-init-23.2.2.tar.gz.

CentOS 7/Fedora 36

  • To install Cloud-Init 23.2.2 in CentOS 7 or Fedora 36, perform the following steps:

    Ensure that Python 3 has been installed.

    1. Check whether Cloud-Init has been installed. If any command output is displayed, Cloud-Init has been installed.
      cloud-init -v
    2. Delete the cache directory of Cloud-Init.
      rm -rf /var/lib/cloud/*
    3. Install dependencies of Cloud-Init.
      yum install python3-pip -y
      yum install python3-devel
    4. Download the Cloud-Init package.
      wget https://launchpad.net/cloud-init/trunk/23.2.2/+download/cloud-init-23.2.2.tar.gz
    5. Decompress the Cloud-Init package.
      tar -zxvf cloud-init-23.2.2.tar.gz
    6. Go to the cloud-init-23.2.2 directory and install dependent libraries:
      cd cloud-init-23.2.2
      pip3 install -r requirements.txt
    7. Install Cloud-Init.
      python3 setup.py build
      python3 setup.py install --init-system systemd
    8. (Optional) Disable Cloud-Init's network configuration capability by modifying the /etc/cloud/cloud.cfg file.
      vi /etc/cloud/cloud.cfg

      Add the following content to the file:

      network:
        config: disabled
    9. Restart Cloud-Init and check its status.
      systemctl restart cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
      systemctl status cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service

    10. Enable Cloud-Init to automatically start upon system boot.
      systemctl enable cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
    11. Check the Cloud-Init version and whether Cloud-Init is running properly.
      cloud-init -v
      cloud-init init --local 

Ubuntu/debian

  • To install Cloud-Init 23.2.2 in Ubuntu 22.04 or Debian 11, perform the following steps:

    Ensure that Python 3 has been installed.

    1. Check and delete redundant Cloud-Init configuration files.
      rm -rf /var/lib/cloud/*
      rm -f /var/log/cloud-init*

      Delete all files except log-related configuration files from the /etc/cloud/cloud.cfg.d/ directory.

    2. Update your package list and check whether Wget is installed. If it is not, install it.
      sudo apt update
      sudo apt install wget 
    3. Install dependencies.
      apt-get install cloud-guest-utils -y
      apt-get install python3-pip -y
      apt-get install python3-devel
    4. Download the Cloud-Init package.
      wget https://launchpad.net/cloud-init/trunk/23.2.2/+download/cloud-init-23.2.2.tar.gz
    5. Decompress the Cloud-Init package.
      tar -zxvf cloud-init-23.2.2.tar.gz
    6. Go to the cloud-init directory.
      cd cloud-init-23.2.2
    7. Install dependent libraries.
      pip3 install -r requirements.txt
    8. Install Cloud-Init.
      python3 setup.py install
    9. (Optional) Disable Cloud-Init's network configuration capability.

      1. If the Cloud-Init version is 0.7.9 or later and you want to configure the network, after Cloud-Init is installed, run the following command to open the /etc/cloud/cloud.cfg file:

      vi /etc/cloud/cloud.cfg

      2. Enter i and set config to disabled for network in /etc/cloud/cloud.cfg. (If there is no such configuration item, add it.)

      network:
        config: disabled
    10. Restart Cloud-Init and check its status.
      systemctl restart cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
      systemctl status cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
    11. Enable Cloud-Init to automatically start upon system boot.
      systemctl enable cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
    12. Check the Cloud-Init version and whether Cloud-Init is running properly.
      cloud-init -v
      cloud-init init --local

SUSE Enterprise Linux Server

  • To install Cloud-Init 23.2.2 in SUSE Enterprise Linux Server 15, perform the following steps:

    Ensure that Python 3 has been installed.

    1. View existing SUSE repositories.
      zypper lr
    2. Delete the Cloud-Init repository from SUSE.
      zypper rr No. of the repository listed in 1
    3. Configure a SUSE repository.
      zypper ar https://ftp5.gwdg.de/pub/opensuse/repositories/Cloud:/Tools/SLE_15_SP4/Cloud:Tools.repo
    4. Refresh SUSE repositories.
      zypper refresh
    5. Install Cloud-Init.
      zypper install cloud-init
    6. Run cloud-init -v. If error messages similar to the following are displayed, install the dependencies.

      pip install requests pyyaml oauthlib jsonschema jsonpatch jinja2 configobj
    7. Check whether Cloud-Init is installed successfully. If the following error message is displayed, configure datasource_list in /etc/cloud/cloud.cfg.
      datasource_list: [ OpenStack ]
      datasource:
        OpenStack:
          metadata_urls: ['https://169.254.169.254']
          max_wait: 120
          timeout: 5
          apply_network_config: false
    8. Modify the configuration file, restart Cloud-Init, and check the Cloud-Init status.
      systemctl restart cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service 
      systemctl status cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
    9. Enable Cloud-Init-related services to automatically start upon system boot.
      systemctl enable cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service

    10. Check the Cloud-Init version and whether Cloud-Init is running properly.
      cloud-init -v
      cloud-init init --local

Install Cloud-Init Using pip with the Official Source Code Package

The following operations use Cloud-Init 0.7.9 as an example to describe how to install Cloud-Init.

  1. Download the cloud-init-0.7.9.tar.gz source code package and upload it to the /home/ directory of the ECS.

    Download cloud-init-0.7.9.tar.gz from the following path:

    https://launchpad.net/cloud-init/trunk/0.7.9/+download/cloud-init-0.7.9.tar.gz

  2. Create the pip.conf file in the ~/.pip/ directory and edit the file as shown below. (The Huawei Cloud open-source mirror is used as an example. Replace it with the actual address of the mirror you use.)

    If the ~/.pip/ directory does not exist, run the mkdir ~/.pip command to create it.

    [global]
    index-url = https://mirrors.huaweicloud.com/repository/pypi/simple
    trusted-host = mirrors.huaweicloud.com
  3. Run the following command to install the Cloud-Init source code package downloaded locally:

    Initial installation:

    pip install /home/cloud-init-0.7.9.tar.gz

    Upgrading the existing version:

    pip install --upgrade /home/cloud-init-0.7.9.tar.gz

    For details about how to install a Cloud-Init source code package, see Cloud-init documentation.

  4. Run the cloud-init -v command. Cloud-Init is installed successfully if information similar to the following is displayed:
    cloud-init 0.7.9
  5. Enable Cloud-Init-related services to automatically start upon system boot.
    • If the OS uses SysVinit to manage automatic start of services, run the following commands:
      chkconfig --add cloud-init-local; chkconfig --add cloud-init; chkconfig --add cloud-config; chkconfig --add cloud-final
      chkconfig cloud-init-local on; chkconfig cloud-init on; chkconfig cloud-config on; chkconfig cloud-final on
      service cloud-init-local status; service cloud-init status; service cloud-config status; service cloud-final status
    • If the OS uses Systemd to manage automatic start of services, run the following commands:
      systemctl enable cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
      systemctl status cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service

      If you install Cloud-Init using the official source code package and pip, note that:

      1. Add user syslog to the adm group during the installation. If user syslog exists, add it to the adm group. For some OSs (such as CentOS and SUSE), user syslog may not exist. Run the following commands to create user syslog and add it to the adm group:
        useradd syslog
        groupadd adm
        usermod -g adm syslog
      2. Change the value of distro in system_info in the /etc/cloud/cloud.cfg file based on the OS release version. For example, the value can be distro: ubuntu, distro: sles, distro: debian, or distro: fedora.

Compile and Install Cloud-Init from the Official Source Code

You can obtain the Cloud-Init source code from GitHub at https://github.com/canonical/cloud-init/

  1. Run the following commands to download the Cloud-Init package and copy it to the /tmp/CLOUD-INIT directory:
    wget https://github.com/canonical/cloud-init/archive/refs/tags/0.7.6.zip
    mkdir /tmp/CLOUD-INIT
    cp cloud-init-0.7.6.zip /tmp/CLOUD-INIT
    cd /tmp/CLOUD-INIT
  2. Run the following command to decompress the package:
    unzip cloud-init-0.7.6.zip
  3. Run the following command to enter the cloud-init-0.7.6 folder:
    cd cloud-init-0.7.6
  4. (Optional) If the Cloud-Init version is 18.3 to 22.3, run the following commands:
    sed -i '/VALID_DMI_ASSET_TAGS =/a\VALID_DMI_ASSET_TAGS += ["HUAWEICLOUD"]' cloudinit/sources/DataSourceOpenStack.py
    cat cloudinit/sources/DataSourceOpenStack.py | grep VALID_DMI_ASSET_TAGS

    The execution is successful if the following information is displayed.

  5. Install Cloud-Init. The commands vary depending on the OS type.
    • For CentOS 6.x or SUSE 11.x, run the following commands:
      python setup.py build
      python setup.py install --init-system sysvinit
    • For CentOS 7.x or SUSE 12.x, run the following commands:
      python setup.py build
      python setup.py install --init-system systemd

    Add user syslog to the adm group during the installation. If user syslog exists, add it to the adm group. For some OSs (such as CentOS and SUSE), user syslog may not exist. Run the following commands to create user syslog and add it to the adm group:

    useradd syslog
    groupadd adm
    usermod -g adm syslog
  6. Enable Cloud-Init to automatically start upon system boot.
    • If the OS uses SysVinit to manage automatic start of services, run the following commands:
      chkconfig --add cloud-init-local; chkconfig --add cloud-init; chkconfig --add cloud-config; chkconfig --add cloud-final
      chkconfig cloud-init-local on; chkconfig cloud-init on; chkconfig cloud-config on; chkconfig cloud-final on
      service cloud-init-local status; service cloud-init status; service cloud-config status; service cloud-final status
    • If the OS uses Systemd to manage automatic start of services, run the following commands:
      systemctl enable cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
      systemctl status cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
  7. Check whether Cloud-Init is installed successfully.
    cloud-init -v
    cloud-init init --local

    Cloud-Init is installed successfully if information similar to the following is displayed:

    cloud-init 0.7.6

Configure Cloud-Init

  1. Enable SSH authentication and remote password login for user root.
    Take CentOS 6.7 as an example. The value 0 of disable_root in the /etc/cloud/cloud.cfg file indicates root login is enabled (in some OSs, true indicates disabled and false indicates enabled). Set ssh_pwauth to 1 and lock_passwd to False (indicating that user passwords are not locked).
    users:
     - name: root
       lock_passwd: False
    
    disable_root: 0
    ssh_pwauth: 1
    • If you inject a password, use it for remote login through SSH or noVNC.
    • If you inject a key, use it for remote login through SSH.
  1. Modify the /etc/cloud/cloud.cfg file to disable Cloud-Init's network configuration capability.

    If the Cloud-Init version is 0.7.9 or later, add the following content to /etc/cloud/cloud.cfg:

    Figure 1 Disabling Cloud-Init's network configuration capability

    The added content must be in the YAML format.

  2. Enable the agent to access the data source.
    • Add the following content to the last line of /etc/cloud/cloud.cfg to enable the agent to access the OpenStack data source:
      datasource_list: [ OpenStack ]
      datasource:
        OpenStack:
          metadata_urls: ['http://169.254.169.254']
          max_wait: 120
          timeout: 5
          apply_network_config: false
    • You can decide whether to set max_wait and timeout. The values of max_wait and timeout in the preceding command output are only for reference.
    • If the OS version is earlier than Debian 8 or CentOS 5, you cannot enable the agent to access the IaaS OpenStack data source.
    • The default zeroconf route must be disabled for CentOS and EulerOS ECSs for accurate access to the IaaS OpenStack data source.

      echo "NOZEROCONF=yes" >> /etc/sysconfig/network

    • apply_network_config: false is required only if you are using Cloud-Init 18.3 or later.
  3. Add the following content to /etc/cloud/cloud.cfg:
    manage_etc_hosts: localhost

    This prevents the system from staying in the Waiting for cloudResetPwdAgent state for a long time during ECS startup.

    Figure 2 Adding manage_etc_hosts: localhost
  4. Run the vi /etc/ssh/sshd_config command to open the /etc/ssh/sshd_config file using the vi editor.
    Change the value of PasswordAuthentication in the sshd_config file to yes.

    For SUSE and openSUSE, change the values of the following parameters in the sshd_config file to yes:

    • PasswordAuthentication
    • ChallengeResponseAuthentication
  5. Modify cloud_init_modules in the cloud.cfg configuration file.
    • Move ssh from the bottom to the top to speed up the SSH login.
    • Enable the hostname update. Do not comment out or delete the -update_hostname statement.
      cloud_init_modules:
       - ssh
       - migrator
       - bootcmd
       - write-files
       - growpart
       - resizefs
       - set_hostname
       - update_hostname
       - update_etc_hosts
       - rsyslog
       - users-groups
  6. Modify the configuration so that the hostname of the ECS created from the image does not contain the .novalocal suffix and can contain a dot (.).
    1. Run the following command to modify the __init__.py file:
      vi /usr/lib/python*.*/site-packages/cloudinit/sources/__init__.py

      The Python version varies depending on the OS.

      Press i to enter editing mode. Modify the file content as follows based on the keyword toks:

      if toks:
          toks = str(toks).split('.')
      else:
          #toks = ["ip-%s" % lhost.replace(".", "-")] # Comment out this line.
          toks = lhost.split(".novalocal")    # Add this line.
      
      if len(toks) > 1:
          hostname = toks[0]
          #domain = '.'.join(toks[1:]) # Comment out this line.
      else:
          hostname = toks[0]
      
      if fqdn and domain != defdomain:
           return hostname   
          #return "%s.%s" % (hostname, domain) # Comment out this line.
      else:
          return hostname

      After the modification is complete, press Esc to exit the editing mode and enter :wq! to save the settings and exit.

    2. Run the following command to switch to the cloudinit/sources folder:
      cd /usr/lib/python*.*/site-packages/cloudinit/sources/

      The Python version varies depending on the OS.

    3. Run the following commands to delete the __init__.pyc file and the optimized __init__.pyo file:
      rm -rf __init__.pyc
      rm -rf __init__.pyo
    4. Run the following commands to clear the logs:
      rm -rf /var/lib/cloud/*
      rm -rf /var/log/cloud-init*

      Delete all files except log-related configuration files from the /etc/cloud/cloud.cfg.d/ directory.

  7. Run the following command to edit the /etc/cloud/cloud.cfg.d/05_logging.cfg file to use cloudLogHandler to process logs:
    vim /etc/cloud/cloud.cfg.d/05_logging.cfg
    [logger_cloudinit]
    level=DEBUG
    qualname=cloudinit
    handlers=cloudLogHandler
    propagate=1
  8. Delete user linux and the /home/linux directory from the image template.
    userdel linux
    rm -fr /home/linux
  9. Ensure that Cloud-Init is enabled.

    If the /etc/cloud/cloud-init.disabled file exists, run cloud-init clean --machine-id. If there is no such file, skip this step.

Check the Cloud-Init Configuration

Run the following command to check whether Cloud-Init has been properly configured:

cloud-init init --local
If Cloud-Init has been properly installed, the version information is displayed and no error occurs. For example, messages indicating lack of files will not be displayed.

(Optional) Run the following command to set the password validity period to the maximum:

chage -M 99999 $user_name

user_name is a system user, such as user root.

You are advised to set the password validity period to 99999.