使用 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM