This guide will take to change the root password of your server.

Changing the password with root access

If you have root access with your current password and simply want to change it, then establish an SSH connection to the server via the command line and type the following command:

# passwd

Next, enter your new password twice, as shown below:

Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Please note that on a Linux distribution, the characters of your password will not appear as you type them.

Changing a password after you have lost it

Step 1: Identify the system partition

After putting your server into rescue mode, you will need to identify the system partition. You can do this with the following command:

# fdisk -l

Disk /dev/hda 40.0 GB, 40020664320 bytes
255 heads, 63 sectors/track, 4865 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 1305 10482381 83 Linux
/dev/hda2 1306 4800 28073587+ 83 Linux
/dev/hda3 4801 4865 522112+ 82 Linux swap / Solaris

Disk /dev/sda 8254 MB, 8254390272 bytes
16 heads, 32 sectors/track, 31488 cylinders
Units = cylinders of 512 * 512 = 262144 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 31488 8060912 c W95 FAT32 (LBA)

In the example above, the system partition is /dev/hda1.

If your server has a software RAID configuration, you will need to mount your raid volume (usually /dev/mdX).

Step 2: Mount the system partition

Once you’ve identified the system partition, you can mount it with the following command:

# mount /dev/hda1 /mnt/
Step 3: Edit the root partition

The system partition is locked for editing by default so you’ll need to open it for write access, which you can do with the following command:

# chroot /mnt
Step 4: Change the root password

The final step is to change the password with the following command:

# passwd

Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

When this is done, change the boot mode on your server back to boot from the hard disk and restart it. Your root password will now be changed.