Notes about magic SysRq key.

1. What is magic SysRq key?

It is a ‘magical’ key combo you can hit which the kernel will respond to regardless of whatever else it is doing, unless it is completely locked up.

Magic SysRq组合键是一串能直接与Linux 内核沟通的组合键,允许用户就算在系统进入死循环濒临崩溃时,直接调用系统底层将资料写入文件系统或重启,避免尚未写入文件系统与硬盘的数据在关机后消失。

此组合键提供一系列在系统崩溃时常用到的功能,比如上述的写入数据,或关闭 X Server 、Kill 进程、卸载 文件系统,也通常是死机时的最后手段。

2. How do I enable the magic SysRq key?

You need to say “yes” to ‘Magic SysRq key (CONFIG_MAGIC_SYSRQ)’ when configuring the kernel. When running a kernel with SysRq compiled in, /proc/sys/kernel/sysrq controls the functions allowed to be invoked via the SysRq key. The default value in this file is set by the CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE config symbol, which itself defaults to 1. Here is the list of possible values in /proc/sys/kernel/sysrq:

  • 0 - disable sysrq completely

  • 1 - enable all functions of sysrq

  • >1 - bitmask of allowed sysrq functions (see below for detailed function description):

    1
    2
    3
    4
    5
    6
    7
    8
      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

Note that the value of /proc/sys/kernel/sysrq influences only the invocation via a keyboard. Invocation of any operation via /proc/sysrq-trigger is always allowed (by a user with admin privileges).

3. How do I use the magic SysRq key?

  • On x86
    You press the key combo ALT-SysRq-<command key>.

  • On all
    Write a character to /proc/sysrq-trigger. e.g.:

    1
    echo t > /proc/sysrq-trigger

4. What are the ‘command’ keys?

kernel.org doc

5. Usage example

5.1 perform a system crash and a crashdump will be taken if configured.

1
2
echo 1 > /proc/sys/kernel/sysrq
echo c > /proc/sysrq-trigger

参考资料:

  1. Linux Magic System Request Key Hacks
  2. wikipedia Magic SysRq组合键