Magic SysRq key

From LinuxReviews
Jump to navigationJump to search

The magic SysRq key is a key combination understood by the Linux kernel which may allow users 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.[1]

The SysRq key, sometimes only labelled "Print Screen"

Pressing Alt+SysRq and another key which specifies a command can be used to forcibly unmount file systems, kill processes, recover keyboard state, and write unwritten data to disk. It can be used as a last resort when problems arise.

The magic SysRq key will not work under certain conditions, such as a kernel panic or a hardware failure preventing the kernel from running properly.

Requirements

Kernel support for the Magic SysRq key can be enabled for disabled with the kernels CONFIG_MAGIC_SYSRQ configuration option. That compile-time options is enabled in the vast majority of GNU/Linux distributions. Its features can be enabled or disabled compile-time with the kernels CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE option and run-time with the sysctl kernel.sysrq kernel option (/proc/sys/kernel/sysrq).

Value Function
0 0x0 Disable SysRq completely
1 0x1 Enable all SysRq functions

Values above 1 can be used to allow a limited amount of functions using bitmasks.

Value Function
2 0x2 enable control of console logging level
4 0x4 enable control of keyboard (SAK, unraw)
8 0x8 enable debugging dumps of processes etc.
16 0x10 enable sync command
32 0x20 enable remount read-only
64 0x40 enable signalling of processes (term, kill, oom-kill)
128 0x80 allow reboot/poweroff
256 0x100 allow nicing of all RT tasks

Both decimal and hexadecimal numbers (with the 0x prefix) can be used - except when configuring CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE which requires a hexadecimal value.

echo 272 > /proc/sys/kernel/sysrq (256+16) allows renicing of RT tasks (256) and the sync command (16).

Enabling SysRq on common GNU/Linux distributions

You can change the SysRq permission value run-time with either sysctl kernel.sysrq = NUMBER or echo "NUMBER" >/proc/sys/kernel/sysrq and it can be set permanently by creating a file called /etc/sysctl.d/99-magic-sysrq.conf[2] with kernel.sysrq=NUMBER. Your NUMBER should correspond to a bitmask which reflects the features you would like to allow. Use 1 if you want all the option enabled.

Fedora defaults to a bitmask of 16 which means that you can not reboot or poweroff or do much else with the default SysRQ configuration. Run sysctl kernel.sysrq to see your current setting.

Allowing the SysRq to be used for everything possible permanently is a easy matter of writing a new configur parameter to /etc/sysctl.d/:

echo "kernel.sysrq = 1" > /etc/sysctl.d/99-magic-sysrq.conf

Run sysctl --system to apply and optionally sysctl kernel.sysrq or sysctl -a (lists everything) to verify the new setting(s).

Kemonomimi rabbit.svg
Note: The kernel.sysrq only applies to keyboard combinations. It is also possible to invoke SysRq by echoing a command to /proc/sysrq-trigger and the root user can send any command regardless of the kernel.sysrq restrictions.

Commands

The key combination consists of Alt+SysRq (on x86) and another key, which controls the command issued. SysRq may be released before pressing the command key, as long as Alt remains held down.

Most modern keyboards do not have a separate SysRq key. The Print Screen (can be labelled PrtScr) key will function the same as the SysRq key if no dedicated SysRq key is present.

Kemonomimi rabbit.svg
Note: The SysRq combinations do not care about keyboard layouts and assume the QWERTY keyboard layout is used. If you, for example, have a Dvorak Simplified Keyboard, the combination to shut the system down uses the R key instead of O.

On some devices, notably laptops, the Fn key may need to be pressed to use the magic SysRq key.

The magic SysRq key may also be accessible from the serial console (by sending an RS-232 break signal).

From user space programs (such as a command line shell), SysRq may be accessed by writing to /proc/sysrq-trigger (e.g., echo s > /proc/sysrq-trigger).[1]

