Websoft9Archive / role_os

useful tools for OS image, e.g GNOME
Other
0 stars 1 forks source link

cloud-init reset password at OracleLinux7.9 #19

Closed qiaofeng1227 closed 2 years ago

qiaofeng1227 commented 2 years ago

Cloud-init is a init software for Linux, we wan to reset root password by Set Passwords module of Cloud-init, below is steps:

  1. Login to Linux, modify /etc/cloud/cloud.cfg, add a line password: 123456 to it
    # This will cause the set+update hostname module to not operate (if true)
    preserve_hostname: false
    password: 123456
  2. Save it
  3. Run the command cloud-init clean
  4. Reboot Linux

These steps can running OK by CentOS7.9, but running failed by OracleLinux8.4. We wan to solved it

Some path of Cloud-init for reference:

chendelin1982 commented 2 years ago

将 OracleLinux 导入等时候,选择为 CentOS 类别,reset password 功能可用

hotHeart48156 commented 2 years ago
在oraclelinux7.9中cloudinit用的是python2.7的版本,
所以使用的包是/usr/lib/python2.7/site-packages/cloudinit/,
它的main函数在cloudinit/cmd/main.py里面,
cloudinit获取metadata是使用cloudinit/sources/DataSourceAliYun.py来获取的,
不同云有不同的datasource文件,不同的datasource文件有不同的配置,
有些datasource里面写了get_password相关的函数,
比如说DataSourceExoscale.py,就可以用这个函数来获取密码,
但是aliyun并没有这个函数。
阿里云获取metadata信息的地址是http://100.100.100.200/,method是get。
cloudinit执行完成之后会生成/run/cloud-init/instance-data-sensitive.json文件,
这个文件包含所有的与ecs相关的信息,
其他的信息放在/var/lib/cloud/instances/i-j6cc4z6ox27xv4or0l6k/目录下,
目前通过/etc/cloud/cloud.cfg配置的密码是可以生效的,已经修改cloud/config/cc_set_password.py,这个文件会在日志中打印密码

image image 所有的cloud/config/下面的模块都会被/cloud/stage.py中的第911行调用 image

qiaofeng1227 commented 2 years ago
2021-12-03 08:59:35,374 - handlers.py[DEBUG]: start: modules-config/config-set-passwords: running config-set-passwords with frequency once-per-instance
2021-12-03 08:59:35,377 - util.py[DEBUG]: Writing to /var/lib/cloud/instances/i-j6c03k8hdm1zspx6lrlc/sem/config_set_passwords - wb: [644] 20 bytes
2021-12-03 08:59:35,377 - helpers.py[DEBUG]: Running config-set-passwords using lock (<FileLock using file '/var/lib/cloud/instances/i-j6c03k8hdm1zspx6lrlc/sem/config_set_passwords'>)
2021-12-03 08:59:35,377 - cc_set_passwords.py[DEBUG]: 设置的密码是:xuwei123
2021-12-03 08:59:35,378 - cc_set_passwords.py[DEBUG]: Changing password for ['root']:
2021-12-03 08:59:35,378 - cc_set_passwords.py[DEBUG]: chpasswd密码distro=<cloudinit.distros.centos.Distro object at 0x7f5c574f59d0>
2021-12-03 08:59:35,378 - cc_set_passwords.py[DEBUG]: chpasswd密码plist_in=root:xuwei123
chendelin1982 commented 2 years ago

@hotHeart48156 很不错