Specify User Data to inject user data into ECSs to:
You can inject user data to an ECS as text or as a file.
Text: Copy the content of the user data script to the text box.
File: Save the user data script to a text file and then upload the file.

User data scripts (referred to as scripts) of Linux ECSs are based on the open-source Cloud-Init architecture. This architecture uses ECS metadata as the data source for configuring the ECSs. User data scripts are compatible with the open-source Cloud-Init. For details about Cloud-Init, see https://cloudinit.readthedocs.io/en/latest/explanation/format.html.
By default, the scripts are executed as user root.
Item |
User-Data Script |
Cloud-Config Data Script |
|---|---|---|
Description |
Scripts, such as Shell and Python scripts, are used for custom configurations. |
Methods pre-defined in Cloud-Init, such as the yum repository and SSH key, are used for configuring certain ECS applications. |
Format |
The first line must start with #! (for example, #!/bin/bash or #!/usr/bin/env python), and no spaces are allowed at the beginning. When a script is started for the first time, it will be executed at the rc.local-like level, indicating a low priority in the boot sequence. |
The first line must be #cloud-config, and no space is allowed in front of it. |
Constraint |
Before Base64 encoding, the size of the script, including the first line, cannot exceed 32 KB. |
Before Base64 encoding, the size of the script, including the first line, cannot exceed 32 KB. |
Frequency |
The script is executed only once when the ECS is started for the first time. |
The execution frequency varies according to the applications configured on the ECS. |
curl http://169.254.169.254/openstack/latest/user_data
The following describes how to inject scripts in different formats into Linux ECSs and view the script execution results.
Example 1: Inject a user-data script.
When creating an ECS, set User Data to As text and enter the user data script.
#!/bin/bash echo "Hello, the time is now $(date -R)" | tee /root/output.txt
After the ECS is created, start it and run the cat [file] command to check the script execution result.
[root@XXXXXXXX ~]# cat /root/output.txt Hello, the time is now Mon, 16 Jul 2016 16:03:18+0800
Example 2: Inject a Cloud-Config data script.
When creating an ECS, set User Data to As text and enter the user data script.
#cloud-config bootcmd: - echo 192.168.1.130 us.archive.ubuntu.com >> /etc/hosts
After the ECS is created, start it and run the cat /etc/hosts command to check the script execution result.

User data scripts of Windows ECSs are based on the open-source Cloudbase-Init architecture. This architecture uses ECS metadata as the data source for initializing and configuring the ECSs. User data scripts are compatible with the open-source Cloudbase-Init. For details about Cloudbase-Init, see https://cloudbase-init.readthedocs.io/en/latest/userdata.html.
Item |
Batch-Processing Program Script |
PowerShell Script |
|---|---|---|
Format |
The script must start with rem cmd and use it as the first line. No space is allowed at the beginning of the first line. |
The script must start with #ps1 and use it as the first line. No space is allowed at the beginning of the first line. |
Constraint |
Before Base64 encoding, the size of the script, including the first line, cannot exceed 32 KB. |
Before Base64 encoding, the size of the script, including the first line, cannot exceed 32 KB. |
The following describes how to inject scripts in different formats into Windows ECSs and view the script execution results.
Example 1: Inject a batch-processing program script.
When creating an ECS, set User Data to As text and enter the user data script.
rem cmd echo "Hello, BAT Test" > C:\1111.txt
After the ECS is created, start it and check the script execution result. In this example, a text file named 1111 is added to disk C:\.

To view the user data injected into the Windows ECS, log in at http://169.254.169.254/openstack/latest/user_data.

Example 2: Inject a PowerShell script.
When creating an ECS, set User Data to As text and enter the user data script.
#ps1 echo "Hello, Powershell Test" > C:\aaaa.txt
After the ECS is created, start it and check the script execution result. In this example, a text file named aaaa is added to disk C:\.

To view the user data injected into the Windows ECS, log in at http://169.254.169.254/openstack/latest/user_data.

This case illustrates how to inject user data to simplify Linux ECS configurations.
In Vim, to enable syntax highlighting, display line numbers, and set tab to 4 spaces, create a .vimrc script and inject it into the /root/.vimrc directory during ECS creation. After the ECS is created, vim is automatically configured based on your requirements. This improves ECS configuration efficiency, especially in batch ECS creation scenarios.
User data example:
#cloud-config
write_files:
- path: /root/.vimrc
content: |
syntax on
set tabstop=4
set number
This case illustrates how to use the user data injection function to set the password for logging in to a Linux ECS.
Parameter |
Requirement |
|---|---|
Password |
|
User data example:
#!/bin/bash echo 'root:$6$V6azyeLwcD3CHlpY$BN3VVq18fmCkj66B4zdHLWevqcxlig' | chpasswd -e;
In this command, $6$V6azyeLwcD3CHlpY$BN3VVq18fmCkj66B4zdHLWevqcxlig is the ciphertext password, which can be generated by performing the following steps:
python -c "import crypt, getpass, pwd;print crypt.mksalt()"
The following information is displayed:
$6$V6azyeLwcD3CHlpY
python -c "import crypt, getpass, pwd;print crypt.crypt('Cloud.1234','\$6\$V6azyeLwcD3CHlpY')"
The following information is displayed:
$6$V6azyeLwcD3CHlpY$BN3VVq18fmCkj66B4zdHLWevqcxlig
After the ECS is created, you can use the password to log in to it.
When you specify the adminPass field during Linux ECS creation, you can refer to this example to set the password for the ECS through user data injection.
This case illustrates how to use the user data injection function to reset the password for logging in to a Linux ECS.
In this example, the password of user root is reset to ******.
Parameter |
Requirement |
|---|---|
Password |
|
User data example (Retain the indentation in the following script):
#cloud-config
chpasswd:
list: |
root:******
expire: False
After the ECS is created, you can use the reset password to log in to it. To ensure system security, change the password of user root after logging in to the ECS for the first time.
This case illustrates how to use the user data injection function to create a user on a Windows ECS and configure the password for the user.
In this example, the user's username is abc, its password is ******, and the user is added to the administrators user group.
User data example:
rem cmd net user abc ****** /add net localgroup administrators abc /add
After the ECS is created, you can use the created username and password to log in to it.
This case illustrates how to use the user data injection function to update system software packages for a Linux ECS and enable the httpd service. After the user data is injected to an ECS, you can use the httpd service.
User data example:
#!/bin/bash yum update -y service httpd start chkconfig httpd on
This case illustrates how to use the user data injection function to assign user root permissions for remotely logging in to a Linux ECS. After the user data is injected to an ECS, you can log in to the ECS as user root using SSH key pair authentication.
User data example:
#cloud-config disable_root: false runcmd: - sed -i 's/^PermitRootLogin.*$/PermitRootLogin without-password/' /etc/ssh/sshd_config - sed -i '/^KexAlgorithms.*$/d' /etc/ssh/sshd_config - service sshd restart