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 file system.
This key combination provides access to features for 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.
The magic SysRq key cannot work under certain conditions, such as a kernel panic or a hardware failure preventing the kernel from running properly.

Implementation

The Linux kernel contains sysrq.c, which implements magic SysRq functionality. Magic SysRq functionality is called by serial devices at a low level in the driver code.

Commands

The key combination consists of and another key, which controls the command issued. may be released before pressing the command key, as long as remains held down.
The combinations always assume the QWERTY keyboard layout. For example, on the Dvorak keyboard layout, the combination to shut down the system uses the key instead of. Furthermore, some keyboards may not provide a separate key. In this case, a separate key should be present.
On some devices, notably laptops, the key may need to be pressed to use the magic key, e.g. on Thinkpad Carbon X1 the is activated by pressing simultaneously, then releasing and while still holding.
SysRq functionalities can also be accessed without a keyboard. See below. Note that some commands may be disabled out of the box as specified in the bitmask value in /proc/sys/kernel/sysrq.
ActionQWERTYDvorakAZERTYColemak
Set the console log level, which controls the types of kernel messages that are output to the console
Immediately reboot the system, without unmounting or syncing filesystems
Perform a system crash. A crashdump will be taken if it is configured.
Display all currently held Locks
Send the signal to all processes except
Call, which kills a process to alleviate an OOM condition
When using Kernel Mode Setting, switch to the kernel's framebuffer console.
If the in-kernel debugger is present, enter the debugger.
Output a terse help document to the console
Any key which is not bound to a command should also perform this action
Send the signal to all processes except
Forcibly "just thaw it" – filesystems frozen by the ioctl.
Kill all processes on the current virtual console
This was originally designed to imitate a secure attention key
Shows a stack backtrace for all active CPUs.
Output current memory information to the console
Reset the nice level of all high-priority and real-time tasks
Shut off the system
Output the current registers and flags to the console
Display all active high-resolution timers and clock sources.
Switch the keyboard from raw mode, used by programs such as X11 and SVGAlib, to XLATE mode
Sync all mounted file systems
Output a list of current tasks and their information to the console
Remount all mounted filesystems in read-only mode
Forcefully restores framebuffer console.
For ARM processors, cause ETM buffer dump instead.
Display list of blocked tasks
Used by xmon interface on PowerPC platforms.
Show global PMU Registers on sparc64.
Dump all TLB entries on MIPS.
Show global CPU registers
Dump the ftrace buffer
Debug dump of BPF scheduler
Replay the kernel log messages on consoles
Disables the BPF scheduler and revert to CFS

Example output of the command:

sysrq: HELP : loglevel reboot crash terminate-all-tasks memory-full-oom-kill kill-all-tasks thaw-filesystems sak show-backtrace-all-active-cpus show-memory-usage nice-all-RT-tasks poweroff show-registers show-all-timers unraw sync show-task-states unmount force-fb show-blocked-tasks dump-ftrace-buffer dump-sched-ext replay-kernel-logs reset-sched-ext

Configuration

The feature is controlled both by a compile-time option in the kernel configuration,, and a sysctl kernel parameter,.
On newer kernels, it is possible to have more fine-grained control over how the magic SysRq key can be used. On these machines, the number written to can be 0, 1, or a number greater than 1 which is a bitmask indicating which features to allow. On Ubuntu this is set at boot time to the value defined in .

Uses

Before the advent of journaled filesystems a common use of the magic SysRq key was to perform a safe reboot of a locked-up Linux computer, which lessened the risk of filesystem corruption. With modern filesystems, syncing and unmounting is still useful to force unflushed data to disk, but is no longer necessary to prevent filesystem corruption. The default value of kernel.sysrq in distributions such as Ubuntu and Debian remains 176 and 438 respectively.
Another past use was to kill a frozen graphical program, as the X Window System used to have complete control over the graphical mode and input devices.
On distributions that do not include a command, the key sequence may be able to force a switch to a text console. Some systems may require use of or instead of.

Other ways to invoke Magic SysRq

While the magic SysRq key 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. From user space programs, SysRq may be accessed by writing to .
Many embedded systems have no attached keyboard, but instead use a serial console for text input/output to the running system. It is possible to invoke a Magic SysRq feature over a serial console by sending a serial break signal, followed by the desired key. The method of sending a break is dependent on the terminal program or hardware used to connect to the serial console. A sysctl option needs to be set to enable this function.
The Linux daemons and provide a method of accessing SysRq features over a TCP connection after authenticating with a plain-text password. The daemon will invoke pre-configured SysRq triggers when system load average exceeds a certain threshold.
The Xen hypervisor has functionality to send magic commands to hosted domains via its command. Additionally, a SysRq command can be invoked from a Xen paravirtual console by sending a break sequence followed by the desired key.
The Kernel-based Virtual Machine hypervisor has functionality to send magic commands to hosted domains via its command.
eg:


virsh send-key --domain domainname --keycode KEY_LEFTALT KEY_SYSRQ KEY_S

Chromebooks have a keyboard but no dedicated SysRq key. They use instead, however some keys have a different function.
IBM Power Systems servers can invoke the Magic SysRq feature using followed by the desired key from the Hardware Management Console.
IBM mainframe partitions can invoke the Magic SysRq feature using followed by the desired key on 3270 or HMC.