Skip to main content

Practice Tutorial

User Data

Last Updated: 2025-02-20 19:17:12

Scenarios

User Data is a core configuration capability during the initialization phase of cloud server instances, allowing users to inject predefined parameters or automation scripts to achieve system-level customization. This feature leverages the Cloud-init component for automated configuration parsing and supports the following two data formats:

  • Recommended Approach: Use the #cloud-config declarative YAML format (supports multi-stage task orchestration and structured configuration).
  • Compatible Approach: Standard Shell script format (suitable for quick execution of command sequences).

Directions

Installing DeepSeek

  1. Log in to the cloud server console.
  2. In the left navigation tree, select "Servers" to jump to the cloud server list page. Then continue to click "New".
  3. Jump to the cloud server purchase page, select the desired image, and then expand the "Advanced Settings" option at the bottom of the page to input custom data, with a maximum support of 32KB.
#cloud-config
# 必须包含的文件头声明

# 基础系统配置
hostname: my-server
fqdn: my-server.example.com

# 用户管理
users:
  - name: admin
    ssh-authorized-keys: 
      - ssh-rsa AAAAB3NzaC...your-public-key
    sudo: ['ALL=(ALL) NOPASSWD:ALL']

# 软件包管理
package_update: true
package_upgrade: true
packages:
  - nginx
  - docker-ce

# 文件写入
write_files:
  - path: /etc/motd
    content: |
      Welcome to QingCloud Cloud Server
      Managed via Cloud-init

# 执行命令
runcmd:
  - [ systemctl, enable, docker ]
  - [ docker, run, -d, -p, 80:80, nginx ]

Shell Script Format

#!/bin/sh
# 标准Shell脚本(支持bash/sh等解释器)

# 设置主机名
hostnamectl set-hostname my-server

# 安装软件包
apt-get update && apt-get install -y nginx

# 写入文件
cat > /etc/motd <<EOF
Welcome to QingCloud Cloud Server
Managed via UserData Script
EOF

# 启动服务
systemctl enable docker
docker run -d -p 80:80 nginx

Debugging Suggestions

After successfully creating the server, you can execute the following commands to view the script execution logs:

/var/log/cloud-userdata.log