All Products
Search
Document Center

Elastic Compute Service:Install cloud-init

Last Updated:Jan 19, 2024

Cloud-init is open source software that automates the initialization of Elastic Compute Service (ECS) instances during system boot. If cloud-init is not installed on a custom image, you must install cloud-init on the image to ensure that ECS instances created from the image can be initialized. This topic describes how to install and configure cloud-init.

For more information about cloud-init, see Cloud-init documentation.

Scenarios

By default, cloud-init is installed on all Alibaba Cloud public images. For the following scenarios, we recommend that you install Alibaba Cloud cloud-init on the Linux servers based on which you want to create custom images. This ensures that the ECS instances created from the custom images can be automatically initialized.

  • Cloud-init is not installed on the Linux servers that you want to migrate to the cloud.

    Note

    Proceed with caution when you install Alibaba Cloud cloud-init on servers that you do not want to migrate to the cloud.

  • Cloud-init of versions earlier than 0.7.9 is installed on Linux servers.

  • Cloud-init is not installed on ECS instances.

Step 1: Check whether to install or upgrade cloud-init

  1. Log on to the source server.

  2. Check whether cloud-init is installed on the source server.

    CentOS series

    rpm -qa | grep -i cloud-init 
    pip list | grep -i cloud-init

    Ubuntu series

    dpkg -l | grep -i cloud-init 
    pip list | grep -i cloud-init
    • If no version number is returned or if the version of cloud-init is earlier than 0.7.9, proceed to Step 2: Install cloud-init.

      Note

      Cloud-init 0.7.9 or earlier is an initial version of the native cloud-init that is not suitable for ECS instances and must be upgraded.

    • If the version of cloud-init is 18 or later, cloud-init can configure networks during instance initialization. If the network configurations do not meet your business requirements, you can modify the configurations. For more information, see the Customize network configurations section in this topic.

    • If the version of cloud-init is 19.1.21, we recommend that you upgrade to cloud-init 23.2.2. Compared with cloud-init 19.1.21, cloud-init 23.2.2 provides the following improvements and new features:

      • Support for starting ECS instances in security hardening mode on Alibaba Cloud. For information about the security hardening mode, see RunInstances.

      • Other feature enhancements, performance improvements, error fixes, and community contributions. For more information, see ChangeLog.

    • If other version numbers of cloud-init are returned, proceed to the What to do next section in this topic.

Step 2: Install cloud-init

  • Alibaba Cloud cloud-init 23.2.2: recommended. This version requires Python 3.6.

  • Alibaba Cloud cloud-init 19.1.21: requires Python 3.6.

  • Alibaba Cloud cloud-init 0.7.6a: Install this version on CentOS 6, Debian 9, and SUSE Linux Enterprise Server 12. This version requires Python 2.7.

    Note

    The Python community no longer provides technical support for Python 2.7. To prevent potential risks that are associated with dependency libraries, we recommend that you use more recent versions of cloud-init.

  • Native cloud-init: The community versions of cloud-init are maintained by the community. The most recent version of Alibaba Cloud cloud-init is 23.2.2. If you want to use a more recent version of cloud-init, install a community version of cloud-init.

    Important
    • Alibaba Cloud cloud-init is a version that is optimized for and can better support Alibaba Cloud services. To ensure compatibility, we recommend that you use Alibaba Cloud cloud-init.

    • To prevent data loss that is caused by accidental operations, we recommend that you back up the data of the source server. For example, you can create snapshots to back up the data of the source server.

(Recommended) Install Alibaba Cloud cloud-init 23.2.2

The most recent version of Alibaba Cloud cloud-init is 23.2.2, and the datasource for this version is Aliyun. Alibaba Cloud cloud-init 23.2.2 is maintained as a package. You can download a package that is suitable for your operating system version or use the source code package to install Alibaba Cloud cloud-init 23.2.2. You can also download other cloud-init versions from the official cloud-init website.

Download links of Alibaba Cloud cloud-init 23.2.2 packages for different operating systems

Operating system distribution

Operating system version

Binary package

MD5 checksum

