如何修改openstack虛擬機密碼

1.虛擬機建立時設置密碼
計算節點安裝如下軟件包
yum install libguestfs python-libguestfs libguestfs-tools-c
配置計算節點nova配置文件/etc/nova/nova.conf,添加以下選項
[libvirt]
inject_password=true
inject_key=true
inject_partition=-1
 
重啓計算節點的nova服務
systemctl restart openstack-nova-compute libvirtd
 
user data 也能夠是 cloud-init 支持的一個 yaml 結構文件,因此還能夠經過以下方式改密碼
# cat ./cloud-config.txt
#cloud-config
chpasswd:
  list: |
    root:123456
  expire: False
建立虛擬機並設置密碼的命令以下
nova boot --user-data ./cloud-config.txt --image ...  或者openstack server create --user-data ./cloud-config.txt --image ...
示範以下
openstack server create --flavor 1c2g --user-data ./cloud-config.txt --availability-zone nova --image 75398b9c-5cd6-4936-a093-eba02984f7f0 \
--nic net-id=5ac5c948-909f-47ff-beba-a2ffaf917c5f,v4-fixed-ip=172.16.99.134 \
--nic net-id=85ae5035-203b-4ef7-b65c-397f80b5a8af,v4-fixed-ip=172.16.100.34 \
--security-group 5bb5f2b1-9210-470f-a4a7-2715220b2920  tomcat-vm4
 
2.虛擬機啓動後修改密碼
若是須要nova set-password起做用,須要image中裝有qemu-guest-agent,另外須要設置meta中的 hw_qemu_guest_agent爲yes
2.1首先須要在openstack鏡像中增長metadata信息:hw_qemu_guest_agent=yes,命令以下:
# glance image-update 75398b9c-5cd6-4936-a093-eba02984f7f0 --property hw_qemu_guest_agent=yes
+---------------------+--------------------------------------------------------------------+
| Property            | Value                                                              |
+---------------------+--------------------------------------------------------------------+
| checksum            | fc345c82d1f2f28c63a1be8b386a15e4                                   |
| container_format    | bare                                                               |
| created_at          | 2020-02-19T08:45:40Z                                               |
| direct_url          | file:///var/lib/glance/images/75398b9c-5cd6-4936-a093-eba02984f7f0 |
| disk_format         | raw                                                                |
| hw_qemu_guest_agent | yes                                                                |
| id                  | 75398b9c-5cd6-4936-a093-eba02984f7f0                               |
| min_disk            | 0                                                                  |
| min_ram             | 0                                                                  |
| name                | CentOS 7.6 64位                                                    |
| owner               | 75aed7016c86445198356e78dddde4ba                                   |
| protected           | False                                                              |
| size                | 5368709120                                                         |
| status              | active                                                             |
| tags                | []                                                                 |
| updated_at          | 2020-02-20T05:05:58Z                                               |
| virtual_size        | None                                                               |
| visibility          | public                                                             |
+---------------------+--------------------------------------------------------------------+

 

其中75398b9c-5cd6-4936-a093-eba02984f7f0爲鏡像ID
 
修改啓動後的虛擬機密碼爲123456
# nova set-password 550e9a70-8064-4180-8bab-502ee15e6647 或者 openstack server set --root-password 550e9a70-8064-4180-8bab-502ee15e6647
New password:
Again:
其中550e9a70-8064-4180-8bab-502ee15e6647爲虛擬機ID
# nova help set-password
usage: nova set-password <server>
 
Change the admin password for a server.
 
Positional arguments:
  <server>  Name or ID of server.
測試密碼是否修改爲功
# sshpass -p '123456' ssh root@172.16.99.134 w
13:28:54 up 14 min,  1 user,  load average: 0.04, 0.08, 0.13
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    10.30.1.103      13:20    8:28   0.03s  0.03s -bash
 
在openstack horizon中重建實例時支持設置密碼,一樣能夠生效了。
 
擴展1:給單個雲主機中增長metadata信息:hw_qemu_guest_agent=yes,命令以下:
nova meta 550e9a70-8064-4180-8bab-502ee15e6647  set hw_qemu_guest_agent=yes
 
擴展2:
上傳鏡像前修改密碼
virt-customize -a /tmp/CentOS7.6_x86_64.raw  --root-password password:123456
上傳鏡像
openstack image create "CentOS 7.6 64位 V2" --file /tmp/CentOS7.6_x86_64.raw --disk-format raw --container-format bare --public
相關文章
相關標籤/搜索