How to force a Linux kernel panic

I received a funny request from a colleague today.

He wanted to know if I can force a kernel panic on a Linux box.

First question: Why?

It turns out they are testing some component of VMWare that will be able to detect when a machine has panicked or stopped responding or something.

Now, kernel panics are not an easy thing to initiate. The Linux kernel is reasonably strong and it should be able to handle most errors without any effort.

The request was made though and I started trying to find an answer.

Surprisingly enough, the answer was reasonably difficult to find.

I found all sorts of suggestions on the web. A few of them were:

  • Write a standard kernel driver module and let it do something like divide by zero. Then, try to insert the module with insmod. This seemed like a lot of hassle for something I was sure had an easier answer
  • Another suggestion involved taking hard drives out while the machine was running. I was not going to risk this at all!
  • Forcing an Alt-SysReq-c command from the console by running the following command:
    echo c > /proc/sysrq-trigger
    This made me hopeful, but didn't work.

Finally, it turned out the answer was simple. I found this on an old message board.

As the root user, run the following command:

cat /dev/zero > /dev/mem

Quite ingenious really. Basically, you write a whole bunch of zeroes (0s) directly to the physical memory. That stopped the machine cold!

After a few more experiments (and reboots), my colleague found that using the append character (>>) had more success in crashing the kernel than the overwrite character (>). Doesn't seem to make too much of a difference to me though. The machine still kept on dying!

I must say, I don't know enought about Linux to know if this is a true kernel panic. For our purposes though, it's exactly what we needed and it works.

So while I am sure that the need to forcefully crash a machine is something you won't be needing to quickly in the near future, I just thought I might share it with you.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Even better, just kill init.

Even better, just kill init. kill -6 1 should do the trick.

This way you don't risk a bunch of zero's causing the system to do something destructive before it actually dies.

Another way

I'm testing Xen's similar ability to restart after a virtual machine crash.
On my system, the kernel prevents writing to /dev/mem like that giving the error:
cat: write error: No space left on device

But I was able to cause a (bonafide) kernel panic by deactivating swap (swapoff /dev/sdb1 or whatever swapon -s shows) first and then using up the physical memory:
for r in /dev/ram*; do cat /dev/zero > $r; done
Be warned though, if you're logged in through ssh (or a non-root user), the kernel might reclaim enough memory to survive by killing your connection.

That is interesting!

So the kernel would kill the SSH session to protect itself? That is interesting.

Thanks for the tip.

Maybe next time the system does not want me to write to /dev/mem, this could work.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Each email address will be obfuscated in a human readble fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <sup> <sub>
  • Lines and paragraphs break automatically.

More information about formatting options

Captcha
Unfortunately, we have to check that you are human. Please prove that you are. Tests are case sensitive!
Copy the characters (respecting upper/lower case) from the image.