Skip to content
systemcoding.dev
Kernels

Magic SysRq Keys Every Kernel Developer Should Know

The SysRq commands that matter for kernel debugging, each with what it does and when not to use it, plus how to enable them and the REISUB sequence.

7 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, FreeBSD and Rust logos.

When a machine stops responding to the keyboard and the network, there is often still a way to talk to the kernel: the magic SysRq key. It is a debugging back door built into the kernel, reachable from the keyboard, from a serial console or from a write to /proc/sysrq-trigger. Each letter is one command: dump all tasks, show blocked processes, sync the disks, crash on purpose to take a dump, reboot immediately. This post lists the keys a kernel developer uses, with what each does and, for every one, when not to press it, since several of them lose data or take the machine down. The reference is Linux Magic System Request Key Hacks.

Turning it on

SysRq must be compiled in (CONFIG_MAGIC_SYSRQ=y, true on distribution kernels) and enabled at run time. The control is the sysctl kernel.sysrq:

Shell
cat /proc/sys/kernel/sysrq
Text
176

The value is a bitmask of which function groups are allowed. 0 disables it entirely and 1 enables everything. Other values enable a subset:

BitValueAllows
22Control of the console log level
34Keyboard control (SAK, unraw)
48Debugging dumps of processes and so on
516The sync command
632Remount read-only
764Signalling of processes (term, kill, oom-kill)
8128Reboot and power off
9256Nicing of all real-time tasks

The value 176 above is 128 plus 32 plus 16: reboot, remount read-only and sync. On a development machine, enable everything:

Shell
sudo sysctl -w kernel.sysrq=1
Text
kernel.sysrq = 1

To make it permanent, put kernel.sysrq = 1 in /etc/sysctl.d/. Leave it restricted on shared or production machines: anyone with a keyboard or console on a host with full SysRq can reboot it.

Three ways to send a key

On a keyboard, hold Alt and SysRq (often the Print Screen key) and press the command letter. On laptops, the Fn key may be needed. Over a serial console, send a break followed by the letter within five seconds (the exact method depends on your terminal program). From a shell, write the letter to the trigger file, which is the way you will use it most:

Shell
echo h | sudo tee /proc/sysrq-trigger
sudo dmesg | tail -n 2
Text
h
sysrq: HELP : loglevel(0-9) reboot(b) crash(c) show-all-locks(d) terminate-all-tasks(e) memory-full-oom-kill(f) kill-all-tasks(i) thaw-filesystems(j) sak(k) show-backtrace-all-active-cpus(l) show-memory-usage(m) nice-all-RT-tasks(n) poweroff(o) show-registers(p) show-all-timers(q) unraw(r) sync(s) show-task-states(t) unmount(u) force-fb(v) show-blocked-tasks(w) dump-ftrace-buffer(z)

The reply goes to the kernel log. If you are on a console and see nothing, raise the log level with a digit key first, or read dmesg. Which commands your kernel offers depends on its configuration, and h prints exactly that list.

The keys, with when not to use each

KeyWhat it doesWhen not to use it
bReboot immediately, with no sync and no unmountWhenever a clean shutdown is still possible; it can corrupt filesystems and lose buffered data
cCrash the kernel on purpose (a NULL dereference), triggering kdump if configuredOn a machine without a crash kernel loaded or one holding data you need; it is a real crash
eSend SIGTERM to every process except initOn a production host where the services should shut down in order; use systemctl while it works
fCall the OOM killer to kill one memory hogWhen the machine is not actually out of memory; it kills a process for nothing
iSend SIGKILL to every process except initWhen you need the services to flush state; SIGKILL gives no chance to
jThaw filesystems frozen by fsfreezeWhen a backup process froze them on purpose and is still copying
kSecure Access Key: kill everything on the current virtual consoleOn a server you are using over SSH; it can kill your own session
lPrint a backtrace of the active CPUsNever harmful, but on a machine with 512 CPUs the output floods the log
mPrint memory usageNever harmful, only informational
oPower the machine offWhen a clean shutdown still works; same data loss risk as b
pPrint registers and flags of the current CPUNot a blocker, just rarely informative on an idle CPU
sSync all mounted filesystemsWhen the disk itself is failing, since the sync may hang forever
tPrint the state and stack of all tasksOn a machine with tens of thousands of tasks, since the output can overflow the ring buffer
uRemount all filesystems read-onlyWhen you still want to write logs or a crash dump to disk afterwards
wPrint tasks in uninterruptible sleep (state D) with stacksNot harmful, but the useful part is only on a hung system
zDump the ftrace buffer to the consoleWhen tracing is not enabled; there is nothing to dump
0 to 9Set the console log levelWhen you want normal console output again afterwards, since it persists