Action QWERTY Dvorak AZERTY Colemak
Set the console log level, which controls the types of kernel messages that are output to the console 0 - 9 0 - 9 0 - 9
(without ⇧ Shift)
0 - 9
Immediately reboot the system, without unmounting or syncing filesystems b x b b
Perform a system crash. A crashdump will be taken if it is configured. c j c c
Display all currently held Lock]s (CONFIG_LOCKDEP kernel option is required) d e d s
Send the SIGTERM signal to all processes except init (PID 1) e . e f
Call oom_kill, which kills a process to alleviate an Out of memory condition f u f t
When using Kernel Mode Setting, switch to the kernel's framebuffer console.
If the in-kernel debugger kdb is present, enter the debugger.
g i g d
Output a terse help document to the console
Any key which is not bound to a command should also perform this action
h d h h
Send the SIGKILL signal to all processes except init i c i u
Forcibly "Just thaw it" – filesystems frozen by the FIFREEZE ioctl. j h j n
Kill all processes on the current virtual console (can kill X and SVGALib programs) k t k e
Shows a stack backtrace for all active CPUs. l n l i
Output current memory information to the console m m , m
Reset the nice level of all high-priority and real-time tasks n b n k
Shut off the system o r o y
Output the current registers and flags to the console p l p ;
Display all active high-resolution timers and clock sources. q ' a q
Switch the keyboard from raw mode, used by programs such as Wayland, X11 and SVGALib, to XLATE mode r p r p
Sync all mounted filesystems s o s r
Output a list of current tasks and their information to the console t y t g
Remount all mounted filesystems in read-only mode u g u l
Forcefully restores framebuffer console.
For ARM processors, cause ETM buffer dump instead.
v k v v
Display list of blocked (D state) tasks w , z w
Used by xmon interface on PowerPC platforms. x q x x
Show global CPU registers (SPARC-64 specific) y f y j
Dump the ftrace buffer z ; w z
Print a summary of valid, but not necessarily enabled, magic SysRq keys space space space space

Common Uses

The SysRq keyboard combination REISUB, which can be remembered by "Reboot Even If System Utterly Broken" or "Raising Elephants Is So Utterly Boring", is a useful sequence if a machine needs to be restarted due to a lock-up or something else which has gone horribly wrong. It does the following:

  • 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 except init, forcing them to terminate immediately),
  • Sync (flush data to disk),
  • Unmount (remount all filesystems read-only),
  • reBoot.

A graphical program which is killed using SysRq can't and won't restore the terminal to a readable state. The command reset (initializes terminals) can be used to fix a terminal with us unreadable.

k, which kills everything in the current virtual console, can be used to kill X and get debugging information from certain kinds of GPU driver crashes.

Invoking SysRq on non-x86 systems

Chromebooks have a keyboard but no dedicated SysRq key. They use Alt+VolumeUp (Alt+F10) instead[3][4][5], however some keys have a different function.[6][7]

IBM Power Systems servers can invoke the Magic SysRq feature using Ctrl+O 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 IBM 3270 or HMC console.

Other ways to invoke Magic SysRq

The SysRq functionality can be used from the command-line as root by sending commands directly to /proc/sysrq-trigger (e.g., echo s > /proc/sysrq-trigger).[1]. This feature can be used on remote systems and headless systems - if it's possible to ssh in (which is frequently not the case if there is a serious problem).

The daemons sysrqd and tcpconsole can be used to invoke SysRq features remotely.

The hangwatch daemon can be used to trigger SysRq commands if a machines load average is above a set threshold.

SysRq can be used to send commands through serial consoles by sending a magic break followed by a key command.

References

  1. 1.0 1.1 1.2 Linux Magic System Request Key Hacks. kernel.org (2019-12-30). Retrieved on 2019-12-30.
  2. Fedora has kernel.sysrq = 16 defined in /usr/lib/sysctl.d/50-default.conf. The sysctl files are applied alphabetically regardless of path. Any file overriding the default value has to start with a number above 50.
  3. Debug Button Shortcuts. Retrieved on 2019-12-30.
  4. Change 29110: CHROMIUM: sysrq: treat F10 as magic sysrq key. Retrieved on 2016-07-11.
  5. Chromium OS dev: SysRq in ChromiumOS' kernel. Retrieved on 2016-07-11.
  6. Helping debug system hangs. Retrieved on 2018-09-25.
  7. CHROMIUM: sysrq: add ability for sysrq-x to signal chrome/X. Retrieved on 2018-04-12.

Links

Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.