Magic SysRq key

Linux specific questions/information are gathered here. The main thrust of topics are applied to Centos/RedHat(RH)/Debian/Ubuntu/Gentoo distributives

Magic SysRq key

Postby lik » Sun Feb 13, 2011 1:35 pm

The magic SysRq key (System request (often abbreviated SysRq or Sys Req)) 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.

Much like Sun Microsystems's Open Firmware (OpenBoot), 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.

Magic commands

The key combination consists of Alt, SysRq and another key, which controls the command issued (as shown in the table below). Users with a keyboard layout other than QWERTY have to remember that their layout becomes QWERTY when they use one of these combinations. For example, on a Dvorak keyboard, the key below '9' and '0' counts as an 'o', not as an 'r', so it shuts the system down instead of switching the keyboard to raw mode. Furthermore, some keyboards may not provide a separate SysRq key. In this case, a separate "Print Screen" key should be present. Under graphical environments (such as Gnome or KDE) 'Alt'+'PrintScrn/SysRq'+key combination generally only leads to a screenshot being dumped. To avoid this Print Screen feature the magic SysRq combination should include the Ctrl, becoming 'Ctrl'+'Alt'+'SysRq'+key. For the same purposes the AltGr key, if present, can be used in place of the Alt key. The magic SysRq can also be accessed from the serial console.

Image

Common usage

Command line access and configuration

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:
Code: Select all
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.

Remote access

The linux daemon sysrqd provides a method of accessing SysRq features over TCP/IP port 4094 after authenticating with a plain-text password.

In hypervisors

The Xen hypervisor has functionality to send magic commands to hosted domains via its "xm sysrq" command.

"Raising Elephants" mnemonic device

A common idiom to perform a safe reboot of a Linux computer which has otherwise locked up, the QWERTY (or AZERTY) mnemonic "Raising Elephants Is So Utterly Boring", "Reboot Even If System Utterly Broken" or simply remembering the word "BUSIER" backwards, is often useful. It stands for:

    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),
    reBoot.
This can prevent a fsck being required on reboot and gives some programs a chance to save emergency backups of unsaved work.
In practice, each command may require a few seconds to complete, especially if feedback is unavailable from the screen due to a freeze or display corruption. For example, sending SIGKILL to processes which have not yet finished terminating can cause data loss.

Disabling SysRq key

The SysRq key can be disabled with the following command:
Code: Select all
echo 0 > /proc/sys/kernel/sysrq

To re-enable:
Code: Select all
echo 1 > /proc/sys/kernel/sysrq

On newer kernels (exact version unknown), it is possible to have a more fine-grained control. On these machines, the number written to /proc/sys/kernel/sysrq can be zero, one, or a number greater than one which is a bitmap indicating which features to allow.

Possible values are:
    * 0 - disable sysrq
    * 1 - enable sysrq completely
    * >1 - bitmask of enabled sysrq functions:
    o 2 - control of console logging level
    o 4 - control of keyboard (SAK, unraw)
    o 8 - debugging dumps of processes etc.
    o 16 - sync command
    o 32 - remount read-only
    o 64 - signalling of processes (term, kill, oom-kill)
    o 128 - reboot/poweroff
    o 256 - nicing of all RT tasks

Taken from http://en.wikipedia.org/wiki/Magic_SysRq_key
http://www.mjmwired.net/kernel/Documentation/sysrq.txt
lik
Founder
Founder
 
Posts: 497
Joined: Wed Dec 15, 2010 3:21 am

Return to Linux specific

cron