Install cloud-init

更新时间:
复制 MD 格式

cloud-init is an open source initialization tool that provides initialization capabilities for Linux operating systems, such as initializing passwords, setting hostnames, and running custom scripts. If a custom image that you create on premises does not have cloud-init installed and needs to be migrated to the cloud, you must manually install cloud-init. Otherwise, ECS instances created from the custom image cannot automatically perform initialization tasks such as setting hostnames and configuring networks, which affects the efficiency of automated deployment and configuration. This topic describes how to install cloud-init on Linux operating systems.

For more information about cloud-init, see the cloud-init official documentation.

Scenarios

All Alibaba Cloud public images have cloud-init installed by default. To ensure that ECS instances created from your on-premises custom images can automatically initialize system configurations, we recommend that you refer to the following suggestions and perform the corresponding operations based on your actual situation.

  • Linux servers to be migrated to the cloud without cloud-init installed

    If your server is scheduled to be migrated to Alibaba Cloud but does not have cloud-init installed, install cloud-init before the migration to ensure that new ECS instances can automatically perform initialization tasks at startup.

    Note

    For servers that are not to be migrated to the cloud, installing cloud-init may introduce unnecessary complexity or resource usage. Decide whether to install cloud-init based on your actual environment.

  • Linux servers with a cloud-init version earlier than 0.7.9

    New versions of cloud-init usually contain more features and fixes to ensure compatibility with the latest cloud platform features. If the cloud-init version on your server is early, we recommend that you upgrade to a later version to ensure optimal compatibility and security.

  • ECS instances running on Alibaba Cloud without cloud-init installed

    If your ECS instance does not have cloud-init, we recommend that you manually install cloud-init inside the instance to ensure that the instance can complete initialization configurations.

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

  1. Log on to the source server.

  2. Run the following commands to check whether cloud-init is installed.

    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 output is returned or the version is earlier than 0.7.9: You must Step 2: Install cloud-init.

      Important

      Version 0.7.9 is an early community edition of cloud-init and cannot be used to initialize ECS instances. You must upgrade to a later version.

    • If the version is 18 or later: You do not need to install cloud-init. However, cloud-init automatically configures the network during instance initialization. If the default configuration does not meet your requirements, you can disable the network initialization feature. For more information, see Disable automatic network initialization by Alibaba Cloud.

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

      • If the cloud-init version of an instance is 23.2.2, the instance supports access to metadata in security hardening mode. For more information about instance metadata, see Instance metadata.

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

    • Other cases: You do not need to install cloud-init. You can directly perform the operations described in What to do next.

  3. To prevent data loss caused by accidental operations during the installation of cloud-init, we recommend that you back up the data of the source server first, for example, by creating snapshots.

Step 2: Install cloud-init

  • Alibaba Cloud cloud-init 23.2.2: recommended. This version depends on Python 3.6 or later and uses the Aliyun data source.

  • Alibaba Cloud cloud-init 19.1.21: This version depends on Python 3.6 or later and uses the Aliyun data source.

  • Alibaba Cloud cloud-init 0.7.6a18: This version is required for specific early operating systems, such as CentOS 6, Debian 9, and SUSE Linux Enterprise Server 12. This version depends on Python 2.7 and uses the Aliyun data source.

    Important

    The Python community has stopped technical support for Python 2.7. We recommend that you use a later version of cloud-init whenever possible to avoid potential issues with dependency libraries.

  • Community edition cloud-init: The community edition of cloud-init is maintained by the community. The latest version of Alibaba Cloud cloud-init is 23.2.2. If you need a later version of cloud-init, you can install the community edition. For more information about versions, see the cloud-init official website.

The community edition of cloud-init is the official version of the cloud-init project, whereas Alibaba Cloud cloud-init is optimized for the Alibaba Cloud platform and provides better support for Alibaba Cloud platform services. Therefore, we recommend that you use Alibaba Cloud cloud-init. Select a cloud-init edition to install based on your actual needs.

(Recommended) Alibaba Cloud cloud-init 23.2.2

The latest version of Alibaba Cloud cloud-init is 23.2.2. This version is maintained as software packages. You can install the software package suitable for your operating system version, or install from the source code package.

The following table lists the software package download URLs for each operating system:

Operating system type

Operating system version

Binary package

MD5SUM checksum

Source code package

Debian

12

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

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

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/cloud-init-23.2.2-8.tar.gz

11

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

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

CentOS Stream

9

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

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

8

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

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

Fedora

