avatarMad Machine

总结

undefined

摘要

path of the storage device for the guest OS</h2><div id="0b5c"><pre>virsh dumpxml | egrep <span class="hljs-string">'source(file|dev)'</span> <source <span class="hljs-attribute">dev</span>=<span class="hljs-string">'/dev/build1vg/myvm'</span>/></pre></div><h2 id="3882">Create a new encrypted password</h2><p id="4024">If you already have an encrypted password hash, you can use that and skip this step.</p><p id="655c">If you need to encrypt a password, use this OpenSSL command:</p><div id="cb3a"><pre><span class="hljs-variable"> </span>openssl passwd <span class="hljs-number">-1</span> <span class="hljs-number">1234568093843924</span> <span class="hljs-variable">1</span><span class="hljs-variable">LXERBPx7</span><span class="hljs-variable">NRLqg7WWlOS688TRMWbmk</span>/</pre></div><h2 id="d2b3">Use the guestfish utility to access the device</h2><p id="4621">Use guestfish to open the file or device in read-write mode.</p><div id="517b"><pre><span class="hljs-meta prompt_"># </span><span class="language-bash">guestfish --rw -a /dev/build1vg/myvm</span></pre></div><h2 id="80b5">Use guestfish to mount the device</h2><div id="d366"><pre><span class="hljs-meta prompt_">></span><span class="language-bash"><fs> launch</span> <span class="hljs-meta prompt_"> 100% </span><span class="language-bash">⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00</span></pre></div><div id="056c"><pre>><<span class="hljs-string">fs</span>> <span class="hljs-built_in">list-filesystems</span> /<span class="hljs-string">dev</span>/<span class="hljs-string">sda1</span>: <span class="hljs-string">ext3</span> /<span class="hljs-string">dev</span

观点

/<span class="hljs-string">sda2</span>: <span class="hljs-string">ext4</span> /<span class="hljs-string">dev</span>/<span class="hljs-string">sda3</span>: <span class="hljs-string">ext4</span> /<span class="hljs-string">dev</span>/<span class="hljs-string">sda4</span>: <span class="hljs-string">unknown</span> /<span class="hljs-string">dev</span>/<span class="hljs-string">sda5</span>: <span class="hljs-string">swap</span> /<span class="hljs-string">dev</span>/<span class="hljs-string">sda6</span>: <span class="hljs-string">ext4</span> ><<span class="hljs-string">fs</span>> <span class="hljs-string">mount</span> /<span class="hljs-string">dev</span>/<span class="hljs-string">sda2</span> /</pre></div><p id="e75d">In the example above, the ‘/’ file system is /dev/sda2 so the last command mounts it at ‘/’.</p><h2 id="509b">Change the password</h2><p id="1899">To change the password for the root user, you will need to edit the ‘/etc/shadow’ file with either <b>vi</b> or <b>emacs</b>.</p><p id="fbc6">Find the line containing the root user and replace the old password with the new password. Save and close the file.</p><h2 id="eb29">Quit Guestfish</h2><div id="bac6"><pre>><span class="hljs-symbol"><fs></span> <span class="hljs-keyword">quit</span></pre></div><h2 id="9d3e">Start the VM back up</h2><div id="8f37"><pre>virsh <span class="hljs-literal">start</span> myvm</pre></div><p id="64b2">You should be able to login using your new password.</p><blockquote id="2ba5"><p>Please consider joining Medium as a paying member. It’s 5 a month OR 50 a year for unlimited access. And if you use the following <a href="https://fongyang.medium.com/membership">link</a> it will go a long way in supporting me as a writer. Thank you!</p></blockquote></article></body>Elmo.chat 是一个 AI 助手,能够快速总结网页、YouTube 视频、PDF 文档,并支持多语言,提供深入的关键词分析和即时问答功能。

Reset the Password of a KVM Guest VM

Change the root password of a guest VM if you’ve forgotten the password.

Photo by regularguy.eth on Unsplash

This post covers resetting a forgotten root password for a KVM virtual machine on CentOS/RHEL/Ubuntu Linux. If you forget your KVM virtual machine root password, you can follow these steps to reset it.

To change the root password for a VM on a KVM server, you can use a tool called guestfish. Guestfish is a shell and command-line tool that allows you to examine and modify virtual machine file systems. So, you can use this tool to edit the virtual machine file system and then modify the /etc/shadow file to change the root password.

Install Guestfish

If you don’t already have guestfish installed, use these commands to install it:

  • For CentOS/RHEL/Rocky/AlmaLinux
# yum install libguestfish-tools
  • For Fedora or newer versions of CentOS or RHEL derivatives:
# dnf install libguestfs-tools
  • For Ubuntu/Debian:
# apt-get install libguestfs-tools

Shutdown the VM

# virsh shutdown myvm

Get the path of the storage device for the guest OS

virsh dumpxml | egrep 'source(file|dev)'
      <source dev='/dev/build1vg/myvm'/>

Create a new encrypted password

If you already have an encrypted password hash, you can use that and skip this step.

If you need to encrypt a password, use this OpenSSL command:

$ openssl passwd -1 1234568093843924
$1$LXERBPx7$NRLqg7WWlOS688TRMWbmk/

Use the guestfish utility to access the device

Use guestfish to open the file or device in read-write mode.

# guestfish --rw -a /dev/build1vg/myvm

Use guestfish to mount the device

><fs> launch
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
><fs> list-filesystems
/dev/sda1: ext3
/dev/sda2: ext4
/dev/sda3: ext4
/dev/sda4: unknown
/dev/sda5: swap
/dev/sda6: ext4
><fs> mount /dev/sda2 /

In the example above, the ‘/’ file system is /dev/sda2 so the last command mounts it at ‘/’.

Change the password

To change the password for the root user, you will need to edit the ‘/etc/shadow’ file with either vi or emacs.

Find the line containing the root user and replace the old password with the new password. Save and close the file.

Quit Guestfish

><fs> quit

Start the VM back up

virsh start myvm

You should be able to login using your new password.

Please consider joining Medium as a paying member. It’s $5 a month OR $50 a year for unlimited access. And if you use the following link it will go a long way in supporting me as a writer. Thank you!

Linux Tutorial
Linux
Recommended from ReadMedium
avatarKuldeepkumawat
How To Use the Linux Fuser Command

4 min read