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?
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
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 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);
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
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
Jan 27, 2014 at 14:36
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")
init=/bin/bash
"cp /etc/passwd- /etc/passwd
"mount -o remount,rw /
"
/etc/passwd-
or in/var/backups/
.passwd.bak
file under/var/backups/
.. simply restore it. :)/etc/passwd
file from a backup which he manually made.