Restore OS password

Introduction

Prerequisites:

  1. The disk is not encrypted
  2. The bios is not protected by password or it is resettable by removing CMOS battery
  3. You can reach recovery mode

Note:
Having a complete hardware access to a machine, VM, etc usually means full control of a system.
Take steps to protect digital security by going the opposite direction.
They are not bullet-proof but each measure makes it more difficult for unauthorized access.
Of course it also makes it more difficult to restore your system so take actions proportional to risks.

Ubuntu

  1. Boot in recovery mode (shift / esc / del / F1-F12 to visit boot menu)
  2. Drop to root shell
  3. mount -o rw,remount /
  4. Check the username by ls /home
  5. passwd username
  6. exit

Centos 7

  1. In boot menu, move to the usual kernel you boot from
  2. Press e on the kernel to enter edit mode
  3. Append rd.break to the line starting with linux16. It should look like “linux16 … rd.break” afterwards
  4. Ctrl-X to save and exit
  5. It reboots, choose the kernel to boot from
  6. mount -o remount,rw /sysroot/
  7. chroot /sysroot
  8. passwd
  9. load policy -i
  10. chcon -t shadow_t /etc/shadow