Contents

Ubuntu安装Cloud-init

Contents

系统检查升级:

sudo apt-get update && sudo apt-get upgrade

安装

sudo apt-get install cloud-init

配置

sudo vim /etc/cloud/cloud.cfg

    # /etc/cloud/cloud.cfg
    # The top level settings are used as module
    # and base 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
    # 定义要使用的用户配置,'default' 会引用下方 'default_user' 配置
    users:
    - default  # 使用默认用户配置

    # If this is set, 'root' will not be able to ssh in and they
    # will get a message to login instead as the default $user
    # 如果设置为 true,将禁用 root 用户的 SSH 登录
    disable_root: true  # 禁用 root 用户登录

    # This will cause the set+update hostname module to not operate (if true)
    # 如果为 true,主机名设置模块将不会操作
    preserve_hostname: false  # 保持主机名不变

    # If you use datasource_list array, keep array items in a single line.
    # If you use multi line array, ds-identify script won't read array items.
    # Example datasource config
    # 数据源配置示例
    # datasource:
    #   Ec2:
    #     metadata_urls: [ 'blah.com' ]
    #     timeout: 5 # (defaults to 50 seconds)
    #     max_wait: 10 # (defaults to 120 seconds)

    # The modules that run in the 'init' stage
    # 在 'init' 阶段运行的模块
    cloud_init_modules:
    - seed_random  # 生成随机数种子
    - bootcmd  # 执行引导时命令
    - write_files  # 写入文件
    - growpart  # 扩展磁盘分区
    - resizefs  # 调整文件系统大小
    - disk_setup  # 磁盘设置
    - mounts  # 挂载分区
    - set_hostname  # 设置主机名
    - update_hostname  # 更新主机名
    - update_etc_hosts  # 更新 /etc/hosts 文件
    - ca_certs  # 设置证书
    - rsyslog  # 启动 rsyslog 服务
    - users_groups  # 配置用户和组
    - ssh  # 配置 SSH 服务
    - set_passwords  # 设置用户密码

    # The modules that run in the 'config' stage
    # 在 'config' 阶段运行的模块
    cloud_config_modules:
    - wireguard  # 配置 WireGuard VPN
    - snap  # 配置 snap 软件包
    - ubuntu_autoinstall  # 启用 Ubuntu 自动安装
    - ssh_import_id  # 从 ssh 密钥导入 ID
    - keyboard  # 设置键盘布局
    - locale  # 设置地区和语言环境
    - grub_dpkg  # 配置 GRUB 引导加载程序
    - apt_pipelining  # 启用 apt 管道
    - apt_configure  # 配置 apt
    - ubuntu_pro  # 配置 Ubuntu Pro 订阅
    - ntp  # 配置 NTP 时间同步
    - timezone  # 设置时区
    - disable_ec2_metadata  # 禁用 EC2 元数据(适用于非 EC2 环境)
    - runcmd  # 执行自定义命令
    - byobu  # 安装 Byobu 终端复用器

    # The modules that run in the 'final' stage
    # 在 'final' 阶段运行的模块
    cloud_final_modules:
    - package_update_upgrade_install  # 更新并安装包
    - fan  # 配置风扇控制
    - landscape  # 配置 Landscape 管理工具
    - lxd  # 配置 LXD 容器
    - ubuntu_drivers  # 自动安装 Ubuntu 驱动
    - write_files_deferred  # 延迟写入文件
    - puppet  # 配置 Puppet 管理工具
    - chef  # 配置 Chef 管理工具
    - ansible  # 配置 Ansible 管理工具
    - mcollective  # 配置 MCollective 管理工具
    - salt_minion  # 配置 Salt Minion
    - reset_rmc  # 重置 RMC(远程管理控制)
    - scripts_vendor  # 执行厂商提供的脚本
    - scripts_per_once  # 执行一次性脚本
    - scripts_per_boot  # 每次启动时执行脚本
    - scripts_per_instance  # 每个实例执行脚本
    - scripts_user  # 执行用户自定义脚本
    - ssh_authkey_fingerprints  # 配置 SSH 密钥指纹
    - keys_to_console  # 将密钥传输到控制台
    - install_hotplug  # 安装热插拔设备
    - phone_home  # 向云服务发送信息
    - final_message  # 显示最终信息
    - power_state_change  # 处理电源状态变化

    # System and/or distro specific settings
    # 系统和/或发行版特定的设置
    # (not accessible to handlers/transforms)
    system_info:
    # This will affect which distro class gets used
    # 这将影响使用哪个发行版类
    distro: ubuntu  # 设置发行版为 Ubuntu
    
    # Default user name + that default users groups (if added/used)
    # 默认用户名和默认用户组
    default_user:
        name: ubuntu  # 设置默认用户名为 ubuntu
        lock_passwd: True  # 锁定密码(禁止设置密码)
        gecos: Ubuntu  # 用户的 GECOS 字段(用于显示的名称)
        groups: [adm, cdrom, dip, lxd, sudo]  # 用户所属的组
        sudo: ["ALL=(ALL) NOPASSWD:ALL"]  # 允许该用户使用 sudo 执行任何命令,不需要密码
        shell: /bin/bash  # 设置用户默认的 shell 为 bash

    network:
        dhcp_client_priority: [dhcpcd, dhclient, udhcpc]  # 设置 DHCP 客户端优先级
        renderers: ['netplan', 'eni', 'sysconfig']  # 配置网络渲染器
        activators: ['netplan', 'eni', 'network-manager', 'networkd']  # 网络激活器

    # Automatically discover the best ntp_client
    # 自动选择最合适的 NTP 客户端
    ntp_client: auto  # 自动选择 NTP 客户端
    
    # Other config here will be given to the distro class and/or path classes
    # 其他配置将传递给发行版类和/或路径类
    paths:
        cloud_dir: /var/lib/cloud/  # Cloud-init 数据存储目录
        templates_dir: /etc/cloud/templates/  # 模板文件目录
        
    # 配置包镜像源
    package_mirrors:
        - arches: [i386, amd64]
        failsafe:
            primary: http://repo.huaweicloud.com/ubuntu
            security: http://repo.huaweicloud.com/ubuntu
        search:
            primary:
            - http://repo.huaweicloud.com/ubuntu/
            security: []
        - arches: [arm64, armel, armhf]
        failsafe:
            primary: http://repo.huaweicloud.com/ubuntu-ports
            security: http://repo.huaweicloud.com/ubuntu-ports
        search:
            primary:
            - http://repo.huaweicloud.com/ubuntu-ports/
            security: []
        - arches: [default]
        failsafe:
            primary: http://repo.huaweicloud.com/ubuntu-ports
            security: http://repo.huaweicloud.com/ubuntu-ports

    ssh_svcname: ssh  # SSH 服务名称

配置-如果上述不起作用则使用本命令

sudo cloud-init clean && sudo cloud-init init && sudo cloud-init modules --mode=config

重启

reboot

启动-开机启动-查看状态

sudo systemctl enable cloud-init && sudo systemctl start cloud-init && sudo cloud-init status