I have accidentally removed /etc/ folder. Now there is no way to do any administration, because /etc/passwd and /etc/shadow are lost.

Is there any way to restore it in Ubuntu?


Here is how I would proceed.

If you want to try to manually recover, boot the ubuntu live CD and update everything

sudo apt-get update && sudo apt-get -y dist-upgrade

You then mount your ubuntu partition at /mnt

sudo -i
mount /dev/sdxy /mnt

You can the copy /etc to /mnt/etc

cp -Raf /etc /mnt

You will then need to manually edit /mnt/etc/passwd and /mnt/etc/group and /mnt/etc/sudoers

The format should be easy enough to understand from existing entries, but if not ask. Make sure your user is in the admin group.

Add in your user to those files ^^

Then chroot

sudo chroot /mnt

And set your user password

passwd your_user

You also need to update /mnt/etc/fstab , define your root partition and swap. You can use the live CD as a template for most of the entries.

reboot and hope it works.

If not, boot the live CD and back up your data to a flash driver or alternate partition and re-install.

Best of luck

  • 1
    It's not /etc/groups, but /etc/group. /etc/shadow is another important file, it contains the password for accounts. Instead of copying it off the Live CD, try recovering from an old backup in /var/backup (/mnt/var/backup on your Live CD following the instructions in this answer)
    – Lekensteyn
    Dec 7 '11 at 16:55
  • /etc/shadow will update when s/he updates the password. You may delete the user ubuntu if you wish.
    – Panther
    Dec 7 '11 at 17:24
  • ...assuming there is only one account. Better safe than sorry and restore passwd, group, shadow and gshadow from /var/backup to /etc.
    – Lekensteyn
    Dec 7 '11 at 17:29
  • if there is a backup, sure use it. If not , simple enough to add users back, can even script it.
    – Panther
    Dec 7 '11 at 19:03

Restore from backup, or reinstall. If you reinstall, use manual partitioning, and do not check the format box and your personal data should remain intact. More recent releases also have the option to reinstall/replace the existing install, without formatting. This is easier to use than manual partitioning.


Restoring files from ext4 is not really easy :-/

You could start from the Live CD and copy all the files from there into your /etc directory. But I fear that will not work.

A clean install should give you better results in less time, but everything will be lost. Personal data has to be backed up before!

  • 1
    Thank you for your answer. How can I back-up files that do not belong to me? Dec 7 '11 at 14:42
  • Using sudo, which does not work … Use a live CD, that should do. Dec 7 '11 at 18:24

You can get a list of all installed packages with

dpkg --get-selections

and you can reinstall packages with

sudo apt-get --reinstall install packagenames

With those two commands one could build one command which reinstalls all installed packages. This could help. However, I do not have the time now, to create this command for you. If someone has time - please just edit this posting.

  • sudo does not work :( Dec 7 '11 at 15:00
  • upps... :D did you setup a root password? Else you will not have the required privilegues to restore your system, then you should give it up. The problem is: the sudoers file is in /etc (/etc/sudoers) - with this file missing, sudo wont work.
    – Michael K
    Dec 7 '11 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.