Passing User Data to ECSs

Scenarios

Use the User Data function to pass user data to ECSs to:

Use Restrictions

Passing User Data

  1. Create a user data script, the format of which complies with user data script specifications. For details, see Helpful Links.
  2. When creating an ECS, set Advanced Options to Configure now, and paste the content of the user data script to the User Data text box or upload the user data file.

    You can pass 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.

    Figure 1 User Data
  3. The created ECS automatically runs Cloud-Init/Cloudbase-Init and reads the user data script upon startup.

User Data Scripts of Linux ECSs

Customized user data scripts of Linux ECSs are based on the open-source Cloud-Init architecture. This architecture uses ECS metadata as the data source for automatically configuring the ECSs. The customized script types are compatible with open-source Cloud-Init. For details about Cloud-Init, see http://cloudinit.readthedocs.io/en/latest/topics/format.html.

User Data Scripts of Windows ECSs

Customized 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 automatically configuring the ECSs. The customized script types are compatible with open-source Cloudbase-Init. For details about Cloudbase-Init, see https://cloudbase-init.readthedocs.io/en/latest/userdata.html.

Case 1

This case illustrates how to use the user data passing function to simplify Linux ECS configuration.

In this example, vim is configured to enable syntax highlighting, display line numbers, and set the tab stop to 4. The .vimrc configuration file is created and injected 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      

Case 2

This case illustrates how to use the user data passing function to set the password for logging in to a Linux ECS.

The new password must meet the password complexity requirements listed in Table 3.

Table 3 Password complexity requirements

Parameter

Requirement

Example Value

Password

  • Consists of 8 to 26 characters.
  • Contains at least three of the following character types:
    • Uppercase letters
    • Lowercase letters
    • Digits
    • Special characters: $!@%-_=+[]:./^,{}?
  • Cannot contain the username or the username spelled backwards.
  • Cannot contain more than two consecutive characters in the same sequence as they appear in the username. (This requirement applies only to Windows ECSs.)

YNbUwp!dUc9MClnv

NOTE:

The example password is generated randomly. Do not use it.

User data example:

Using a ciphertext password (recommended)
#!/bin/bash 
echo 'root:$6$V6azyeLwcD3CHlpY$BN3VVq18fmCkj66B4zdHLWevqcxlig' | chpasswd -e;

In the preceding command output, $6$V6azyeLwcD3CHlpY$BN3VVq18fmCkj66B4zdHLWevqcxlig is the ciphertext password, which can be generated as follows:

  1. Run the following command to generate an encrypted ciphertext value:

    python -c "import crypt, getpass, pwd;print crypt.mksalt()"

    The following information is displayed:

    $6$V6azyeLwcD3CHlpY
  2. Run the following command to generate a ciphertext password based on the salt value:

    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.

Case 3

This case illustrates how to use the user data passing function to reset the password for logging in to a Linux ECS.

In this example, the password of user root is reset to ******.

The new password must meet the password complexity requirements listed in Table 4.

Table 4 Password complexity requirements

Parameter

Requirement

Example Value

Password

  • Consists of 8 to 26 characters.
  • Contains at least three of the following character types:
    • Uppercase letters
    • Lowercase letters
    • Digits
    • Special characters: $!@%-_=+[]:./^,{}?
  • Cannot contain the username or the username spelled backwards.
  • Cannot contain more than two consecutive characters in the same sequence as they appear in the username. (This requirement applies only to Windows ECSs.)

YNbUwp!dUc9MClnv

NOTE:

The example password is generated randomly. Do not use it.

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.

Case 4

This case illustrates how to use the user data passing 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.

The new password must meet the password complexity requirements listed in Table 4.

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.

Case 5

This case illustrates how to use the user data passing function to update system software packages for a Linux ECS and enable the HTTPd service. After the user data is passed to an ECS, you can use the HTTPd service.

User data example:

#!/bin/bash
yum update -y
service httpd start
chkconfig httpd on

Case 6

This case illustrates how to use the user data passing function to assign user root permission for remotely logging in to a Linux ECS. After passing the file 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

Helpful Links

For more information about user data passing cases, visit the official Cloud-init/Cloudbase-init website: