Linux: the System Request Key

Everyday with Linux there seems to be something new to learn or a little tidbit that is worth remembering.  This time, the sysrq. Activates the System Request Key (aka. the Magic SysRq key), if thi value is set to anything other than zero (0), the default.

sysrq

The System Request Key allows immediate input to the kernel through simple key combinations. For example, the System Request Key can be used to immediately shut down or restart a system, sync all mounted file systems, or dump important information to the console. To initiate a System Request Key, type Alt-SysRq-<system request code> . Replace <system request code> with one of the following system request codes:

  • r — Disables raw mode for the keyboard and sets it to XLATE (a limited keyboard mode which does not recognize modifiers such as Alt, Ctrl, or Shift for all keys).
  • k — Kills all processes active in a virtual console. Also called Secure Access Key (SAK), it is often used to verify that the login prompt is spawned from init and not a trojan copy designed to capture usernames and passwords.
  • b — Reboots the kernel without first unmounting file systems or syncing disks attached to the system.
  • c — Crashes the system without first unmounting file systems or syncing disks attached to the system.
  • o — Shuts off the system.
  • s — Attempts to sync disks attached to the system.
  • u — Attempts to unmount and remount all file systems as read-only.
  • p — Outputs all flags and registers to the console.
  • t — Outputs a list of processes to the console.
  • m — Outputs memory statistics to the console.
  • 0 through 9 — Sets the log level for the console.
  • e — Kills all processes except init using SIGTERM.
  • i — Kills all processes except init using SIGKILL.
  • l — Kills all processes using SIGKILL (including init). The system is unusable after issuing this System Request Key code.
  • h — Displays help text.

The following example enables the Magic SysRq Key.

echo 1 > /proc/sys/kernel/sysrq

Now that the key is enabled, the following key combination on the QWERTY keyboard will reboot the system. Hit Alt-SysRq-b.  If your keyboard does not have the SysRq key, the PrtSc or PrintScreen key will work. Upon reboot the value will reset itself to a default other than 1.

sysrq-trigger

Alternately, the following command may be executed to immediately reboot the system. Be warned that this may trigger fsck or a dead box.

echo b > /proc/sysrq-trigger
mnemonics

A common use of the magic SysRq key is to perform a safe reboot of a Linux computer which has otherwise locked up. This can prevent a fsck being required on reboot and gives some programs a chance to save emergency backups of unsaved work. The QWERTY (or AZERTY) mnemonics: "Raising Elephants Is So Utterly Boring", "Reboot Even If System Utterly Broken" or simply the word "BUSIER" read backwards, are often used to remember the following SysRq-keys sequence:

To begin, enable the sysrq.

echo 1 > /proc/sys/kernel/sysrq

Then Alt-PrtSc-REISUB

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.

Source(s)
https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s3-proc-sys-kernel.html
https://en.wikipedia.org/wiki/Magic_SysRq_key#.22Raising_Elephants.22_mnemonic_device
https://www.kernel.org/doc/Documentation/sysrq.txt