account blocked by changing the /etc/passwd : how to revert or gain access again

To get root access to my existing account in ubuntu i followed this post. But even after following the steps mentioned i wasn't able to create/modify file or directory without using the sudo keyword. Then i found one post which suggested that i can change the /etc/passwd and modify my username to match root:x:0:0:root:/root:/bin/bash. I followed, rebooted the system and then instead of my name in the welcome screen I saw the guest instead. While searching the net I found that root user is locked in ubuntu. Because i modified my username to match the root user, even my account is locked ( i think). How can i get it back? sudo is not working with guest account (as expected)


Solution 1:

Boot your PC while holding Ctrl+Shift, you should see the GRUB menu appear. While focusing on the first Ubuntu entry, press e to edit it.

You should see a line which ends with ro quiet splash --. Change this last part to:

rw verbose init=/bin/bash --

Don't touch the first part of the string, leave it the same as before. Now press Ctrl+X to boot. You should boot straight into a root shell. Now type:

sudo passwd sanchit

Choose a new password for your user. To recover the modified information, you should also do:

sudo nano /etc/passwd

Restore the correct user id and group for your user (they are both 1000 instead of 0). Now type the following command to mount your filesystem back in read only mode:

sync
mount -o remount,ro /

Now switch off your PC using the power button. Turn the PC on again and it should work. If I can add a bottomline: next time, think twice before making these kind of changes to system files.