Harley Hahn's Guide to
What to Do If You Forget the Root Password
When you use your own Unix system, you are the system administrator, which means there is no one to help you if something goes wrong.
So what do you do if you forget the root (superuser) password?
Here are the steps to follow to solve this problem for a typical Linux system. The actual details may vary a bit from one system to another, but what you read here should work with most modern Linux distributions.
I won't explain all of the commands in detail, because that would take us into the realm of system administration, which is beyond the scope of this book. If there is a command you don't understand, just look it up in the online manual or ask someone for help.
If you have sudo privileges (see Chapter 6), you may be able to change the root password quickly by using the command:
sudo passwd root
You will find that more elaborate measures are necessary if you don't have sudo privileges, or if your system is configured in such a way that sudo will not let you change the root password.
The general strategy is to take control of the computer by booting from a live Linux CD. Then mount the main (root) file system that resides on your hard disk. Once this is done, you can use the mount point as the root of the file system, and then change the root password with the standard passwd program.
(If your system does not have parted, you'll have to use another partition editor such as fdisk, cfdisk or sfdisk.)
If you are not sure which is the root partition, look for a file system type of ext3, ext2, reiserfs or xfs. If there is more than one such partition, write down all their numbers.
You should now be back at the shell prompt.
mount /dev/hda2 /mnt/harley
If, in step 7, you found more than one possible partition, choose one of them. If it doesn't work, you can try another.
If the password file isn't there, you have not mounted the root partition. Go back to step 10 and try a different partition. Continue until you have successfully mounted the root partition.
There are various ways to do this. The simplest strategy is to run the passwd command using the new mount point as the root of the file system. This can be done with one simple command:
chroot /mnt/harley passwd
This chroot (change root) command means: "Change the root of the file tree temporarily to /mnt/harley, and then execute the command passwd."
Since you are in superuser mode, the passwd command will change the root password. And since the file system root is temporarily /mnt/harley, the password file that will be used is the one on the hard disk (/mnt/harley/etc/shadow).
In this way, you are able to change the root password for the system on the hard disk.
What's in a Name?
Within Unix, the name "root" has four different meanings:
Notice that, within the short set of instructions in this appendix, we have managed to use the word "root" in all four ways.
That is, we mounted the root file system that resides in the root partition, in order to make the mount point the root of the Unix file tree so we could change the root password.
© All contents Copyright 2023, Harley Hahn