If we don't know the root password and don't have root access to the machine, how can we change the root password?
Here are a few ways I can think of, from the least intrusive to the most intrusive.
With sudo: if you have
Enter your password, then enter a new password for root twice. Done.
Editing files: this works in the unlikely case you don't have full
These are the easy ones.
Single User mode: This was just explained by Renan. It works if you can get to GRUB (or your boot loader) and you can edit the Linux command line. It doesn't work if you use Debian, Ubuntu, and some others. Some boot loader configurations require a password to do so, and you must know that to proceed. Without further ado:
Your system will boot up in single user mode. Some distributions won't ask you for a root password at this point (Debian and Debian-based ones do). You're root now. Change your password:
Depending on your particular setup, these may be trivial (identical to the instructions for single user mode), or highly non-trivial: loading modules, initialising software RAID, opening encrypted volumes, starting LVM, et cetera. Without
Rescue Disk: this one's easy. Boot a rescue disk of your choice. Mount your root filesystem. The process depends on how your volumes are layered, but eventually boils down to:
Obviously, there are countless variations to the above. They all boil down to two steps:
This should work on just about any distro, I think.
If you can access the root partition from another system, e.g. a live CD, you can as root from there edit
Erase everything between the first two colons so you end up with:
To be extra careful:
Judging from the tags I take that you're using RHEL, but this solution should work equally well for all distros.
If the root password is forgotten, you can boot in single-user mode and use this to change the password. This approach is described in the Red Hat step-by-step guide:
You will eventually get to a prompt where you can type
I use Clonezilla for a lot of sysadmin-y rescue tasks. Clonezilla is a free imaging software which is awesome in itself. It also features a shell and mini-linux kernel which lets you do a LOT of things, including something like resetting a root password. It's really awesome and is a good tool to get familiar with!
My blog and videos on how to use it to reset root password: http://geekswing.com/geek/resetting-root-password-on-a-linux-system-using-clonezilla/