Help Center > Elastic Cloud Server > User Guide > Getting Started > ECS Features > (Optional) Injecting User Data into ECSs

(Optional) Injecting User Data into ECSs

Scenarios

Use the user data injection function to inject user data into ECSs to:

  • Use scripts to simplify ECS configuration.
  • Use scripts to initialize the ECS OS configuration.
  • Upload your scripts to ECSs during ECS creation.
  • Use scripts to perform other tasks.

You can also implement these functions using file injection, but user data injection is preferred.

Use Restrictions

  • For Linux
    • The image that is used to create ECSs must have Cloud-Init installed.
    • The user data to be injected must be less than or equal to 32 KB.
    • User data uploaded as text can contain only ASCII characters. User data uploaded as a file can contain any characters.
  • For Windows
    • The image that is used to create ECSs must have Cloudbase-Init installed.
    • The user data to be injected must be less than or equal to 32 KB.
    • User data uploaded as text can contain only ASCII characters. User data uploaded as a file can contain any characters.

Injecting User Data

  1. Create a Cloud-Config script, the format of which complies with Cloud-Config script specifications. For details, see section Related Links.
  2. During ECS creation, copy the Cloud-Config script content to the user data injection text box, or select the Cloud-Config file and upload it.
  3. The created ECS automatically runs Cloud-Init/Cloudbase-Init and the Cloud-Config script upon startup.

Use Case 1

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

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

The content of the script file to be injected is as follows:

#cloud-config
write_files:
  - path: /root/.vimrc
    content: |
      syntax on
      set tabstop=4
      set number      

Use Case 2

This use 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.

NOTE:

The new password must meet the password complexity requirements. Table 1 lists the password rules.

Table 1 Password complexity requirements

Parameter

Requirement

Example Value

Password

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

test12$@

The content of the script file to be injected is as follows:

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.

Use Case 3

This use case illustrates how to use the user data injection function to activate user root permission to log in to an ECS. After injecting the user data, you can log in to the ECS as user root in SSH key authentication mode.

The content of the script file to be injected is as follows:

#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

Related Links

For more descriptions, visit either of the following web pages:

Registration