华为私有云HCS810中Ubuntu镜像分发ECS不能修改主机名问题
背景:HCS810 Ubuntu操作系统无法自定主机名,cloud-init和UVP-tools安装完成
#vim /etc/cloud/cloud.cfg
参数preserve_hostname: true 修改成true
然后把这两行注释"set_name"和"update_hostname"
1 # The top level settings are used as module 2 # and system configuration. 3 # A set of users which may be applied and/or used by various modules 4 # when a 'default' entry is found it will reference the 'default_user' 5 # from the distro configuration specified below 6 users: 7 - default 8 9 10 # If this is set, 'root' will not be able to ssh in and they 11 # will get a message to login instead as the default $user 12 disable_root: true 13 14 # This will cause the set+update hostname module to not operate (if true) 15 preserve_hostname: true 16 17 # If you use datasource_list array, keep array items in a single line. 18 # If you use multi line array, ds-identify script won't read array items. 19 # Example datasource config 20 # datasource: 21 # Ec2: 22 # metadata_urls: [ 'blah.com' ] 23 # timeout: 5 # (defaults to 50 seconds) 24 # max_wait: 10 # (defaults to 120 seconds) 25 26 27 28 # The modules that run in the 'init' stage 29 cloud_init_modules: 30 - migrator 31 - seed_random 32 - bootcmd 33 - write-files 34 - growpart 35 - resizefs 36 - disk_setup 37 - mounts 38 #- set_hostname 39 #- update_hostname 40 - update_etc_hosts 41 - ca-certs 42 - rsyslog 43 - users-groups 44 - ssh 45 46 # The modules that run in the 'config' stage 47 cloud_config_modules: 48 # Emit the cloud config ready event 49 # this can be used by upstart jobs for 'start on cloud-config'. 50 - emit_upstart 51 - snap 52 - ssh-import-id 53 - locale 54 - set-passwords 55 - grub-dpkg 56 - apt-pipelining 57 - apt-configure 58 - ubuntu-advantage 59 - ntp 60 - timezone 61 - disable-ec2-metadata 62 - runcmd 63 - byobu 64 65 # The modules that run in the 'final' stage 66 cloud_final_modules: 67 - package-update-upgrade-install 68 - fan 69 - landscape 70 - lxd 71 - ubuntu-drivers 72 - write-files-deferred 73 - puppet 74 - chef 75 - mcollective 76 - salt-minion 77 - reset_rmc 78 - refresh_rmc_and_interface 79 - rightscale_userdata 80 - scripts-vendor 81 - scripts-per-once 82 - scripts-per-boot 83 - scripts-per-instance 84 - scripts-user 85 - ssh-authkey-fingerprints 86 - keys-to-console 87 - install-hotplug 88 - phone-home 89 - final-message 90 - power-state-change 91 92 # System and/or distro specific settings 93 # (not accessible to handlers/transforms) 94 system_info: 95 # This will affect which distro class gets used 96 distro: ubuntu 97 # Default user name + that default users groups (if added/used) 98 default_user: 99 name: ubuntu 100 lock_passwd: True 101 gecos: Ubuntu 102 groups: [adm, audio, cdrom, dialout, dip, floppy, lxd, netdev, plugdev, sudo, video] 103 sudo: ["ALL=(ALL) NOPASSWD:ALL"] 104 shell: /bin/bash 105 # Automatically discover the best ntp_client 106 ntp_client: auto 107 # Other config here will be given to the distro class and/or path classes 108 paths: 109 cloud_dir: /var/lib/cloud/ 110 templates_dir: /etc/cloud/templates/ 111 upstart_dir: /etc/init/ 112 package_mirrors: 113 - arches: [i386, amd64] 114 failsafe: 115 primary: http://archive.ubuntu.com/ubuntu 116 security: http://security.ubuntu.com/ubuntu 117 search: 118 primary: 119 - http://%(ec2_region)s.ec2.archive.ubuntu.com/ubuntu/ 120 - http://%(availability_zone)s.clouds.archive.ubuntu.com/ubuntu/ 121 - http://%(region)s.clouds.archive.ubuntu.com/ubuntu/ 122 security: [] 123 - arches: [arm64, armel, armhf] 124 failsafe: 125 primary: http://ports.ubuntu.com/ubuntu-ports 126 security: http://ports.ubuntu.com/ubuntu-ports 127 search: 128 primary: 129 - http://%(ec2_region)s.ec2.ports.ubuntu.com/ubuntu-ports/ 130 - http://%(availability_zone)s.clouds.ports.ubuntu.com/ubuntu-ports/ 131 - http://%(region)s.clouds.ports.ubuntu.com/ubuntu-ports/ 132 security: [] 133 - arches: [default] 134 failsafe: 135 primary: http://ports.ubuntu.com/ubuntu-ports 136 security: http://ports.ubuntu.com/ubuntu-ports 137 ssh_svcname: ssh
然后执行
虚拟机内执行rm -rf /var/lib/cloud/*和rm -f /var/log/cloud-init* (高危操作 切勿在生产镜像虚拟机中直接执行,要先进行测试没问题后再进行生产环境的动作)
#rm -rf /var/lib/cloud/*
#rm -f /var/log/cloud-init*
不需要重装cloud-init,关机转成镜像后测试。
#vim /etc/cloud/cloud.cfg配置禁止cloud-init接管网络
在最后一行添加
network:
config: disabled
删除/etc/cloud/cloud.cfg.d/目录下除了log相关的配置文件外的所有文件
至此问题并没有解决;
然后重新搞了一台新的镜像虚拟机
调整配置:
1、rm -rf /var/lib/cloud/*
2、rm -f /var/log/cloud-init*
3、删除/etc/cloud/cloud.cfg.d/目录下除了log相关的配置文件外的所有文件
4、vim /etc/cloud/cloud.cfg配置禁止cloud-init接管网络(可选)
#vim /etc/cloud/cloud.cfg配置禁止cloud-init接管网络
在最后一行添加
network:
config: disabled
然后制作镜像,成了 主机名能够自定义识别了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?