Debian

12

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/debian12/cloud-init_23.2.2-1_all.deb

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/debian12/cloud-init_23.2.2-1_all.deb.md5sum

CentOS Stream

9

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/centosstream9/cloud-init-23.2.2-2.el9.noarch.rpm

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/centosstream9/cloud-init-23.2.2-2.el9.noarch.rpm.md5sum

8

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/centosstream8/cloud-init-23.2.2-2.el8.noarch.rpm

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/centosstream8/cloud-init-23.2.2-2.el8.noarch.rpm.md5sum

Fedora

39

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/fedora39/cloud-init-23.2.2-2.fc39.noarch.rpm

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/fedora39/cloud-init-23.2.2-2.fc39.noarch.rpm.md5sum

38

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/fedora38/cloud-init-23.2.2-1.fc38.noarch.rpm

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/fedora38/cloud-init-23.2.2-1.fc38.noarch.rpm.md5sum

AlmaLinux

9

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/almalinux9/cloud-init-23.2.2-2.el9.noarch.rpm

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/almalinux9/cloud-init-23.2.2-2.el9.noarch.rpm.md5sum

8

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/almalinux8/cloud-init-23.2.2-2.el8.noarch.rpm

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/almalinux8/cloud-init-23.2.2-2.el8.noarch.rpm.md5sum

Rocky Linux

9

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/rockylinux9/cloud-init-23.2.2-2.el9.noarch.rpm

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/rockylinux9/cloud-init-23.2.2-2.el9.noarch.rpm.md5sum

8

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/rockylinux8/cloud-init-23.2.2-2.el8.noarch.rpm

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/rockylinux8/cloud-init-23.2.2-2.el8.noarch.rpm.md5sum

The following sections describe how to install Alibaba Cloud cloud-init 23.2.2 on Debian 12 and CentOS Stream 9.

Debian 12

  1. Run the following command to download the Alibaba Cloud cloud-init package:

    wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/debian12/cloud-init_23.2.2-1_all.deb
  2. Run the following command to install the Alibaba Cloud cloud-init package:

    sudo apt-get install -y ./cloud-init_23.2.2-1_all.deb
  3. (Optional) To prevent Alibaba Cloud cloud-init from updating or upgrading to a more recent version, we recommend that you run the following command to solidify the Alibaba Cloud cloud-init package version:

    sudo apt-mark hold cloud-init

CentOS Stream 9

  1. Run the following command to download the Alibaba Cloud cloud-init package:

    wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/centosstream9/cloud-init-23.2.2-2.el9.noarch.rpm
  2. Run the following command to install the Alibaba Cloud cloud-init package:

    sudo yum install -y ./cloud-init-23.2.2-2.el9.noarch.rpm
  3. (Optional) To prevent Alibaba Cloud cloud-init from updating or upgrading to a more recent version, we recommend that you run the following command to solidify the Alibaba Cloud cloud-init package version:

    sudo sh -c 'echo "exclude=cloud-init" >> /etc/dnf/dnf.conf'
Note

Alibaba Cloud cloud-init 23.2.2 no longer automatically adds hostname-to-private IPv4 address mappings to the /etc/hosts file. This may affect the applications or services that depend on the mappings. You can manually add the mappings to the file based on your business requirements. In recent operating system versions, the nss-myhostname module is used to provide hostname resolution. In Red Hat operating systems, the nss-myhostname module is included in the systemd-libs package. In Debian operating systems, the nss-myhostname module is included in the libnss-myhostname package. The /etc/nsswitch.conf configuration file is used to enable the nss-myhostname module. For more information, see nss-myhostname(8) - Linux manual page.

Install Alibaba Cloud cloud-init 19.1.21

