OpenStack:cloud.cfg 默认配置说明

1. 用户配置

1
2
3
4
users:
- default
disable_root: 0 # 允许root登录
ssh_pwauth: 1 # 启用SSH密码认证

2. 磁盘与文件系统配置

1
2
mount_default_fields: [~, ~, 'auto', 'defaults,nofail,x-systemd.requires=cloud-init.service', '0', '2']
resize_rootfs_tmp: /dev # 根分区调整临时目录

3. SSH 配置

1
2
3
ssh_deletekeys: 1              # 删除临时SSH密钥
ssh_genkeytypes: ['rsa', 'ecdsa', 'ed25519'] # 生成的SSH密钥类型
syslog_fix_perms: ~ # 系统日志权限修复

4. 系统定制化选项

1
2
3
disable_vmware_customization: false  # 启用VMware定制
preserve_hostname: false # 不保留主机名
manage_etc_hosts: true # 管理/etc/hosts文件

5. 网络配置

1
2
network:
config: disabled # 禁用网络配置

6. 初始化模块

6.1 云初始化模块

1
2
3
4
5
6
7
8
9
10
11
12
13
cloud_init_modules:
- disk_setup # 磁盘设置
- migrator # 迁移工具
- bootcmd # 启动命令
- write-files # 写入文件
- growpart # 分区扩展
- resizefs # 文件系统调整
- set_hostname # 设置主机名
- update_hostname # 更新主机名
- update_etc_hosts # 更新hosts文件
- rsyslog # 日志系统
- users-groups # 用户和组
- ssh # SSH配置

6.2 云配置模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cloud_config_modules:
- mounts # 挂载点
- locale # 本地化设置
- set-passwords # 设置密码
- rh_subscription # Red Hat订阅
- yum-add-repo # 添加YUM仓库
- package-update-upgrade-install # 软件包管理
- timezone # 时区设置
- puppet # Puppet集成
- chef # Chef集成
- salt-minion # SaltStack集成
- mcollective # MCollective集成
- disable-ec2-metadata # 禁用EC2元数据
- runcmd # 运行命令

6.3 最终模块

1
2
3
4
5
6
7
8
9
10
11
cloud_final_modules:
- rightscale_userdata # RightScale用户数据
- scripts-per-once # 一次性脚本
- scripts-per-boot # 每次启动脚本
- scripts-per-instance # 每个实例脚本
- scripts-user # 用户脚本
- ssh-authkey-fingerprints # SSH密钥指纹
- keys-to-console # 密钥输出到控制台
- phone-home # 回拨通知
- final-message # 最终消息
- power-state-change # 电源状态变更

7. 系统信息配置

1
2
3
4
5
6
7
8
9
10
11
12
13
system_info:
default_user:
name: cloud-user # 默认用户名
lock_passwd: true # 锁定密码
gecos: Cloud User # 用户描述
groups: [adm, systemd-journal] # 用户所属组
sudo: ["ALL=(ALL) NOPASSWD:ALL"] # sudo权限
shell: /bin/bash # 默认shell
distro: rhel # 发行版类型
paths:
cloud_dir: /var/lib/cloud # 云目录
templates_dir: /etc/cloud/templates # 模板目录
ssh_svcname: sshd # SSH服务名

8. 密码设置

1
2
3
4
chpasswd:
list: |
root:intel@123 # root用户密码
expire: False # 密码永不过期

9. 重要配置说明

配置项 说明 推荐值
disable_root 是否禁用root登录 0 (允许)
ssh_pwauth 是否允许SSH密码认证 1 (允许)
resize_rootfs_tmp 根分区调整临时目录 /dev
manage_etc_hosts 是否自动管理/etc/hosts true
network.config 网络配置模式 disabled (禁用自动配置)
cloud_init_modules 初始化阶段执行的模块 按需调整
system_info.default_user 默认用户配置 根据安全要求调整

10. 最佳实践建议

  1. 安全配置

    1
    2
    disable_root: 1           # 生产环境建议禁用root登录
    ssh_pwauth: 0 # 建议使用密钥认证
  2. 密码策略

    1
    2
    chpasswd:
    expire: True # 密码过期
  3. 网络配置

    1
    2
    network:
    config: enable # 如需自动配置网络
  4. 自定义模块

    1
    2
    cloud_final_modules:
    - scripts-user # 添加自定义脚本

11. 配置文件验证

1
2
3
4
5
# 检查YAML语法
python3 -c 'import yaml, sys; yaml.safe_load(sys.stdin)' < /etc/cloud/cloud.cfg

# 测试cloud-init配置
cloud-init schema --config-file /etc/cloud/cloud.cfg