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.
NoteFor 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
-
Log on to the source server.
-
Run the following commands to check whether cloud-init is installed.
CentOS series
rpm -qa | grep -i cloud-init pip list | grep -i cloud-initUbuntu 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.
ImportantVersion 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.
-
-
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
Aliyundata source. -
Alibaba Cloud cloud-init 19.1.21: This version depends on Python 3.6 or later and uses the
Aliyundata 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
Aliyundata source.ImportantThe 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 examples show how to install cloud-init by downloading and installing the binary packages on Debian 12 and CentOS Stream 9.
Debian 12
-
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 -
Run the following command to install the package.
sudo apt-get install -y ./cloud-init_23.2.2-8_all.deb -
(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 -
Run the following command to check whether the cloud-init version meets your expectation.
cloud-init --version
CentOS Stream 9
-
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 -
Run the following command to install the package.
sudo yum install -y ./cloud-init-23.2.2-8.el9.noarch.rpm -
(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' -
Run the following command to check whether the cloud-init version meets your expectation.
cloud-init --version
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
-
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-pipUbuntu/Debian
sudo apt-get -y install python3-pipOpenSUSE/SUSE
sudo zypper -n install python3-pip -
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 -
Run the following command to decompress the cloud-init installation package to the current directory.
tar -zxvf cloud-init-19.1.21.tgz -
Go to the cloud-init directory and install the dependency libraries.
cd ./cloud-init-19.1.21 pip3 install -r ./requirements.txt -
Go to the tools directory of cloud-init.
cd ./tools -
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.
NoteIf 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.
NoteUbuntu 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 7command. -
Check whether cloud-init is installed.
If
"description": "success"is returned, the installation is successful.{ "status_code": 0, "description": "success" } -
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
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.
-
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 -
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-pipDebian 9
sudo apt-get -y install python2-pip -
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 -
Go to the tools directory of cloud-init.
cd cloud-init-0.7.6a18/tools/ -
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 6command.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
-
Run the following command to check whether the cloud-init version meets your expectation.
cloud-init --version
Community edition cloud-init
-
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-pipUbuntu/Debian
sudo apt-get -y install git python3 python3-pipOpenSUSE/SUSE
sudo zypper -n install git python36 python3-pip -
Run the following command to download the cloud-init source code package by using Git.
git clone https://git.launchpad.net/cloud-init -
Go to the cloud-init directory.
cd ./cloud-init -
Run the following command to install all dependency libraries.
sudo pip3 install -r ./requirements.txt -
Run the following command to install cloud-init.
python3 setup.py install -
Modify the cloud.cfg configuration file.
-
Open the configuration file.
sudo vi /etc/cloud/cloud.cfgLocate 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: -
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:
-
-
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.
-
Run the following command to open the default cloud-init configuration file, and press
ito enter insert mode.sudo vim /etc/cloud/cloud.cfg -
Add the disabled configuration before
Example datasource configto 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: 300After 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. -
Press the
Esckey, enter:wq, and then press theEnterkey to save and close the configuration file. -
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.
-
Run the following command to open the default cloud-init configuration file, and press
ito enter edit mode.sudo vim /etc/cloud/cloud.cfg -
Change
preserve_hostname: falsein the configuration file topreserve_hostname: true.# This will cause the set+update hostname module to not operate (if true) preserve_hostname: true -
Press the
Esckey, enter:wq, and then press theEnterkey 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-eth0The 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