From time to time I am asked how to set reset the root password on a Linux server when you get locked out. Obvious for security reasons you can’t to do this remotely, but it is pretty easy to do if you have physical access.
First, reboot the system and interrupt the grub bootloader that looks like this by pressing ‘e’. In this example we are booting from CentOS, but it works for pretty much any modern Linux distribution (SuSE, Debian, Ubuntu, etc), because they all use grub:
The when you press ‘e’ it will display a edit screen like this. If you are prompted for a password when you press ‘e’ then your system administrator has disabled editing the bootloader configuration. It is still possible to reset the root password if your bootloader his password it, but you will need to boot off of a rescue disk to do it.
Arrow down to the first section/line that starts with ‘linux16’ or ‘linuxefi’ or ‘linux’. it usually looks something like this (yes, it is a big long single line):
linux16 /vmlinuz-3.10.0-1127.19.1.el7.x86_64 root=/dev/mapper/centos-root ro rd.lvm.lv=centos/swap vconsole.font=latarcyrheb-sun16 rd.lvm.lv=centos/root crashkernel=auto vconsole.keymap=us biosdevname=0 net.ifnames=0 rd.auto=1 LANG=en_US.UTF-8
Add this to the end of that line; this is a temporary change to the bootloader that will exist only for this reboot:
init=/bin/bash
It should look something like this:
Then press control-x to boot and eventually it will give you a bash prompt. When it does, run the following commands:
# mount -o remount,rw / # passwd root <change the password> # mount -o remount,ro / # echo b > /proc/sysrq-trigger
The last line will reboot the system. That should do it! Now you should be able to log in with your new root password.
-Eric