The datasource for Alibaba Cloud cloud-init is Aliyun. You can download other cloud-init versions from the official cloud-init website.

  1. Make sure that the python-pip dependency library is installed on the source server.

    In the examples, python3-pip is used. Run one of the following commands to install the python-pip dependency library based on the operating system of the source server.

    • CentOS or Red Hat Enterprise Linux:

      sudo yum -y install python3-pip
    • Ubuntu or Debian:

      sudo apt-get -y install python3-pip
    • openSUSE or SUSE:

      sudo zypper -n install python3-pip
  2. Run the following command to download the Alibaba Cloud cloud-init installation package:

    wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/cloud-init-19.1.21.tgz
  3. Run the following command to decompress the Alibaba Cloud cloud-init installation package to the current directory:

    tar -zxvf cloud-init-19.1.21.tgz
  4. Run the following commands to go to the cloud-init directory and install the dependency library:

    cd ./cloud-init-19.1.21
    pip3 install -r ./requirements.txt
  5. Run the following command to go to the tools subdirectory of the cloud-init directory:

    cd ./tools
  6. Run the following command to execute the deploy.sh script to install Alibaba Cloud cloud-init:

    sudo bash ./deploy.sh <issue> <major_version>

    The following table describes the parameters and the corresponding values in the deploy.sh script.

    Parameter

    Description

    Example

    <issue>

    The operating system type. Valid values: centos, redhat, rhel, debian, ubuntu, opensuse, and sles. The value of this parameter is case-sensitive. sles indicates SUSE or SUSE Linux Enterprise Server (SLES).

    Note

    If the operating system is CentOS Stream, set this parameter to centos.

    centos

    <major_version>

    The major version number of the operating system.

    Note

    Alibaba Cloud cloud-init 19.1.21 cannot be installed on Ubuntu 14.

    The major version number of CentOS 7.6 is 7.

    For example, if the current operating system is CentOS 7, run the sudo bash ./deploy.sh centos 7 command.

  7. Check whether Alibaba Cloud cloud-init is installed.

    If Alibaba Cloud cloud-init is installed, "description": "success" is returned.阿里云cloud-init安装成功

The following section provides sample shell scripts that can be used to install Alibaba Cloud cloud-init on different Linux distributions. Modify the script based on your operating system.

CentOS 7 or 8

# Check whether the python3-pip dependency library is installed. If not, install it.
if ! python3 -c 'import setuptools' >& /dev/null; then
  yum -y install python3-pip
fi
# Back up cloud-init of an earlier version.
test -d /etc/cloud && mv /etc/cloud /etc/cloud-old
# Download and decompress the Alibaba Cloud cloud-init installation package.
wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/cloud-init-19.1.21.tgz
tar -zxvf ./cloud-init-19.1.21.tgz
# Install Alibaba Cloud cloud-init.
issue_major=$( cat /etc/redhat-release | grep -Eo '[0-9]+\.?[0-9]+' | head -1 | awk -F'.' '{printf $1}')
bash ./cloud-init-*/tools/deploy.sh centos "$issue_major"

Red Hat Enterprise Linux 7 or 8

# Check whether the python3-pip dependency library is installed. If not, install it.
if ! python3 -c 'import setuptools' >& /dev/null; then
  yum -y install python3-pip
fi
# Back up cloud-init of an earlier version.
test -d /etc/cloud && mv /etc/cloud /etc/cloud-old
# Download and decompress the Alibaba Cloud cloud-init installation package.
wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/cloud-init-19.1.21.tgz
tar -zxvf ./cloud-init-19.1.21.tgz
# Install Alibaba Cloud cloud-init.
issue_major=$( cat /etc/os-release | grep VERSION_ID | grep -Eo '[0-9]+\.?[0-9]+' | head -1 | awk -F'.' '{printf $1}')
bash ./cloud-init-*/tools/deploy.sh rhel "$issue_major"

Ubuntu 16, 18, or 20

# Check whether the python3-pip dependency library is installed. If not, install it.
if ! python3 -c 'import setuptools' >& /dev/null; then
  apt-get install python36 python3-pip -y
fi
# Back up cloud-init of an earlier version.
test -d /etc/cloud && mv /etc/cloud /etc/cloud-old
# Download and decompress the Alibaba Cloud cloud-init installation package.
wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/cloud-init-19.1.21.tgz
tar -zxvf ./cloud-init-19.1.21.tgz
# Install Alibaba Cloud cloud-init.
issue_major=$( cat /etc/os-release | grep VERSION_ID | grep -Eo '[0-9]+\.?[0-9]+' | head -1 | awk -F'.' '{printf $1}')
bash ./cloud-init-*/tools/deploy.sh ubuntu "$issue_major"

