Magic SysRq key

The magic SysRq key is a key combination understood by the Linux kernel, which allows the user to perform various low level commands regardless of the system's state. It is often used to recover from freezes, or to reboot a computer without corrupting the filesystem.

To be able to use this functionality the CONFIG_MAGIC_SYSRQ option has to be enabled at kernel compile time.

This key combination provides access to powerful tools for software development and disaster recovery. In this sense, it can be considered a form of escape sequence. Principal among the offered commands are means to forcibly unmount file systems, kill processes, recover keyboard state, and write unwritten data to disk. With respect to these tasks, this feature serves as a tool of last resort.

The key combination consists of Alt, SysRq and another key, which controls the command issued (as shown below).

0 through 9: Set the console log level, which controls the types of kernel messages that are output to the console
b: Immediately reboot the system, without unmounting partitions or syncing
c: Reboot kexec and output a crashdump
d: Display all currently held Locks
e: Send the SIGTERM signal to all processes except init (PID 1)
f: Call oom_kill, which kills a process to alleviate an OOM condition
g: When using Kernel Mode Setting, provides emergency support for switching back to the kernel's framebuffer console
h: Output a terse help document to the console. Any key which is not bound to a command should also do the trick
i: Send the SIGKILL signal to all processes except init
k: Kill all processes on the current virtual console (Can be used to kill X and svgalib programs, see below)
m: Output current memory information to the console
n: Reset the nice level of all high-priority and real-time tasks
o: Shut off the system
p: Output the current registers and flags to the console
q: Display all active high-resolution timers and clock sources.
r: Switch the keyboard from raw mode, the mode used by programs such as X11 and svgalib, to XLATE mode
s: Sync all mounted filesystems
t: Output a list of current tasks and their information to the console
u: Remount all mounted filesystems in read-only mode
v: Output Voyager SMP processor information
w: Display list of blocked (D state) tasks

The SysRq key can be disabled with the following command:

[root@server1 ~] # echo 0 > /proc/sys/kernel/sysrq 

To re-enable:

[root@server1 ~] # echo 1 > /proc/sys/kernel/sysrq

While this was originally implemented as part of the kernel's keyboard handler for debugging, the functionality has been also exposed via the proc filesystem and is commonly used to provide extended management capabilities to headless and remote systems. As an example, shell script can be simply used:

[root@server1 ~] # echo b > /proc/sysrq-trigger

This is equivalent to the key combination Alt + SysRq + B which reboots the machine.

The feature is controlled both by a compile-time option in the kernel configuration, CONFIG_MAGIC_SYSRQ, and a sysctl kernel parameter, kernel.sysrq.

1 comment:

  1. And remember "Raising Elephants Is So Utterly Boring" for when all else fails, you can use that as a last ditch effort to reboot gracefully.

    Directly from Wikipedia:

    unRaw (take control of keyboard back from X),
    tErminate (send SIGTERM to all processes, allowing them to terminate gracefully),
    kIll (send SIGKILL to all processes, forcing them to terminate immediately),
    Sync (flush data to disk),
    Unmount (remount all filesystems read-only),