14

While I was trying to remove files from a SD card from the same directory, I ran sudo rm /* and now Ubuntu isn't working. When I restarted I get the error: kernel panic attempted to kill init.

I'm new to Linux. What should I do now? I don't want to loose my data.

  • 26
    You may find it relatively simpler to reinstall Ubuntu – Archisman Panigrahi Jan 22 '23 at 18:50
  • 5
    if no of the answers work boot from a LIVE SESSION and install Ubuntuusing "something else" and only mounting the partitions as you have them now. So ---without---- formatting. It will replace all the system files, keep configs intact and also keep your personal files.You will need to reinstall software you installed yourself though (these will also keep settings you changed) – Rinzwind Jan 22 '23 at 18:58
  • 1
    But all the root file system is in one partition only. – Jag Mohan Ray Jan 22 '23 at 22:20
  • 5
    does not matter You can reinstall using the "something else" option and not pick "format". Only mount your partitions, It will keep your personal files and configuration as is and only replace system files. – Rinzwind Jan 23 '23 at 01:58
  • 13
    Daily reminder that sudo doesn't mean obey me, I'm your Master, it means yeah, I seriously mean it, I checked it twice so there should be no problem. – Neinstein Jan 23 '23 at 07:22
  • If you are relatively new to Linux, then don't work as root (use sudo) unless needed (A normal user cannot damage the OS). Next thing is whenever you run commands as root "think before hitting enter". You told the system to remove the files in the root directory, and the system did it. What you would to to undo that is called "desaster recovery". – U. Windl Jan 24 '23 at 07:34
  • 9
    I don't know why nobody has mentioned explicitly: The exact command you ran will only delete files in the root directory, and will not look inside any folders. That means the majority of your data is intact. On my ubuntu system, the only files in / are the initramfs, the kernel image and the swapfile, none of which are hard to restore. – preferred_anon Jan 25 '23 at 10:30
  • 1
    One relatively safe way of handling: Take the drive from your PC, put it aside safely, get another drive and install fresh on it, THEN connect the old drive and salvage any data from the comfort of a fully installed system. Rescue environments can be tricky for a relative beginner. – rackandboneman Jan 25 '23 at 16:49

2 Answers2

32

That command would have deleted all the links in / (and any files if you happened to have any). Boot an install USB, mount the damaged root (change the sdxy to match your system) at /mnt:

sudo mount -t ext4 /dev/sdxy /mnt

Replace the links with the following:

cd /mnt
sudo ln -s usr/bin bin
sudo ln -s usr/sbin sbin
sudo ln -s usr/lib lib
sudo ln -s usr/lib32 lib32
sudo ln -s usr/lib64 lib64
sudo ln -s usr/libx32 libx32

The only other possible file that might cause problems eventually is a /swapfile if one were present. I don't, but that may be another thing to fix after you can boot normally.

terdon
  • 100,812
ubfan1
  • 17,838
  • have i deleted all the data on the pc – Jag Mohan Ray Jan 22 '23 at 18:12
  • 11
    @JagMohanRay Running that command without the -rf will only delete what is in the / directory, but will leave the other directories out of it. All your stuff should still be there. Follow what is written above by booting to a live USB. – Terrance Jan 22 '23 at 18:21
  • @Terrance i cannot understand the commands above can you guide me a bit. I have created the live usb. – Jag Mohan Ray Jan 22 '23 at 18:23
  • 1
    @JagMohanRay There isn't a lot of space here to tell you, and I also don't have time to sit down and teach you. You will have to learn a lot on your own about Linux and Ubuntu, but the information is out there. If you see https://askubuntu.com/questions/813640/how-to-mount-a-partition-from-live-usb there OP listed the drive info from a terminal window and running fdisk -l that gave what the drives and partitions are. The first command in this answer here you replace sdxy with what is listed from fdisk -l like sda1. – Terrance Jan 22 '23 at 18:30
  • @Terrance I followed all the steps but i'm still getting the error -kernel panic- not syncing:Attempted to kill init – Jag Mohan Ray Jan 22 '23 at 18:46
  • 7
    @JagMohanRay You need to edit your question then and add as much detail as you can. You might have also removed or broke the actual boot files needed for it to start. But, there is a possibility that you will need to actually reinstall the OS. When you come here to ask questions, please be as detailed as you can so it can help us help you! One thing to remember as well, AskUbuntu is a Q&A site and not a forum like https://ubuntuforums.org/ is. – Terrance Jan 22 '23 at 18:50
15

I suggest you boot a "Live CD" to get a running host.

Then find your data. If you find it, copy it to an external disk or another host. Finally, do a completely fresh install, and set up a backup solution.

If you can't find your data, (and presuming you don't have a backup) make a decision now about how important it was and whether it's worth trying to recover it. Undelete may be possible as long as you don't keep writing to the disk. Something like ddrescue might find some more data too. Or if it is business critical then you might have to pay for professional data recovery.

The other option is to pull the old disk and do a fresh OS install onto a different disk. Put the old one aside for data recovery purposes later, and work on getting a live system up.

Then set up your backups so that in the future you have better options, should something like this happen again.

Good luck!

Criggie
  • 681