What is kernel panic?

Solution 1:

1 Question at a time per the FAQ

  1. What is kernel panic?
    When the kernel can't load properly or "freaks out" and fails to boot properly or crashes(see edit credit at the bottom).

  2. Why it occurs?
    Hosed updates, failing hardware, unsupported hardware, failed or missing drive or partition (see edit credit at the bottom)

  3. How can I understand kernel panic occurred?
    Watch boot prompts(turn off quiet kernel parameter) OR your machine fails to boot

  4. What effect it has on system?
    Failure to boot or system crash

  5. Does it only occur in Linux?
    No, all unix-like operating systems can have kernel panics. It's the equivalent of a Windows Blue Screen of Death

  6. How can I prevent it?
    It normally doesn't happen. Test updates and troubleshoot the problem. Use stable instead of development branches.

Additional note: Kernel panic and system failure/shutdown can be directly responsible for protecting your computer from physical damage. Examples include halting before extreme overheating or disk corruption. See edit credits at the bottom for direct quote.

edits

Per B. Roland Missing or corrupted disks or volumes can cause this as well.
(Good point and I overlooked it)

Per Kees Kernel panic can also occur while running.
note: Can happen when a function fails sort of gracefully inside the kernel, but most often happens during module or kernel loading(which is usually during boot). I thought I touched on it at "during boot or system crash", but I see how my answer to (1) stated it only occurs during boot.

Per rafalcieslak direct quote - 'There is one very important thing that must be added: The main point in the whole kernel panic is to protect your computer. The kernel freezes not only because it failed to do something, but also in order to prevent your computer from f.e. overheating, hard drives corruption, and any other hardware problems, that may occur, if some incorrect orders are executed, of a module (for example a module responsible for controlling the fan) failed to load, etc. This is why the kernel prefers to freeze, than to overcome the problem.'

Solution 2:

A kernel panic happens when the kernel detects an error from which it can't recover. A function named panic() to handle this situation (often by dumping some debug info and/or rebooting the system) was first introduced in one of the very early UNIX versions, so that's where the name comes from (and it's still called that in most UNIX-like operating systems, including Mac OS X).

The famous Blue Screen Of Death (officially called a Fatal system error, as in VMS/OpenVMS) in Microsoft's NT kernel is also a form of "kernel panic".

A linux kernel error from which the kernel can (partially or temporarily) recover is called a kernel oops (you might see those in dmesg output sometimes).

The reasons for a kernel panic can be a bug in the kernel (e.g. in a driver that is part of the kernel) or a hardware or software issue that causes an unexpected/unpredictable condition for the kernel (e.g. when it is impossible to start the init proces, or it gets killed).

To find out what caused it you can see if anything was printed on the screen or to a serial console (if you configured one). If it is configured to do so you can also have the system make a linux kernel crash dump, which saves the kernel memory to disk in case of a panic, so that a kernel developer can analyze it (of course, that requires it to be able to write to a disk). In some cases /var/log/messages might also have useful info (e.g. in some cases recovering from an oops may lead to an unrecoverable error later). There is some more detailed info here.

Solution 3:

I have a kernel panic on my system right now, Ubuntu 16.04. My computer had been complaining the boot disk was full, so I ran 'apt autoremove'. It removed what I thought were unnecessary files, and the result is the kernel panic.

So I guess any level of editing of the boot disk can cause a kernel panic. So don't fool around with it if you're not paying attention to what you are doing.

Solution 4:

@RobotHumans' answer addresses all the parts of your question. However, I would like to elaborate the causes of Kernel Panic.

Firstly, there are 2 types of Kernel Panic - Soft Kernel Panic(also known as Oops)and (Hard Kernel Panics, also knows as Aieee!).

A system runs into hard kernel panic mode when it encounters an exception that cannot be handled. Soft kernel panics are relatively easier to handle.

Causes of Kernel Panic

There are several causes, but most common are listed below:

  • Defective or Incompatible RAM is the most common and frequent cause of Kernel Panic.
  • Obsolete, Incompatible or Corrupted Kernel Extensions
  • Obsolete, Incompatible or Corrupted Kernel Drivers.
  • Hard disk corruption or issues such as bad sectors or directory corruption can also lead to kernel panic.
  • Insufficient RAM or Hard disk space
  • Defective hardware, badly written programs or hardware failures can also lead to kernel Panic.

This is also important to note that only modules that are located within the kernel space can cause kernel panic. lsmod command can be run to get a list of dynamically loaded modules.

To troubleshoot kernel panic, check /var/log/messages. Sometimes all of the information might be logged there while sometimes nothing related to kernel panic might be logged there. Although it is inevitable to run into kernel panics, there's nothing much to worry about.