3

I deleted the /etc/passwd file and cannot login now. I don't know the root password and tried lots of passwords, but none of them worked.

How can I fix this?

7
  • The easiest solution should be just using your latest backup.
    – Keba
    Jan 27, 2014 at 13:21
  • 11
    The given answer is incomplete, you'd better restore the file from backup instead of creating a new one. You can find a backup at /etc/passwd- or in /var/backups/.
    – Lekensteyn
    Jan 27, 2014 at 13:38
  • 1
    +1 for @Lekensteyn 's comment. there's a passwd.bak file under /var/backups/ .. simply restore it. :)
    – AzkerM
    Jan 27, 2014 at 13:46
  • 1
    @Lekensteyn I think it's slightly a different case, as the OP in the other question is asking to recover the /etc/passwd file from a backup which he manually made.
    – Dan
    Jan 27, 2014 at 14:52
  • 1
    what if i have deleted the passwd- as well?
    – user276607
    May 1, 2014 at 8:22

3 Answers 3

7

This question seems to already have the suitable answers, I will just expand on @Lekensteyn 's comment:

sudo cp /var/backups/passwd.bak /etc/passwd
sudo chmod 644 /etc/passwd
0
5

Here are the steps to do this graphically:

  1. Boot to a live Ubuntu environment using an Ubuntu bootable media;

  2. Start Nautilus file manager and mount your Ubuntu partition (here in the snapshot, it's the 129 GB Filesystem); snap1

    (Hit Ctrl + L to see the partition's UUID.) snap2

  3. Start Nautilus with root privilege (Press Alt + F2 and type gksu nautilus and hit enter); snap3

  4. The Ubuntu partition should show up in the side bar with its UUID. Click to mount it, and go to the /etc directory; snap4

  5. In /etc directory, scroll down to find the file named passwd- and make a copy of this in the directory (press-hold Ctrl key, place your mouse pointer over the file, press-hold the left mouse-button, drag the mouse pointer, and then release the mouse button followed by Ctrl key to make a copy in the same directory; a file named passwd- (copy) will be created); snap5

  6. Select the copy file, right-click and select rename. Then rename the file to passwd; snap6

  7. After the file has been renamed to passwd right-click and select Properties from the context menu, and in the tab Permissions set the appropriate permissions, i.e. the Owner root should have Read and write permission while for the Group and Others set the access to Read-only and close the properties window; snap7

  8. Now reboot your system and you should be able to log in with your password.


You can do the same using terminal in a live environment.

The steps:

  1. Boot to live Ubuntu session;

  2. Open a terminal or a tty and type in the command:

    sudo fdisk -l
    

    (and note your Ubuntu partition's device name which will be in the form /dev/sdX.)

  3. Mount the device,

    sudo mount /dev/sdXY /mnt
    

    (repace sdXY with your target device.)

  4. cd to the target systems /etc directory:

    cd /mnt/etc
    
  5. Use the backup file to restore, and set the appropriate permissions:

    sudo cp passwd- passwd
    sudo chmod 644 passwd
    

    Or use the backup in /var/backups/passwd.bak:

    sudo install -m 644 /mnt/var/backups/passwd.bak /mnt/var/backups/passwd
    

    (as pointed out by Lekensteyn in his comments)

  6. Then reboot your computer:

    sudo reboot
    
3
  • 4
    Even shorter: sudo install -m644 /etc/passwd- /etc/passwd. Check the timestamps with ls -ltr /etc/passwd- /var/backups/passwd.bak (the newest file is listed last). Compare two files with: sudo diff -u /etc/passwd- /var/backups/passwd.bak
    – Lekensteyn
    Jan 27, 2014 at 14:36
  • @hash just for query what is the advantage to mount with UUID?
    – sourav c.
    Jan 27, 2014 at 17:27
  • ..I have just used nautilus to do the mount operations, no manual use of UUID was done there. yes, I could have omitted the "UUID part" but I didn't.. You may note UUID being used as the partition label in the nautilus window with elevated privileges...
    – rusty
    Jan 27, 2014 at 17:39
3

If you are can login just open terminal and try this :

sudo cp /etc/passwd- /etc/passwd

sudo chmod 644 /etc/passwd

Else

Boot into recovery mode or from Ubuntu live Cd . Then, mount your old drive:

mkdir /olddrive
mount /dev/hda5 /olddrive
assuming that your old drive is on /dev/hda5.
then, cd to /olddrive/etc
type:
echo "root::0:0:Superuser:/:/bin/bash" > passwd

then reboot. That will set no passwd for root.

Then, you can login, create all of your accounts over again, etc.

You may also use the back up which is called(i think) /etc/passwd-

Edit:(another method "I don't try that before bu i think it should work also")

  • Start GRUB on boot (press ESC while booting)
  • Press e over (recovery mode)
  • Press e over the line beginning with kernel
  • Press Space bar and enter "init=/bin/bash"
  • Press enter
  • Press b
  • At command prompt type: "cp /etc/passwd- /etc/passwd"
  • Reboot to GRUB again
  • Press e over (recovery mode)
  • Press e over the line beginning with kernel
  • Press Space bar and enter "init=/bin/bash"
  • Press enter
  • Press b
  • At command prompt type "mount -o remount,rw /"
  • Type "passwd YOURUSERNAMEHERE" (IF you don't know your username type "ls /home" (that is a Lower case L and lower case S) for a list of users)
  • Enter new password at prompt
  • Reboot to normal boot.

Source

1
  • 1
    Please add a reference if copying from elsewhere
    – Wilf
    Jan 27, 2014 at 14:00

Not the answer you're looking for? Browse other questions tagged .