42

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/fedora42/cloud-init-23.2.2-8.fc42.noarch.rpm

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/fedora42/cloud-init-23.2.2-8.fc42.noarch.rpm.md5sum

41

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/fedora41/cloud-init-23.2.2-8.fc41.noarch.rpm

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/fedora41/cloud-init-23.2.2-8.fc41.noarch.rpm.md5sum

40

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/fedora40/cloud-init-23.2.2-8.fc40.noarch.rpm

https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloudinit/fedora40/cloud-init-23.2.2-8.fc40.noarch.rpm.md5sum

39

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

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

38

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

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

AlmaLinux

9

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

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

8

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

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

Rocky Linux

9

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

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

8

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

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

Ubuntu

24

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

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

22

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

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

20

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

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

The following examples show how to install cloud-init by downloading and installing the binary packages on Debian 12 and CentOS Stream 9.

Debian 12

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

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

    sudo apt-get install -y ./cloud-init_23.2.2-8_all.deb
  3. (Optional) To prevent the package from being updated or upgraded to a later open source version, we recommend that you run the following command to pin the cloud-init package version.

    sudo apt-mark hold cloud-init
  4. Run the following command to check whether the cloud-init version meets your expectation.

    cloud-init --version

CentOS Stream 9

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

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

    sudo yum install -y ./cloud-init-23.2.2-8.el9.noarch.rpm
  3. (Optional) To prevent the package from being updated or upgraded to a later open source version, we recommend that you run the following command to pin the cloud-init package version.

    sudo sh -c 'echo "exclude=cloud-init" >> /etc/dnf/dnf.conf'
  4. Run the following command to check whether the cloud-init version meets your expectation.

    cloud-init --version
Note

By default, Alibaba Cloud cloud-init 23.2.2 no longer maps the hostname to private-ipv4 in the /etc/hosts file. If your business requires this configuration, configure it manually. Later operating systems implement hostname connectivity through the nss-myhostname component, which is provided by the systemd-libs package (Red Hat series) or the libnss-myhostname package (Debian series) and enabled through the /etc/nsswitch.conf configuration file. For more information, see the nss-myhostname manual.

Alibaba Cloud cloud-init 19.1.21

  1. Make sure that the Python PIP dependency library is installed on the source server.

    The following examples show the commands used to install the Python3-pip dependency library on some Linux distributions.

    CentOS/Red Hat Enterprise Linux

    sudo yum -y install python3-pip

    Ubuntu/Debian

    sudo apt-get -y install python3-pip

    OpenSUSE/SUSE

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

    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 cloud-init installation package to the current directory.

    tar -zxvf cloud-init-19.1.21.tgz
  4. Go to the cloud-init directory and install the dependency libraries.

    cd ./cloud-init-19.1.21
    pip3 install -r ./requirements.txt
  5. Go to the tools directory of cloud-init.

    cd ./tools
  6. Run the following command to run the deploy.sh script that installs cloud-init.

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

    The following table describes the parameters and sample values of the deploy.sh script:

    Parameter

    Description

    Example

    <issue>

    The operating system platform type. Valid values: centos | redhat |rhel | debian | ubuntu | opensuse | sles. The values are case-sensitive. sles indicates SUSE/SLES.

    Note

    If the operating system is CentOS Stream, select centos as the operating system platform type.

    centos

    <major_version>

    The major version number of the operating system platform.

    Note

    Ubuntu 14 does not support Alibaba Cloud cloud-init 19.1.21.

    The major version number of CentOS 7.6 is 7

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

  7. Check whether cloud-init is installed.

    If "description": "success" is returned, the installation is successful.

    {
      "status_code": 0,
      "description": "success"
    }
  8. Run the following command to check whether the cloud-init version meets your expectation.

    cloud-init --version

The following sample shell scripts show how to install Alibaba Cloud cloud-init on different Linux distributions for your reference. Adjust the scripts based on your operating system before installation.

CentOS 7/8

# Check and install python3-pip
if ! python3 -c 'import setuptools' >& /dev/null; then
  yum -y install python3-pip
fi
# Back up the earlier version of cloud-init
test -d /etc/cloud && mv /etc/cloud /etc/cloud-old
# Download and decompress Alibaba Cloud cloud-init
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 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/8

# Check and install python3-pip
if ! python3 -c 'import setuptools' >& /dev/null; then
  yum -y install python3-pip
fi
# Back up the earlier version of cloud-init
test -d /etc/cloud && mv /etc/cloud /etc/cloud-old
# Download and decompress Alibaba Cloud cloud-init
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 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/20

