Resetting the Root Password on Linux

Posted on Jul 1, 2014

The drunken scribblings of a mad man. Possibly some kind of ancient cuneiform. A random page out of the Middle Earth phone directory. Whatever it was, it was the server password, not that it matters now: the little Post-It note reminder had long since thrown itself into the waste paper bin. Somewhere a landfill sat, knowing our password and chuckling away at our misfortune.

It seems that our hero is in a bit of trouble, if only slightly self-inflicted. (Repeat after me: Passwords do not go on sticky notes.) All is not lost, however. Assuming we have physical access to the server, this is more of an inconvenience than a brick wall. Assuming we haven't encrypted our user directory, we should be able to reset the password and get going again with little effort. Provided that we have a LiveCD, that is.

Start by booting the live media and mounting the target drive that our existing Linux is installed on. One of the easiest ways to do this is with the disk for Ubuntu Server.

With our root mounted from the LiveCD, we should be able to reset the password with only one command. From an existing terminal session, run the following, where “/target” is the path that your root filesystem is mounted to. Voila!

chroot /target passwd

How it works

In Linux when we want to change a user's password we run passwd. passwd by itself presents us with a chicken-and-egg problem though: we can't use it to change the password for root until we login as root.

What if we just ran it from the LiveCD then? That wouldn't really help us either. That would simply change the password for the root user of the live session. The problem with just running passwd is that the live environment isn't the environment that needs updating.

The magic here is chroot. In this case, the util chroot makes /target appear to be the root while it runs passwd. Thus, passwd is run against the root file system of the correct environment.