Debian 9 or 10

# Check whether the python3-pip dependency library is installed. If not, install it.
if ! python3 -c 'import setuptools' >& /dev/null; then
  apt-get -y install python3-pip
fi
# Back up cloud-init of an earlier version.
test -d /etc/cloud && mv /etc/cloud /etc/cloud-old
# Download and decompress the Alibaba Cloud cloud-init installation package.
wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/cloud-init-19.1.21.tgz
tar -zxvf ./cloud-init-19.1.21.tgz
# Install Alibaba Cloud cloud-init.
issue_major=$( cat /etc/os-release | grep VERSION_ID | grep -Eo '[0-9]+\.?[0-9]+' | head -1 | awk -F'.' '{printf $1}')
bash ./cloud-init-*/tools/deploy.sh debian "$issue_major"

SUSE 12 or 15

# Check whether the python3-pip dependency library is installed. If not, install it.
if ! python3 -c 'import setuptools'>& /dev/null; then
  zypper -n install python3-pip
fi
# Back up cloud-init of an earlier version.
test -d /etc/cloud && mv /etc/cloud/etc/cloud-old
# Download and decompress the Alibaba Cloud cloud-init installation package.
wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/cloud-init-19.1.21.tgz
tar -zxvf ./cloud-init-19.1.21.tgz
# Install Alibaba Cloud cloud-init.
issue_major=$( cat /etc/os-release | grep VERSION_ID | grep -Eo '[0-9]+\.?[0-9]+' | head -1 | awk -F'.' '{printf $1}')
bash ./cloud-init-*/tools/deploy.sh sles "$issue_major"

openSUSE 15

# Check whether the python3-pip dependency library is installed. If not, install it.
if ! python3 -c 'import setuptools'>& /dev/null; then
  zypper -n install python3-pip
fi
# Back up cloud-init of an earlier version.
test -d /etc/cloud && mv /etc/cloud/etc/cloud-old
# Download and decompress the Alibaba Cloud cloud-init installation package.
wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/cloud-init-19.1.21.tgz
tar -zxvf ./cloud-init-19.1.21.tgz
# Install Alibaba Cloud cloud-init.
issue_major=$( cat /etc/os-release | grep VERSION_ID | grep -Eo '[0-9]+\.?[0-9]+' | head -1 | awk -F'.' '{printf $1}')
bash ./cloud-init-*/tools/deploy.sh opensuse"$issue_major"

Install Alibaba Cloud cloud-init 0.7.6a15

If the source server runs an operating system of an earlier version such as CentOS 6, Debian 9, or SUSE Linux Enterprise Server 12, install Alibaba Cloud cloud-init 0.7.6a15.

Note

By default, Alibaba Cloud cloud-init 0.7.6a15 is installed on Alibaba Cloud public images for CentOS 6, Debian 9, and SUSE Linux Enterprise Server 12. If you want to test Alibaba Cloud cloud-init-0.7.6a15, run the mv /etc/cloud/cloud.cfg /etc/cloud/cloud.cfg_bak command to back up the configuration file.

  1. Run the following command to check whether the operating system version is CentOS 6, Debian 9, or SUSE Linux Enterprise Server 12:

    cat /etc/issue
  2. Run the following commands to download and decompress the Alibaba Cloud cloud-init 0.7.6a15 installation package:

    wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloud-init-0.7.6a15.tgz
    tar -zxvf cloud-init-0.7.6a15.tgz
  3. Run the following command to go to the tools subdirectory of the cloud-init directory:

    cd cloud-init-0.7.6a15/tools/
  4. Run the following command to execute the deploy.sh script to install Alibaba Cloud cloud-init:

    sudo bash ./deploy.sh <issue> <major_version>

    The following table describes the parameters and the corresponding values in the deploy.sh script.

    Parameter

    Description

    Example

    <issue>

    The operating system type. Valid values: centos, debian, and sles. The value of this parameter is case-sensitive. sles indicates SUSE or SLES.

    centos

    <major_version>

    The major version number of the operating system.

    The major version number of CentOS 6.5 is 6.

    For example, if the current operating system is CentOS 6, run the bash ./deploy.sh centos 6 command.

