There is also a second, more advanced method that can be used if you only had a root account on your ubuntu machine, or if any other accounts are unable to use the sudo command. This method uses grub to get past the operating system altogether, and allows you to change the password of the root user without needing to know any passwords.<\/span><\/p>\nMethod 2: Grub (No account needed)<\/h3>\nStep 1: Enter the grub options screen.<\/h4>\n
Restart your machine. If Ubuntu is your only operating system on the machine, then hold shift after the bios screen passes to enter the operating system selection menu. If you have more than one operating system on your machine, then this menu shows up automatically. Press the ‘e’ key to enter the grub options screen for ubuntu.<\/p>\n
Step 2: Edit the grub option.<\/h4>\n
When in the editing menu, scroll down to the line starting with the word “linux”, and append the following to the end of the line:<\/p>\n
exec \/sbin\/init<\/pre>\nNow press Ctrl+X to boot with this new configuration.<\/p>\n
Step 3: Change the root password.<\/h4>\n
Changing the root password is now possible. Execute the “passwd” command, and enter your new password – re-enter it when prompted. Your root password has now been changed. Simply use the “reboot” command, or if that doesn’t work, use the following command to restart your system to its normal state:<\/p>\n
exec \/sbin\/init<\/pre>\n<\/span>How to change the root password on CentOS<\/span><\/span><\/h2>\nThis tutorial focuses on CentOS 7 – the procedure for a password reset actually differs between CentOS 7 and previous versions of CentOS, which is why we will only focus on the latest version. <\/span><\/p>\nResetting your root password is not as easy on CentOS as it is on Ubuntu. There are more steps, and each command must be correct, so be careful with your commands.<\/span><\/p>\nStep 1: Enter the grub options screen.<\/span><\/h3>\nThis is done by either restarting your machine, or by starting it if it was already off. When the CentOS boot screen appears, press \u2018e\u2019 to enter the grub options menu. <\/span><\/p>\nStep 2: Edit the grub option.<\/span><\/h3>\nScroll down to the line starting with \u201clinux\u201d or \u201clinux16\u201d. Replace the word \u201cro\u201d with \u201crw init=\/sysroot\/bin\/sh\u201d. Then press Ctrl+X at the same time to boot using this configuration. <\/span><\/p>\nStep 3: Access the filesystem.<\/span><\/h3>\nUsing the command \u201cchroot \/sysroot\u201d, we are able to access and perform commands on the filesystem as if we were booted into the operating system. We need to do this in order to access the \u201cpasswd\u201d command.<\/span><\/p>\nStep 4: Change your root password.<\/span><\/h3>\nEnter the command \u201cpasswd root\u201d to change your root password. It will ask you to confirm the password, which is where you will enter your new password again. Once done, proceed to the next step.<\/span><\/p>\nStep 5: Fixing the \/etc\/shadow file (SELinux only)<\/span><\/h3>\nIf you have SELinux disabled, skip this step. If you do not have SELinux disabled, enter the following command to make sure the new \/etc\/shadow file that was created when you changed your password is accepted as the new file:<\/span><\/p>\ntouch \/.autorelabel<\/span><\/pre>\nAfter this is done, simply type \u201cexit\u201d, followed by another \u201cexit\u201d. If your machine hangs\/freezes, just reset your machine. While rebooting, it may take a few minutes for the auto relabeling to occur.<\/span><\/p>\nOnce you reboot, you should be able to now login as the root user with your new root password!<\/span><\/p>\n<\/span>How to change the root password on Debian<\/span><\/span><\/h2>\nChanging your root password on Debian is similar to the method used for changing it on CentOS – however, this will work on most versions of Debian. NOTE: If something on your system does not look like what we describe in this tutorial, then you should stop – you may cause your system to become unbootable. <\/span><\/p>\nStep 1: Enter the grub options screen.<\/span><\/h3>\nDo this by restarting your system and pressing \u2018e\u2019 once the boot screen shows up. Proceed to the next step when done.<\/span><\/p>\nStep 2: Edit your grub configuration.<\/span><\/h3>\nScroll down to the line that starts with \u201clinux\u201d, and go to the end of that line, either by pressing the END key on your keyboard, or by manually scrolling to the end of the line. Enter the following to boot into the \u2018#\u2019 prompt:<\/span><\/p>\ninit=\/bin\/sh<\/span><\/pre>\nDon\u2019t forget to have a blank space between the previous arguments and the new argument that you just wrote.<\/span><\/p>\nStep 3: Mount the filesystem.<\/span><\/h3>\nEnter the next command to mount your root filesystem in the read\/write mode. This is needed in order to be able to edit the root password:<\/span><\/p>\nmount -o remount rw \/<\/span><\/pre>\nOnce done, proceed to the next step.<\/span><\/p>\nStep 4: Change your root password.<\/span><\/h3>\nSimply enter the \u201cpasswd\u201d command to change your root password. Enter, then re-enter your new password when prompted. When done, just reboot your system by using the \u201creboot\u201d or \u201cexit\u201d command. If it hangs\/freezes on this screen, just reset your machine.<\/span><\/p>\n