How to recover a deleted /etc/passwd file? [duplicate]
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?
Solution 1:
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
Solution 2:
Here are the steps to do this graphically:
Boot to a live Ubuntu environment using an Ubuntu bootable media;
-
Start Nautilus file manager and mount your Ubuntu partition (here in the snapshot, it's the 129 GB Filesystem);
(Hit Ctrl + L to see the partition's UUID.)
Start Nautilus with root privilege (Press Alt + F2 and type
gksu nautilus
and hit enter);The Ubuntu partition should show up in the side bar with its UUID. Click to mount it, and go to the
/etc
directory;In
/etc
directory, scroll down to find the file namedpasswd-
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 namedpasswd- (copy)
will be created);Select the copy file, right-click and select rename. Then rename the file to
passwd
;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;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:
Boot to live Ubuntu session;
-
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
.) -
Mount the device,
sudo mount /dev/sdXY /mnt
(repace
sdXY
with your target device.) -
cd
to the target systems/etc
directory:cd /mnt/etc
-
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)
-
Then reboot your computer:
sudo reboot
Solution 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