Install the native cloud-init

The most recent version of Alibaba Cloud cloud-init is 23.2.2. If you want to use a more recent version of cloud-init, install a community version of cloud-init.

  1. Make sure that the Git, Python, and python-pip dependency libraries are installed on the source server.

    In the examples, Git, Python 3.6, and python3-pip are used. Run the following commands to install Git, Python, and python-pip dependency libraries for specific Linux distributions:

    CentOS or Red Hat Enterprise Linux

    sudo yum -y install git python36 python3-pip

    Ubuntu or Debian

    sudo apt-get -y install git python36 python3-pip

    openSUSE or SUSE

    sudo zypper -n install git python36 python3-pip
  2. Run the following command to download the cloud-init source code repository from Git:

    git clone https://git.launchpad.net/cloud-init
  3. Run the following command to go to the cloud-init directory:

    cd ./cloud-init
  4. Run the following command to install all required dependency libraries:

    sudo pip3 install -r ./requirements.txt
  5. Run the following command to install cloud-init:

    python3 setup.py install
  6. Modify the cloud.cfg configuration file.

    1. Open the configuration file.

      sudo vi /etc/cloud/cloud.cfg

      vi /etc/cloud/cloud.cfg

    2. Replace the content that precedes cloud_init_modules: with the following content:

      # Example datasource config
      # The top level settings are used as module
      # and system configuration.
      # A set of users which may be applied and/or used by various modules
      # when a 'default' entry is found it will reference the 'default_user'
      # from the distro configuration specified below
      users:
         - default
      user:
          name: root
          lock_passwd: False
      # If this is set, 'root' will not be able to ssh in and they 
      # will get a message to login instead as the above $user
      disable_root: false
      # This will cause the set+update hostname module to not operate (if true)
      preserve_hostname: false
      syslog_fix_perms: root:root
      datasource_list: [ AliYun ]
      # Example datasource config
      datasource:
          AliYun:
              support_xen: false
              timeout: 5 # (defaults to 50 seconds)        max_wait: 60 # (defaults to 120 seconds)
      #      metadata_urls: [ 'blah.com' ]
      # The modules that run in the 'init' stage
      cloud_init_modules:

(Optional) Step 3: Configure cloud-init

Customize network configurations

Cloud-init 18 and later automatically initialize network configurations as follows: BOOTPROTO=dhcp DEVICE=eth0 ONBOOT=yes STARTMODE=auto TYPE=Ethernet USERCTL=no. If the default network configurations do not meet your business requirements, you can perform the following operations to customize network configurations:

  1. After you install cloud-init, open the /etc/cloud/cloud.cfg file.

    sudo vim /etc/cloud/cloud.cfg
  2. Press the I key to enter Insert mode. Add the following disabled configuration before Example datasource config:

    network:
      config: disabled
    Note

    After you add the configuration, you must manually manage the network configurations in the/etc/sysconfig/network-scripts/ directory because cloud-init does not manage the network configurations.

    cloud-init-disable-config

  3. Press the Esc key to exit Insert mode, enter :wq, and then press the Enter key to save and close the file.

Retain the hostname and the /etc/hosts configuration file

By default, the hostname in the cloud-init configuration file is not retained. If you do not want the hostname and /etc/hosts configuration file of your instance to be modified, you can perform the following operations to modify the cloud-init configuration file:

  1. After you install cloud-init, open the /etc/cloud/cloud.cfg file.

    sudo vim /etc/cloud/cloud.cfg
  2. Press the I key to enter Insert mode and change preserve_hostname: false to preserve_hostname: true.

    hostname

  3. Press the Esc key to exit Insert mode, enter :wq, and then press the Enter key to save and close the file.

    preserve_hostname: true prevents cloud-init from modifying the instance hostname and the /etc/hosts configuration file.