Entries to remember as the developer's set: l, w, t and m for diagnosing a hang without changing anything, s then u then b for a controlled emergency reboot, and c for forcing a crash dump from a hung machine.

Diagnosing a hang without rebooting

When the machine is slow or processes are stuck, the read-only keys show why. w lists tasks in uninterruptible sleep, the ones waiting on disk or a lock:

Shell
echo w | sudo tee /proc/sysrq-trigger
sudo dmesg | tail -n 12
Text
sysrq: Show Blocked State
task:jbd2/sda2-8     state:D stack:0     pid:312   tgid:312   ppid:2      flags:0x00004000
Call Trace:
 <TASK>
 __schedule+0x3c1/0x9a0
 schedule+0x2f/0xa0
 io_schedule+0x46/0x70
 bit_wait_io+0x11/0x60
 __wait_on_bit+0x4b/0x110
 jbd2_journal_commit_transaction+0x1b3/0x1ae0
 </TASK>

The task named is stuck waiting for I/O inside the filesystem journal commit, which points at the disk. Lines like this explain a "frozen" system much faster than guessing. Use l when the problem looks like a CPU stuck in a loop, since it shows what each active CPU is executing:

Shell
echo l | sudo tee /proc/sysrq-trigger
Text
sysrq: Show backtrace of all active CPUs
NMI backtrace for cpu 3
CPU: 3 UID: 0 PID: 2411 Comm: ticker-test Tainted: G           O       6.12.0 #3
Call Trace:
 <NMI>
 ...
 ticker_spin+0x1d/0x40 [ticker]

A CPU whose backtrace sits in your own function every time you press it is stuck there. This is the quickest way to find the culprit of a soft lockup before the watchdog even fires.

The REISUB sequence for a frozen desktop

A frozen graphical session with a working kernel is the classic use, and the mnemonic is REISUB, from the old advice "Reboot Even If System Utterly Broken". Wait a few seconds between keys so each step can finish:

  1. r: take the keyboard out of raw mode, so the other keys work in a stuck X session.

  2. e: ask all processes to terminate with SIGTERM.

  3. i: kill the remaining ones with SIGKILL.

  4. s: sync the disks.

  5. u: remount read-only.

  6. b: reboot.

Modern desktops often handle this less often than they used to, but the principle remains: signal, sync, unmount, then reboot, in that order. Skipping s and u and pressing b first is how filesystems are damaged.

Using SysRq to capture a crash dump

When the machine is hung and you want a dump of memory to analyze later, c forces a crash on purpose, which hands control to kdump if a crash kernel is loaded. Check first, because without one the machine just panics:

Shell
cat /sys/kernel/kexec_crash_loaded
Text
1

A 1 means a crash kernel is ready. Then:

Shell
echo c | sudo tee /proc/sysrq-trigger

The system panics, boots the crash kernel and saves the dump, usually under /var/crash. This is the correct tool for a hang that has no oops to read. Test it first on a healthy test machine so you know the dump path works before you need it.

Security and operational notes

A few cautions apply beyond the table.

SysRq over a serial console accepts the break sequence from anyone on the line. If the console is exposed, such as a remote management console, the break-triggered keys are reachable from there. Restrict kernel.sysrq accordingly.

The trigger file is root-only, but containers with access to /proc/sysrq-trigger can reboot the host. Container runtimes mask it for this reason; do not unmask it.

The keys act on the whole machine and are not logged with your identity, only that the command ran. Write down what you pressed when you share a log.

Also inDebugging
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.