使用 guestfish 修改 qcow2 镜像文件中的 root 密码,时区等

下载 CentOS-Stream-8 qcow2 镜像文件

curl -O https://cloud.centos.org/centos/8-stream/x86_64/images/CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2

 

sha256sum

SHA256 (CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2) = a25560ab39e10594ee7a4a1dadcba7bf303b7c3c41559b4a7fc3c522540a6672

 

在宿主上通过 openssh 命令生成加密密码,如:

[root@172-16-20-18 ~]# openssl passwd -1 123456
$1$C7lylF/z$zl7lH47UASlC2tJhO.BGx0

 

使用 guestfish 进入交互命令界面后依次执行run、list-filesystems、mount等指令

复制代码
[root@172-16-20-18 wangrui]# guestfish --rw -a CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2

Welcome to guestfish, the guest filesystem shell for
editing virtual machine filesystems and disk images.

Type: ‘help’ for help on commands
      ‘man’ to read the manual
      ‘quit’ to quit the shell

><fs> run
><fs> list-filesystems
/dev/sda1: xfs
><fs> mount /dev/sda1 /
><fs>
复制代码

 

编辑 /etc/shadow,修改 root 用户密码

><fs> vi /etc/shadow

 

内容如下:

复制代码
root:!!:19017:0:99999:7:::
bin:*:18397:0:99999:7:::
daemon:*:18397:0:99999:7:::
adm:*:18397:0:99999:7:::
lp:*:18397:0:99999:7:::
sync:*:18397:0:99999:7:::
shutdown:*:18397:0:99999:7:::
halt:*:18397:0:99999:7:::
mail:*:18397:0:99999:7:::
operator:*:18397:0:99999:7:::
games:*:18397:0:99999:7:::
ftp:*:18397:0:99999:7:::
nobody:*:18397:0:99999:7:::
dbus:!!:19017::::::
systemd-coredump:!!:19017::::::
systemd-resolve:!!:19017::::::
tss:!!:19017::::::
polkitd:!!:19017::::::
unbound:!!:19017::::::
rpc:!!:19017:0:99999:7:::
sssd:!!:19017::::::
setroubleshoot:!!:19017::::::
rpcuser:!!:19017::::::
cockpit-ws:!!:19017::::::
cockpit-wsinstance:!!:19017::::::
chrony:!!:19017::::::
sshd:!!:19017::::::
rngd:!!:19017::::::
复制代码

 

将第一行两个 ":" 之间的内容替换加密密码

$1$C7lylF/z$zl7lH47UASlC2tJhO.BGx0

 

完了第一行的内容为:

root:$1$C7lylF/z$zl7lH47UASlC2tJhO.BGx0:19017:0:99999:7:::

保存退出

 

重新生成时区软连接

><fs> rm /etc/localtime
><fs> ln /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

 

编辑 rc.local 设置 sshd_config 配置

><fs> vi /etc/rc.local

增加如下内容:

sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
systrmctl restart sshd

授予可执行权限

><fs> chmod 0755 /etc/rc.local

 

quit 退出

><fs> quit

 

完了就可以上传 qcow2 到 openstack 投递虚拟机或者直接本地使用 virsh 启动 kvm 虚拟机,如:

复制代码
virt-install \
--virt-type kvm \
--name CentOS-Stream-GenericCloud-8-20220125.1.x86_64 \
--memory 4096 \
--vcpus 2 \
--import \
--hvm \
--disk CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2,size=50 \
--bridge=br1 \
--graphics vnc,listen=0.0.0.0 \
--video vga \
--noautoconsole \
--os-type=linux \
--os-variant=centos8
复制代码

 

开启 vnc 使用 vncviewer 进行验证

virsh vncdisplay CentOS-Stream-GenericCloud-8-20220125.1.x86_64

 

posted @   Nihaorz  阅读(1766)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示