# Check and install python3-pip
if ! python3 -c 'import setuptools' >& /dev/null; then
  apt-get install python36 python3-pip -y
fi
# Back up the earlier version of cloud-init
test -d /etc/cloud && mv /etc/cloud /etc/cloud-old
# Download and decompress Alibaba Cloud cloud-init
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 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/10

# Check and install python3-pip
if ! python3 -c 'import setuptools' >& /dev/null; then
  apt-get -y install python3-pip
fi
# Back up the earlier version of cloud-init
test -d /etc/cloud && mv /etc/cloud /etc/cloud-old
# Download and decompress Alibaba Cloud cloud-init
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 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/15

# Check and install python3-pip
if ! python3 -c 'import setuptools'>& /dev/null; then
  zypper -n install python3-pip
fi
# Back up the earlier version of cloud-init
test -d /etc/cloud && mv /etc/cloud /etc/cloud-old
# Download and decompress Alibaba Cloud cloud-init
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 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 and install python3-pip
if ! python3 -c 'import setuptools'>& /dev/null; then
  zypper -n install python3-pip
fi
# Back up the earlier version of cloud-init
test -d /etc/cloud && mv /etc/cloud /etc/cloud-old
# Download and decompress Alibaba Cloud cloud-init
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 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"

Alibaba Cloud cloud-init 0.7.6a18

Note

The Alibaba Cloud public images CentOS 6, Debian 9, and SUSE Linux Enterprise Server 12 have cloud-init-0.7.6a18 installed by default. If you need to perform tests, run the sudo mv /etc/cloud/cloud.cfg /etc/cloud/cloud.cfg_bak command to back up the configuration file first.

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

    cat /etc/issue
  2. Make sure that the Python PIP dependency library is installed on the source server.

    The following examples show the commands used to install the Python2-pip dependency library.

    CentOS 6/SUSE Linux Enterprise Server 12

    sudo yum -y install python2-pip

    Debian 9

    sudo apt-get -y install python2-pip
  3. Run the following commands to download and decompress Alibaba Cloud cloud-init 0.7.6a18.

    wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloud-init-0.7.6a18.tgz
    tar -zxvf cloud-init-0.7.6a18.tgz
  4. Go to the tools directory of cloud-init.

    cd cloud-init-0.7.6a18/tools/
  5. Run the following command to install cloud-init.

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

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

    The following table describes the parameters and sample values of the deploy.sh script:

    Parameter

    Description

    Example

    <issue>

    The operating system platform type. Valid values: centos | debian | sles. The values are case-sensitive. sles indicates SUSE/SLES.

    centos

    <major_version>

    The major version number of the operating system platform.

    The major version number of CentOS 6.5 is 6

  6. Run the following command to check whether the cloud-init version meets your expectation.

    cloud-init --version

Community edition cloud-init

  1. Make sure that Git, Python, and the Python PIP dependency library are installed on the source server.

    The following examples show the commands used to install Git, Python 3.6, and the Python3-pip dependency library on some Linux distributions.

    CentOS/Red Hat Enterprise Linux

    sudo yum -y install git python36 python3-pip

    Ubuntu/Debian

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

    OpenSUSE/SUSE

    sudo zypper -n install git python36 python3-pip
  2. Run the following command to download the cloud-init source code package by using Git.

    git clone https://git.launchpad.net/cloud-init
  3. Go to the cloud-init directory.

    cd ./cloud-init
  4. Run the following command to install all 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

      Locate the cloud_init_modules: section in the file.

      # 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
      
      # If this is set, 'root' will not be able to ssh in and they
      # will get a message to login instead as the default $user
      disable_root: true
      
      # This will cause the set+update hostname module to not operate (if true)
      preserve_hostname: false
      
      # Example datasource config
      # datasource:
      #    Ec2:
      #      metadata_urls: [ 'blah.com' ]
      #      timeout: 5 # (defaults to 50 seconds)
      #      max_wait: 10 # (defaults to 120 seconds)
      
      # The modules that run in the 'init' stage
      cloud_init_modules:
    2. Change the configurations before cloud_init_modules: to 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:
  7. Run the following command to check whether the cloud-init version meets your expectation.

    cloud-init --version

(Optional) Step 3: Configure cloud-init

Disable automatic network initialization by Alibaba Cloud

