Proxmox KVM 模版虚拟机的Cloud-init配置

一、安装软件包

# 1. 完全卸载
apt-get purge -y cloud-init
apt-get autoremove -y

# 2. 清理残留
rm -rf /etc/cloud
rm -rf /var/lib/cloud

# 3. 更新软件源
apt-get update

# 4. 重新安装
apt-get install -y cloud-init

# 5. 重新加载 systemd
systemctl daemon-reload

二、修改配置

# The top level settings are used as module
# and system 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
users:
   - name: root
     lock_passwd: false


# 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
disable_root: false

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: false

ssh_pwauth: true
chpasswd:
  expire: false

apt:
   # This prevents cloud-init from rewriting apt's sources.list file,
   # which has been a source of surprise.
   preserve_sources_list: true

# 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)
datasource_list: [ NoCloud, ConfigDrive ]

# The modules that run in the 'init' stage
cloud_init_modules:
 - seed_random
 - bootcmd
 - write-files
 - growpart
 - resizefs
 - disk_setup
 - mounts
 - set_hostname
 - update_hostname
 - update_etc_hosts
 - ca-certs
 - rsyslog
 - users-groups
 - ssh

# The modules that run in the 'config' stage
cloud_config_modules:
 - snap
 - ssh-import-id
 - keyboard
 - locale
 - set-passwords
 - resolv-conf 
 - grub-dpkg
 - apt-pipelining
 - apt-configure
 - ntp
 - timezone
 - disable-ec2-metadata
 - runcmd
 - byobu

# The modules that run in the 'final' stage
cloud_final_modules:
 - package-update-upgrade-install
 - fan
 - landscape
 - lxd
 - write-files-deferred
 - puppet
 - chef
 - mcollective
 - salt-minion
 - reset_rmc
 - scripts-vendor
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - scripts-user
 - ssh-authkey-fingerprints
 - 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: debian
   # Default user name + that default users groups (if added/used)
   default_user:
     name: root
     lock_passwd: false
     shell: /bin/bash
   # Other config here will be given to the distro class and/or path classes
   paths:
      cloud_dir: /var/lib/cloud/
      templates_dir: /etc/cloud/templates/
   package_mirrors:
     - arches: [default]
       failsafe:
         primary: https://deb.debian.org/debian
         security: https://deb.debian.org/debian-security
   ssh_svcname: ssh

三、WHMCS中一定要选择生成key,因为默认模版会删除key,每台机器都会自动生成,防止安全隐患

dpkg-reconfigure openssh-server

四、清除临时文件,并且打包上传

# 清理 cloud-init
cloud-init clean --logs --seed

# 清理 SSH 密钥
rm -f /etc/ssh/ssh_host_*

# 清理 machine-id
truncate -s 0 /etc/machine-id

# 清理历史
history -c

# 写入DNS配置
cat > /etc/cloud/cloud.cfg.d/99_custom_dns.cfg << 'EOF'
manage_resolv_conf: true
resolv_conf:
  nameservers: ['8.8.8.8', '1.1.1.1']
  searchdomains: ['local']
  domain: fortu.cfd

bootcmd:
  - [ cloud-init, single, --name, cc_resolv_conf, --frequency, always ]

runcmd:
  - rm -f /etc/resolv.conf
  - echo "nameserver 8.8.8.8" > /etc/resolv.conf
  - echo "nameserver 1.1.1.1" >> /etc/resolv.conf
  - echo "search fortu.cfd local" >> /etc/resolv.conf
EOF

# 关机
halt

然后把文件拷贝出来即可

五、有些服务器要默认启动

systemctl enable sshd

六、安装计算机上

qm importdisk 100 debian12.qcow2 local
qm set 100 --scsi0 local:vm-100-disk-0
qm set 100 --ide0 local:cloudinit

关于Zeno Chen

本人涉及的领域较多,杂而不精 程序设计语言: Perl, Java, PHP, Python; 数据库系统: MySQL,Oracle; 偶尔做做电路板的开发,主攻STM32单片机
此条目发表在Linux分类目录。将固定链接加入收藏夹。