Troubleshooting

  • What do I do if the required dependency libraries are missing after I use Python 3 to install cloud-init?

    The dependency libraries that are missing may vary based on the image. You can use pip to install the libraries and then re-install cloud-init.

  • What do I do if cloud-init is not running as expected and an error message is returned?

    If different versions of dependency libraries are installed by using the default software package manager (such as YUM) and the pip manager, library version conflicts may occur and cause cloud-init to not run as expected. We recommend that you download dependency libraries based on the error message.

    Error message

    Possible cause

    Troubleshooting command

    no setuptools module in python

    The python setuptools module is missing.

    For example, if you use Python 3.6, run one of the following commands based on the operating system to install the python setuptools module:

    • CentOS and Red Hat: yum -y install python3-pip

    • Ubuntu and Debian: apt-get -y install python3-pip

    • OpenSUSE and SUSE: zypper -n install python3-pip

    File "/home/testuser/cloud-init/cloudinit/log.py", line 19, in <module>
          import six
      ImportError: No module named six  )

    The six dependency library is missing.

    pip3 install six
    File "/home/testuser/cloud-init/cloudinit/url_helper.py", line 20, in <module>
          import oauthlib.oauth1 as oauth1
      ImportError: No module named oauthlib.oauth1  )

    The oauthlib dependency library is missing.

    pip3 install oauthlib

    No indication of the missing dependency library.

    The error message does not indicate a cause.

    Run the following command to install all dependency libraries that are displayed in the requirements.txt file of cloud-init:

    pip3 install -r requirements.txt
  • How do I make sure that the correct version of cloud-init is installed?

    After you install cloud-init, you can run the cloud-init --version command to check the version of cloud-init. If the returned version is the version that you want to install, the version of cloud-init is correct. For example, you want to install Alibaba Cloud cloud-init 19.1.21. After you install cloud-init and run the cloud-init --version command, the command output contains 19.1.21 if cloud-init 19.1.21 is installed.

  • What do I do if cloud-init does not run as expected on an instance after I install a new version of Python 3 on the instance?

    If you install a new version of Python 3 such as Python 3.9 on your instance and create a symbolic link (such as ln -s /usr/bin/python3.9 /usr/bin/python3) for the version to configure the installed version as the default version, cloud-init does not run as expected on the instance. For example, when you run the cloud-init --version command, an error is reported.

    $cloud-init --version
    Traceback (most recent call last):
      File "/usr/local/bin/cloud-init", line 33, in <module>
        sys.exit(load_entry_point('cloud-init==19.1.9', 'console_scripts', 'cloud-init')())
      File "/usr/local/bin/cloud-init", line 22, in importlib_load_entry_point
        for entry_point in distribution(dist_name).entry_points
      File "/usr/lib64/python3.9/importlib/metadata.py", line 524, in distribution
        return Distribution.from_name(distribution_name)
      File "/usr/lib64/python3.9/importlib/metadata.py", line 187, in from_name
        raise PackageNotFoundError(name)
    importlib.metadata.PackageNotFoundError: cloud-init

    You can use one of the following methods to resolve the issue:

    • Method 1: Use the new version of Python 3 to re-install cloud-init.

    • Method 2: Modify the cloud-init executable file to change the Python interpreter path to the path of an earlier Python 3 version. In this example, Python 3.6 is used. Perform the following operations to change the Python interpreter path in the cloud-init executable file:

      1. Run the following command to open the cloud-init executable file:

        vim   /usr/local/bin/cloud-init
      2. Press the I key to enter Insert mode. At the beginning of the executable file, change the content that follows #! to the path of Python 3.6.

        Example of the new content on the line that starts with #!:

        #!/usr/bin/python3.6
      3. Press the Esc key to exit Insert mode, enter :wq, and then press the Enter key to save and close the file.

  • What do I do if the user data that I specified in the instance metadata when I created an instance does not or cannot run after I install cloud-init on the instance?

    1. Run the following command on the instance to check whether the user data exists in instance metadata:

      curl http://100.100.100.200/latest/user-data

      Check results:

      • If the user data exists in the instance metadata, the command output contains the user data. In this case, perform further operations to troubleshoot the issue.

      • If the user data does not exist in the instance metadata, no command output is returned.

    2. Identify the reason why the user data does not or cannot run.

      • Check whether the user data is in a valid format.

        The user data must be in a valid format to allow cloud-init to run the user data. For example, the first line of the user data must start with #!. For more information, see Manage the user data of Linux instances.

      • Check whether Security Hardening Mode was selected during instance creation.

        If Security Hardening Mode was selected during instance creation, the components of cloud-init cannot be initialized, which affects instance configurations such as metadata and user data. For more information, see Overview of ECS instance metadata.

      • Check the execution result of the user data in the /var/log/cloud-init.log file of cloud-init and troubleshoot the issue based on the returned error message.

        Example of the execution result of the user data in the log file:

         util.py[DEBUG]: Running command ['/var/lib/cloud/instance/scripts/part-001'] with allowed return codes [0] (shell=False, capture=False)
      • Check the standard output (stdout) and standard error (stderr) generated while the user data was running.

        In systemd (a system initialization tool of Linux), user data is run by the cloud-final.service system service. You can run the following command to check the stdout and stderr generated while the user data was running. Then, you can troubleshoot the issue based on the command output.

        journalctl -u cloud-final.service
      • If you still cannot identify the reason why the user data does not or cannot run after you perform the preceding operations, we recommend that you copy the user data to your on-premises computer and check whether the user data can be run on the computer.

  • After cloud-init is installed on an instance, the system configurations of the instance cannot be initialized and the user data script cannot be run. What do I do?

    1. Run the following commands to check whether the four cloud-init services are configured to run on system startup:

      systemctl is-enabled cloud-init-local.service
      systemctl is-enabled cloud-init.service
      systemctl is-enabled cloud-config.service
      systemctl is-enabled cloud-final.service

      If an error message or disabled is displayed for a cloud-init service, the service is not configured to run on system startup.

    2. Run the following commands to configure the four cloud-init services to run on system startup:

      systemctl enable cloud-init-local.service
      systemctl enable cloud-init.service
      systemctl enable cloud-config.service
      systemctl enable cloud-final.service
  • I resized the system disk when I created an instance but the root partition of the system disk was not automatically extended. What do I do?

    Cloud-init automatically resizes partitions of Linux instances. Make sure that the growpart tool is installed and that the configurations in the cloud.cfg file are correct.

    1. Run the following command to check whether the growpart tool is installed on the instance operating system:

      which growpart
    2. (Conditionally required) If the growpart tool is not installed, run one of the following commands based on your operating system to install the growpart tool:

      • RHEL series:

        sudo yum -y install cloud-utils-growpart
      • Debian series:

        sudo apt -y install cloud-guest-utils
    3. Run the following command to check whether the /etc/cloud/cloud.cfg file contains the content shown in the following figure:

      cat /etc/cloud/cloud.cfg

      配置内容

    4. (Conditionally required) If the /etc/cloud/cloud.cfg file does not contain the content, add the content to the file.

What to do next

  • You can use a cloud migration tool to migrate Linux instances to the cloud. For more information, see Migrate a server to Alibaba Cloud by using the Cloud Migration tool.

  • After you install cloud-init on an ECS instance that uses a Linux custom image, you can restart the instance to check whether cloud-init is installed. If cloud-init is installed, the hostname, software repositories, and Network Time Protocol (NTP) settings of the instance can be automatically configured.

    Sample content of the network configuration file:

    [testuser@iZbp1ios3psx4hoi******Z ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
    # Created by cloud-init on instance boot automatically, do not edit.
    #
    BOOTPROTO=dhcp
    DEVICE=eth0
    ONBOOT=yes
    STARTMODE=auto
    TYPE=Ethernet
    USERCTL=no
  • You must install the virtio driver on the source server, create a Linux image based on the source server, and then check whether the image file meets the import requirements.