If your cloud-init version is 18 or later, cloud-init automatically initializes the network configuration. The automatically configured network is BOOTPROTO=dhcp DEVICE=eth0 ONBOOT=yes STARTMODE=auto TYPE=Ethernet USERCTL=no. If the default network configuration does not meet your business requirements, you can modify the cloud-init configuration file by performing the following operations to customize the network configuration.

  1. Run the following command to open the default cloud-init configuration file, and press i to enter insert mode.

    sudo vim /etc/cloud/cloud.cfg
  2. Add the disabled configuration before Example datasource config to disable the automatic network configuration feature of cloud-init.

    network:
      config: disabled
    # This will cause the set+update hostname module to not operate (if true)
    preserve_hostname: true
    
    network:
      config: disabled
    
    # If you use datasource_list array, keep array items in a single line.
    # If you use multi line array, ds-identify script won't read array items.
    
    datasource_list: [ AliYun ]
    
    # Example datasource config
    datasource:
        AliYun:
            timeout: 5
            max_wait: 300

    After you add this configuration, cloud-init no longer manages the network configuration files in the /etc/sysconfig/network-scripts/ directory, such as ifcfg-eth0. You must manage these files yourself.

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

  4. Customize the network configurations in the /etc/sysconfig/network-scripts/ directory based on your requirements, such as the IP address, subnet mask, and gateway.

Disable automatic hostname configuration by Alibaba Cloud

By default, cloud-init automatically sets the hostname and updates the /etc/hostname file when the instance starts. If you do not want the hostname to be modified, you can modify the cloud-init configuration file by performing the following operations.

  1. Run the following command to open the default cloud-init configuration file, and press i to enter edit mode.

    sudo vim /etc/cloud/cloud.cfg
  2. Change preserve_hostname: false in the configuration file to preserve_hostname: true.

    # This will cause the set+update hostname module to not operate (if true)
    preserve_hostname: true
  3. Press the Esc key, enter :wq, and then press the Enter key to save and close the configuration file.

What to do next

  • For Linux servers to be migrated to the cloud, you can use Server Migration Center (SMC) to perform the migration. For more information, see Migrate servers to ECS.

  • For ECS instances that run Linux custom images on Alibaba Cloud, you can restart the system to verify the results. If the system automatically configures the hostname, network, and NTP settings, cloud-init is installed. For example, run the following commands to view the network configuration file:

    sudo reboot
    cat /etc/sysconfig/network-scripts/ifcfg-eth0

    The following output indicates that the system has automatically configured network settings such as the DHCP protocol, network device, and device type through cloud-init.

    BOOTPROTO=dhcp
    DEVICE=eth0
    ONBOOT=yes
    STARTMODE=auto
    TYPE=Ethernet
    USERCTL=no

