Cloud-init is a tool that is used to automate the initialization of Elastic Compute Service (ECS) instances, such as generating an initial password, configuring the hostname, and running custom scripts. If cloud-init is not installed on a custom image, install cloud-init on the image to ensure that the ECS instances created from the image can be initialized. This topic describes how to install and configure cloud-init on a Linux ECS instance.
For more information about cloud-init, see Cloud-init documentation.
Scenarios
By default, cloud-init is installed on all Alibaba Cloud public images. In 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 to ensure 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 Alibaba Cloud.
Before you migrate a Linux server on which cloud-init is not installed to Alibaba Cloud, install cloud-init on the server. This way, ECS instances that are created based on the Linux server can be automatically initialized during system boot.
NoteIf you do not want to migrate a Linux server to Alibaba Cloud, installing cloud-init on the server may cause unnecessary complexity or resource usage. Proceed with caution.
Cloud-init 0.7.9 or earlier is installed on Linux servers.
In most cases, more recent cloud-init versions include more features and fixes to ensure compatibility with the latest cloud platform features. If cloud-init of an outdated version is installed on your Linux server, we recommend that you upgrade cloud-init to a version later than 0.7.9 to maximize compatibility and security.
Cloud-init is not installed on ECS instances.
If cloud-init is not installed on an ECS instance, we recommend that you install cloud-init on the instance to ensure that the instance can be automatically initialized.
Step 1: Check whether cloud-init needs to be installed or upgraded
Log on to the source server.
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 the version of cloud-init is 0.7.9 or earlier, proceed to Step 2: Install cloud-init.
NoteCloud-init 0.7.9 or earlier is an initial version of the native cloud-init, which 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 of 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:
Cloud-init 23.2.2 allows you to access instance metadata in security hardening mode. For information about instance metadata, see Obtain instance metadata.
Other feature enhancements, performance improvements, error fixes, and community contributions. For more information, see ChangeLog.
If other version numbers of cloud-init are returned, perform the operations that are described in the What to do next section of this topic.
Step 2: Install cloud-init
Alibaba Cloud cloud-init 23.2.2: recommended. This version requires Python 3.6 or later.
Alibaba Cloud cloud-init 19.1.21: This version requires Python 3.6 or later.
Alibaba Cloud cloud-init 0.7.6a: Install this version on CentOS 6, Debian 9, and SUSE Linux Enterprise Server (SLES) 12. Alibaba Cloud cloud-init 0.7.6a requires Python 2.7.
NoteThe Python community no longer provides technical support for Python 2.7. To prevent 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 official versions that are publicly available and maintained by the community of developers who contribute to the project. 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.
ImportantAlibaba Cloud cloud-init 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. 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.
The following sections describe how to download the binary packages of cloud-init and install cloud-init on Debian 12 and CentOS Stream 9.
Debian 12
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-5_all.deb
Run the following command to install the Alibaba Cloud cloud-init package:
sudo apt-get install -y ./cloud-init_23.2.2-5_all.deb
(Optional) To prevent the Alibaba Cloud cloud-init package from being automatically updated or upgraded to a more recent open source version, we recommend that you run the following command to hold the package at the current version:
sudo apt-mark hold cloud-init
CentOS Stream 9
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-5.el9.noarch.rpm
Run the following command to install the Alibaba Cloud cloud-init package:
sudo yum install -y ./cloud-init-23.2.2-5.el9.noarch.rpm
(Optional) To prevent the Alibaba Cloud cloud-init package from being automatically updated or upgraded to a more recent open source version, we recommend that you run the following command to hold the package at the current version:
sudo sh -c 'echo "exclude=cloud-init" >> /etc/dnf/dnf.conf'
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 hostname-to-private IPv4 address mappings. You can manually add the hostname-to-private IPv4 address mappings to the /etc/hosts 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.
Make sure that the python-pip dependency library is installed on the source server.
In the examples, the python3-pip dependency library 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
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
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
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
Run the following command to go to the tools subdirectory of the cloud-init directory:
cd ./tools
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 type of the operating system. Valid values: centos, redhat, rhel, debian, ubuntu, opensuse, and sles. The value of this parameter is case-sensitive. sles specifies SUSE or SLES.
NoteIf the operating system is CentOS Stream, set this parameter to centos.
centos
<major_version>
The major version number of the operating system.
NoteYou cannot install Alibaba Cloud cloud-init 19.1.21 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.Check whether Alibaba Cloud cloud-init is installed.
If Alibaba Cloud cloud-init is installed,
"description": "success"
is returned.
The following section provides sample shell scripts that you can use to install Alibaba Cloud cloud-init on different Linux distributions. Modify the shell script based on the operating system.
CentOS 7/8
# Check whether the python3-pip dependency library is installed. If the python3-pip dependency library is not installed, install the dependency library.
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/8
# Check whether the python3-pip dependency library is installed. If the python3-pip dependency library is not installed, install the dependency library.
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/20
# Check whether the python3-pip dependency library is installed. If the python3-pip dependency library is not installed, install the dependency library.
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/10
# Check whether the python3-pip dependency library is installed. If the python3-pip dependency library is not installed, install the dependency library.
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/15
# Check whether the python3-pip dependency library is installed. If the python3-pip dependency library is not installed, install the dependency library.
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 the python3-pip dependency library is not installed, install the dependency library.
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.6a17
If the source server runs an operating system of an earlier version such as CentOS 6, Debian 9, or SLES 12, install Alibaba Cloud cloud-init 0.7.6a17.
By default, Alibaba Cloud cloud-init-0.7.6a17
is installed on Alibaba Cloud public images for CentOS 6, Debian 9, and SLES 12. If you want to test Alibaba Cloud cloud-init 0.7.6a17, run the mv /etc/cloud/cloud.cfg /etc/cloud/cloud.cfg_bak command to back up the configuration file.
Run the following command to check whether the operating system version is CentOS 6, Debian 9, or SLES 12:
cat /etc/issue
Run the following commands to download and decompress the Alibaba Cloud cloud-init 0.7.6a17 installation package:
wget https://ecs-image-tools.oss-cn-hangzhou.aliyuncs.com/cloud-init-0.7.6a17.tgz tar -zxvf cloud-init-0.7.6a17.tgz
Run the following command to go to the tools subdirectory of the cloud-init directory:
cd cloud-init-0.7.6a17/tools/
Run the following command to execute the deploy.sh script to install Alibaba Cloud cloud-init:
sudo bash ./deploy.sh <issue> <major_version>
For example, if the current operating system is CentOS 6, run the
sudo bash ./deploy.sh centos 6
command to install Alibaba Cloud cloud-init.The following table describes the parameters and the corresponding values in the deploy.sh script.
Parameter
Description
Example
<issue>
The type of the operating system. Valid values: centos, debian, and sles. The value of this parameter is case-sensitive. sles specifies SUSE or SLES.
centos
<major_version>
The major version number of the operating system.
The major version number of CentOS 6.5 is 6.
Install 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.
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/Red Hat Enterprise Linux
sudo yum -y install git python36 python3-pip
Ubuntu/Debian
sudo apt-get -y install git python36 python3-pip
OpenSUSE/SUSE
sudo zypper -n install git python36 python3-pip
Run the following command to download the cloud-init source code repository from Git:
git clone https://git.launchpad.net/cloud-init
Run the following command to go to the cloud-init directory:
cd ./cloud-init
Run the following command to install all required 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 cloud.cfg configuration file.
sudo vi /etc/cloud/cloud.cfg
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 the network configurations:
After you install cloud-init, open the /etc/cloud/cloud.cfg file.
sudo vim /etc/cloud/cloud.cfg
Press the I key to enter Insert mode. Add the following disabled configuration before
Example datasource config
:network: config: disabled
NoteAfter 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.
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 to modify the hostname and /etc/hosts configuration file of your ECS instance, you can perform the following operations to modify the cloud-init configuration file:
After you install cloud-init, open the /etc/cloud/cloud.cfg file.
sudo vim /etc/cloud/cloud.cfg
Press the I key to enter Insert mode and change
preserve_hostname: false
topreserve_hostname: true
.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.
What to do next
You can use a cloud migration tool to migrate Linux servers to Alibaba 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 runs 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 ECS 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
For a source server based on which an image is being created, you must install the virtio driver on the source server, create a Linux image based on the server, and then check whether the image file meets the import requirements.