Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I had an accident with /etc/passwd - using putpwent(), I replaced the first line of /etc/passwd with mine.
Unfortunately this line used to belong to root.
Now I don't have root permission, and I can't change it back.
The file belongs to user 0 group root:
-rw-r--r-- 1 0 root 1.8K Jan 9 11:33 passwd

Any idea how to recover?

share|improve this question

4 Answers 4

up vote 1 down vote accepted

You can boot using any LiveCD Linux like Ubuntu, SystemRescueCD ..etc Then locate your partition containing /etc :

# Using root user of the LiveCD session.
fdisk -l

This will show you all partitions. Now mount them one by one

mkdir /mypartition
mount /dev/sd<x> /mypartition

Until you locate the partition with your /etc.

Now you can edit your passwd file to recover it to the right version.

share|improve this answer
    
thank for your assistance, my pc is now well. –  Ran Regev Jan 9 at 11:51

When the bootloader shows up, add

init=/bin/bash

instead of booting normally edit kernel parameters, so for different distro you can find your solution but it should be according to your needs :

mount -o remount,rw /dev/sda1

and change your root credentials and then it should be ok

share|improve this answer
    
Well I didn't think to this one that can also work –  Kiwy Jan 9 at 10:29
    
Since I don't have root user, I'm afraid I'll not be able to start a single user session. what do you think? –  Ran Regev Jan 9 at 10:31
1  
at least you can try, from my point of view root user must be initiated with init=bin/bash –  klerk Jan 9 at 10:34
    
@RanRegev The bootloader is before loading kernel and going into multi or single user session. –  Zelda Jan 9 at 11:11

What should work on most systems:

  • Download a live linux distro
  • Boot on the live linux
  • Mount your partition where the file is
  • Authenticate as root on the live linux
  • Use chroot to change your root to your partition
  • Edit your file with any editor: vi, nano, ed ?

Edit 1:

  • Or you can also consider to use passwd to change root password in your new root
share|improve this answer
    
@Zelda why any form of sympathy like good luck or It is a pleasure is remove every time someone use them ? what's the problem with being polite ? –  Kiwy Jan 9 at 11:12
1  
You write it only once but you force others to many times waste their precious time to read through non-essential parts of an answer (or question). Better answer and question have no "thank you", "good luck", "I would like to know what ..." instead "What ...", etc. Keep it Answers and Questions crisp, that makes them better. –  Zelda Jan 9 at 12:04

If you have another account on the machine and still can do sudo in there (i.e. sudo asks for the accounts password and not root password). You can use that to get root permissions and change /etc/passwd.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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