FAQ

  • What do I do if dependency libraries are missing when I install cloud-init by using Python 3?

    Different images may lack different libraries. You can install the missing libraries by using pip and then install cloud-init again.

  • What do I do if cloud-init runs abnormally and an error message is reported?

    If the default package manager of the system (such as yum) and the pip manager have installed different versions of dependency libraries, library version conflicts may occur and cause cloud-init to run abnormally. We recommend that you download the required dependency libraries based on the error messages.

    Error message

    Cause

    Troubleshooting command

    no setuptools module in python

    The python setuptools module is missing

    Take Python 3.6 as an example:

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

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

    • OpenSUSE/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

    The error message does not specify the missing dependency library

    The error message is not mapped

    Based on the libraries listed in the requirements.txt file of cloud-init, run the following command to install all dependency libraries:

    pip3 install -r requirements.txt
  • What do I do if a new version of Python 3 installed on the instance causes cloud-init to run abnormally?

    If you install a new version of Python 3 (such as Python 3.9) on the instance and set the new version as the default version (that is, you have created a symbolic link for the new Python 3 version, for example, ln -s /usr/bin/python3.9 /usr/bin/python3), the installed cloud-init runs abnormally. For example, running cloud-init --version reports the following error:

    $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 either of the following methods to resolve this issue:

    • Method 1: Reinstall cloud-init by using the new version of Python 3.

    • Method 2: Modify the cloud-init executable file to change the path of the Python interpreter to the previous version of Python 3. Take Python 3.6 as an example. Perform the following operations to modify the interpreter in the cloud-init executable file:

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

        vim   /usr/local/bin/cloud-init
      2. Press i to enter edit mode, and change the content after #! at the beginning of the file to the path of Python 3.6.

        After the modification, the #! line is as follows:

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

  • What do I do if the user data specified in the instance metadata during instance creation is not run or fails to run after cloud-init is installed?

    1. Run the following command inside the instance to check whether user-data exists in the instance metadata.

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

      Check result description:

      • If user-data is set, the corresponding user-data information is returned. Proceed to the next step for troubleshooting.

      • If no information is returned, user-data is not set.

    2. Use the following methods to troubleshoot why user-data is not run or fails to run.

      • Check whether the user-data format is correct.

        The user-data is run by cloud-init. You must ensure that the data format is correct. For example, the first line of user-data must start with #!. For more information, see Customize instance initialization configurations.

      • Check the user-data execution results in the cloud-init log file /var/log/cloud-init.log and troubleshoot the issue based on the error messages.

        The following example shows the information recorded 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)
      • View the standard output and standard error output of the user-data execution process.

        In systemd, the Linux system initialization tool, user-data is run by the cloud-final.service system service. You can run the following command to view the standard output and standard error output of the user-data execution process and troubleshoot the issue based on the output.

        journalctl -u cloud-final.service
      • If none of the preceding methods can identify why user-data is not run or fails to run, we recommend that you copy the user-data content to an on-premises host for debugging to check whether it can run successfully.

  • What do I do if the system initialization configurations and instance user data scripts cannot run as expected after cloud-init is installed?

    1. Run the following commands to check whether the four cloud-init services are set to start at 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 returned, cloud-init is not set to start at system startup.

    2. Run the following commands to set cloud-init to start at system startup.

      systemctl enable cloud-init-local.service
      systemctl enable cloud-init.service
      systemctl enable cloud-config.service
      systemctl enable cloud-final.service
  • What do I do if the root partition is not automatically expanded inside the instance after I expand the system disk during instance creation?

    Automatic partition expansion for Linux instances is supported by cloud-init. Make sure that the growpart tool is installed in the system and the cloud.cfg file is correctly configured.

    1. Run the following command to check whether the growpart tool is installed in the system.

      which growpart
    2. (Conditionally required) If the tool is not installed, run the following commands 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 following content exists in the /etc/cloud/cloud.cfg file.

      cat /etc/cloud/cloud.cfg

      Make sure that the frequency of growpart and resizefs is once-per-instance:

      # The modules that run in the 'init' stage
      cloud_init_modules:
       - migrator
       - source-address
       - pip-source
       - seed_random
       - bootcmd
       - write-files
       - [ growpart, once-per-instance ]
       - [ resizefs, once-per-instance ]
       - set_hostname
       - update_hostname
       - update_etc_hosts
       - rsyslog
       - users-groups
       - ssh
    4. (Conditionally required) If the content does not exist, add it to the /etc/cloud/cloud.cfg file.

  • What do I do if the new hostname does not take effect after I update the hostname in the console and restart an ECS instance whose cloud-init version is 0.7.6a16 or earlier?

    • Problem description

      After you update the hostname in the ECS console and restart the ECS instance, the new hostname is not set. When you run the cloud-init --version command, the cloud-init version is 0.7.6a16 or earlier.

    • Cause

      When cloud-init updates the hostname through the update_hostname module, it runs the hostname command to obtain the current hostname of the system, reads the /var/lib/cloud/data/previous-hostname file to obtain the hostname in the cloud-init data cache, and then checks whether the two are consistent. If they are consistent, the hostname is updated. If they are inconsistent, cloud-init assumes that the hostname has been manually modified and does not update the hostname.

      In Red Hat series operating systems, the line break in the /var/lib/cloud/data/previous-hostname file is processed when the file is read, but the output of the hostname command is not processed for line breaks. As a result, the two results are inconsistent and the hostname is not updated.

    • Solution

      You can use either of the following solutions to resolve this issue:

      • Upgrade the cloud-init version. For more information, see Step 2: Install cloud-init.

        • If you use CentOS 6 or earlier, use cloud-init 0.7.6a18.

        • If you use CentOS 7, use cloud-init 19.1.21.

        • If you use CentOS 8 or later, we recommend that you use cloud-init 23.2.2 or later.

      • Manually process the line break.

        Manually modify the defective cloud-init file cloudinit/distros/rhel.py (obtain the file path by running the find / -name rhel.py command). Refer to distros/rhel.py: _read_hostname() missing strip on "hostname" to process the line break in the output of the hostname command. The following example shows how to process the line break:

        diff --git a/cloudinit/distros/rhel.py b/cloudinit/distros/rhel.py
        index 0b68414e..65176e99 100644
        --- a/cloudinit/distros/rhel.py
        +++ b/cloudinit/distros/rhel.py
        @@ -143,6 +143,7 @@ class Distro(distros.Distro):
                     return util.load_file(filename).strip()
                 elif self.uses_systemd():
                     (out, _err) = util.subp(['hostname'])
        +            out = out.strip()
                     if len(out):
                         return out
                     else: