96

Is it possible to cause a kernel panic with a single command line?

What would be the most straightforward such command for a sudoing user and what would it be for a regular user, if any?

Scenarios that suggest downloading something as a part of the command do not count.

11
  • 20
    :(){ :|:& };: maybe?
    – Carl
    Feb 26, 2013 at 21:27
  • @carleeto Ok, could you explain that one to rest of us? Feb 26, 2013 at 22:30
  • 23
    @hydroparadise It's called a "forkbomb". :() defines a function called : with the body of :|:&, meaning "run : and also run : in the background". ; ends the function definition, and : calls your new function, which endlessly spawns new versions of itself until you either hit process limits or the system grinds to a halt. It's a command that effectively freezes any system without good process limits set. Don't try this at home.
    – Phoshi
    Feb 26, 2013 at 22:37
  • 1
    @Kevin You mean writing a C program, compiling it, and installing it as a driver, all in a single command line? A working example would be great. Feb 26, 2013 at 23:13
  • 2
    A forkbomb does not necessarily causes a kernel panic. OTOH, one thing that may do that is to write (as root) is to, say, dd if=/dev/urandom of=/dev/mem (depending on your kernel version, you may not have /dev/kmem). But I wouldn't use the system after that. :)
    – rbrito
    Feb 27, 2013 at 5:08

6 Answers 6

120

FreeBSD:

sysctl debug.kdb.panic=1

Linux (more info in the kernel documentation):

echo c > /proc/sysrq-trigger
10
  • 18
    echo c > /proc/sysrq-trigger sure does a good job in freezing a Linux system. But personally, an ol' good black screen of death narrating about a dramatic development of the call stack would feel like a more "canonical" kernel panic. Feb 27, 2013 at 0:01
  • 10
    On Linux, you might have to echo 1 > /proc/sys/kernel/sysrq before you are able to echo c > /proc/sysrq-trigger.
    – Christian
    Mar 19, 2014 at 8:16
  • 1
    how in OpenBSD?
    – mykhal
    Apr 24, 2014 at 13:35
  • 1
    When one needs to prove how flawed an innocent piece of hw is, this might come in handy...
    – JWL
    Jul 18, 2015 at 8:51
  • 1
    @mykhal See man.openbsd.org/ddb That will describe how to enter the kernel debugger on OpenBSD.
    – Kusalananda
    May 15, 2017 at 8:14
39
mkdir /tmp/kpanic && cd /tmp/kpanic && printf '#include <linux/kernel.h>\n#include <linux/module.h>\nMODULE_LICENSE("GPL");static int8_t* message = "buffer overrun at 0x4ba4c73e73acce54";int init_module(void){panic(message);return 0;}' > kpanic.c && printf 'obj-m += kpanic.o\nall:\n\tmake -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules' > Makefile && make && insmod kpanic.ko

Compiles a module that crashes the kernel by calling the panic function, needs root, requires make and gcc and linux kernel headers (apt install build-essential make gcc on debian/ubuntu)

Replace the "buffer overrun at 0x4ba4c73e73acce54" in the command with something interesting for more drama.

4
  • 7
    This source code looks harmless enough. Feb 23, 2016 at 21:34
  • This caused the kernel to crash, but how do you tell the kernel to dump memory and restart? Raspbian just hung for me using this. Jul 27, 2019 at 22:38
  • Update: Looks like I need to do apt-get install kdump-tools on Raspbian/Debian. Jul 27, 2019 at 23:04
  • make[1]: *** /lib/modules/5.4.58-07649-ge120df5deade/build: No such file or directory. Stop. Dec 10, 2020 at 2:19
6

The easiest thing is to hold down alt + print screen (sysrq) and press c while still holding them It does the same as echo c > /proc/sysrq-trigger A little explanation: the sysrq key is used to send low-level commands to the kernel itself, as a last resort to try to save the system. If you hold alt + print screen(sysrq) down and press another key next to them, it does the same as if you were to echo the key in that sysrq-trigger file. They call it trigger for a reason ;3 The 'c' tells the kernel to crash (cause a kernel panic)

However, you may want to see the content of 'proc/sys/kernel/sysrq'. If it is 178 or anything else, you should change it to 1. 0 is all disabled, 1 is all enabled, and anything larger than 1 is a bitmap for the specific things the kernel allows to do with sysrq.

2
  • 2
    You can also slowly type "REISUB" While holding those magic keys down to restart your computer when it completely froze under linux. R-change keyboard mode to Xlate ||E-send SigTerm to all process|| I- send SigKill to all processes (except for init of course)||S- sync all mounted drives||U-Remount all devices in read-only||B- restarts instantly without any process killing or unmounting (which we took care about before). You can also use O instead of B to shut down instead of restart ;D have a nice time crashing your system
    – Yakusho
    May 7, 2018 at 6:59
  • You only need to keep the Alt key down.
    – wizzwizz4
    Aug 10, 2020 at 19:57
5

The kernel is meant to keep running no matter what. So any way to cause a kernel panic by user interaction (other than deliberate vandalism by all-powerful root, like Bruce Ediger jokingly proposes, and most kernels today are built so most of those pranks won't work in the first place) is an extremely serious bug, that would get fixed fast.

4
  • Well, there is no much use of the kernel when the system has been completely frozen by a non-sudoing user who issued a command in the likeness of :(){ :|:& };:. Feb 26, 2013 at 23:27
  • 6
    @DesmondHume A good setup doesn't crash due to too many processes. Look in to the /etc/security/limits.conf file.
    – Vreality
    Feb 27, 2013 at 1:43
  • 4
    op (like me) might be interested in how to fire a kernel panic to debug how its infrastructure reacts when a node gets down. I for example came here looking for a way to do chaos testing in my AWS infra.
    – theist
    Apr 30, 2020 at 14:42
  • 4
    The kernel is not meant to keep running no matter what. The kernel is meant to be stable for as long as possible. If a condition is detected where the kernel cannot ensure a consistent environment, it should panic rather than attempt to process data in an unknown state. A node crashing is a lot easier to deal with then your application suddenly giving away free cars to every visitor because the hardware lost its mind and the kernel just ignored it. Jun 14, 2021 at 17:40
4

compile the following code into a module and insmod it, sure you should get a panic:

static int crash_module_init(void)

{
     printf("crash module starting\n");
     int *p = 0;

     printk("%d\n", *p);

     return 0;
}

static void crash_module_exit(void)
{
    printf("crash module exiting\n");
}

module_init(crash_module_init);
module_exit(crash_module_exit);
1
  • 3
    This will cause an oops, but not a panic. Nov 7, 2013 at 13:27
1

Try this:

dd if=/dev/urandom of=/proc/sysrq-trigger 


This did a very fast kernel panic for me, but I'm not sure how safe the process is because I did it on the live Ubuntu installation. But the kernel did spam error messages at me when I did it in the pure terminal environment.

2
  • 3
    Why is it down voted? It addresses the question asked. Nov 24, 2018 at 22:11
  • 13
    The issue with this one is it can potentially run the command to terminate all the user processes before it writes 'c' to the file.
    – user
    Aug 8, 2019 at 18:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .