Magic SysRq key

From HandWiki
Revision as of 22:35, 6 February 2024 by JMinHep (talk | contribs) (simplify)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
The 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 filesystem.[1] Its effect is similar to the computer's hardware reset button (or power switch) but with many more options and much more control.

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[2] or a hardware failure preventing the kernel from running properly.

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;[citation needed] 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 .[3] On a ChromeOS device, is activated by pressing .

SysRq functionalities can also be accessed without a keyboard. See § Other ways to invoke Magic SysRq below. Note that some commands may be disabled out of the box as specified in the bitmask value in /proc/sys/kernel/sysrq.[4]

Action QWERTY Dvorak AZERTY Colemak
Set the console log level, which controls the types of kernel messages that are output to the console - - -
(without )
-
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 (CONFIG_LOCKDEP kernel option is required)
Send the SIGTERM signal to all processes except init (PID 1)
Call oom_kill, which kills a process to alleviate an OOM condition
When using Kernel Mode Setting, switch to the kernel's framebuffer console.[5]
If the in-kernel debugger kdb 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 SIGKILL signal to all processes except init
Forcibly "just thaw it" – filesystems frozen by the FIFREEZE ioctl.
Kill all processes on the current virtual console (can kill X and SVGAlib programs, see below)
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 filesystems
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 (D state) tasks
Used by xmon interface on PowerPC platforms.
Show global PMU Registers on sparc64.
Dump all TLB entries on MIPS.[6]
Show global CPU registers (SPARC-64 specific)
Dump the ftrace buffer

Example output of the command:

sysrq: HELP : loglevel(0-9) reboot(b) crash(c) terminate-all-tasks(e) memory-full-oom-kill(f) kill-all-tasks(i) thaw-filesystems(j) sak(k) show-backtrace-all-active-cpus(l) show-memory-usage(m) nice-all-RT-tasks(n) poweroff(o) show-registers(p) show-all-timers(q) unraw(r) sync(s) show-task-states(t) unmount(u) force-fb(v) show-blocked-tasks(w) dump-ftrace-buffer(z)

Uses

Before the advent of journaled filesystems a common use of the magic SysRq key was to perform a safe reboot of a Linux computer which has otherwise locked up (abbr. REISUB), which avoided a risk of filesystem corruption. With modern filesystems, this practice is not encouraged, offering no upsides over straight reboot,[7] though the default value of kernel.sysrq in such distributions as Ubuntu and Debian remains 176 and 438 [8] respectively.

Another past use was to kill a frozen graphical program, as the X Window System used to have complete control over graphical mode and input devices.

On distributions that do not include a textmode command, the key command may sometimes be able to force a return to a text console. (Use , , , ... , , where n is the highest number of text consoles set up by the distribution. would normally be used to reenter GUI mode on a system on which the X server has not crashed.) There are also distributions where + (, ... ,) opens text consoles, reenter GUI mode, reenter GUI mode on the login page, in order to log in another user.

Configuration

The feature is controlled both by a compile-time option in the kernel configuration, CONFIG_MAGIC_SYSRQ, and a sysctl kernel parameter, kernel.sysrq.

On newer kernels (since 2.6.12[9]), it is possible to have more fine-grained control over how the magic SysRq key can be used.[10] On these machines, the number written to /proc/sys/kernel/sysrq 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 /etc/sysctl.d/10-magic-sysrq.conf .

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 (such as a command line shell), SysRq may be accessed by writing to /proc/sysrq-trigger (e.g., echo s > /proc/sysrq-trigger).[1]

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.[11]

The Linux daemons sysrqd[12] and tcpconsole[13] provide a method of accessing SysRq features over a TCP connection after authenticating with a plain-text password. The hangwatch[14] 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 xm sysrq command.[15] Additionally, a SysRq command can be invoked from a Xen paravirtual console by sending a break sequence followed by the desired key.

Chromebooks have a keyboard but no dedicated SysRq key. They use () instead,[16][17][18] however some keys have a different function.[19][20]

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 console.

See also

References

  1. 1.0 1.1 "Linux Magic System Request Key Hacks". kernel.org. 2013-08-12. https://www.kernel.org/doc/html/latest/admin-guide/sysrq.html. Retrieved 2017-05-21. 
  2. Khamlichi, Mel (18 Oct 2010), Use Magic SysRq Combination Key to Recover from a Frozen System in Linux, Unixmen, http://www.unixmen.com/use-magic-sysrq-combination-key-to-recover-from-a-frozen-system-in-linux-ubuntu/, retrieved 21 Sep 2014 .
  3. "Alt+SysRq on a laptop" (in en). https://superuser.com/a/1237766. 
  4. Linux Magic System Request Key Hacks — The Linux Kernel documentation
  5. Barnes, Jesse (2008-11-07). "DRM: i915: add mode setting support". Git. Kernel. https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=79e539453b34e35f39299a899d263b0a1f1670bd. Retrieved 2013-08-31. 
  6. "Linux Magic System Request Key Hacks". 2021-09-13. https://www.kernel.org/doc/Documentation/admin-guide/sysrq.rst. 
  7. "Documentation: sysrq: don't recommend 'S' 'U' before 'B'". Git. Kernel. 2019-09-06. https://lore.kernel.org/lkml/20190909183817.GB12602@angband.pl/T/#m11316a7c03c12e46d140fae9c670fa736f3d8ccf. 
  8. url = https://www.debian.org/doc/manuals/securing-debian-manual/restrict-sysrq.html
  9. Calleja, Diego (2012-07-17). "Linux 2.6.12". Linux Kernel Newbies. http://kernelnewbies.org/Linux_2_6_12. Retrieved 2013-08-31. 
  10. "QA/Sysrq". Fedora Project. 2013-05-09. https://fedoraproject.org/wiki/QA/Sysrq. Retrieved 2015-04-09. 
  11. Turner, Glen; Komarinski, Mark F (2003-03-31). "Magic SysRq key". Remote Serial Console How-To. The Linux Documentation Project. http://www.tldp.org/HOWTO/Remote-Serial-Console-HOWTO/security-sysrq.html. Retrieved 2015-05-08. 
  12. Danjou, Julien. "sysrqd". https://github.com/jd/sysrqd. Retrieved 2016-09-13. 
  13. van Heusden, Folkert. "tcpconsole". http://www.vanheusden.com/tcpconsole/. Retrieved 2013-08-31. 
  14. Morgan, Paul. "hangwatch". GitHub. https://github.com/jumanjiman/hangwatch. Retrieved 2013-08-31. 
  15. "27. The xm command quick reference", Enterprise Linux 5 Virtualization Guide, RedHat, https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Virtualization/chap-Virtualization-The_xm_command_quick_reference.html, retrieved 2013-08-31 
  16. "Debug Button Shortcuts". https://chromium.googlesource.com/chromiumos/docs/+/master/debug_buttons.md. Retrieved 2018-10-22. 
  17. "Change 29110: CHROMIUM: sysrq: treat F10 as magic sysrq key". https://chromium-review.googlesource.com/#/c/29110/2/drivers/tty/sysrq.c. Retrieved 2016-07-11. 
  18. "Chromium OS dev: SysRq in ChromiumOS' kernel". https://groups.google.com/a/chromium.org/d/msg/chromium-os-dev/HnW8maLcDas/NJU1HFmE76kJ. Retrieved 2016-07-11. 
  19. "Helping debug system hangs". https://sites.google.com/a/chromium.org/dev/chromium-os/how-tos-and-troubleshooting/debugging-hangs. Retrieved 2018-09-25. 
  20. "CHROMIUM: sysrq: add ability for sysrq-x to signal chrome/X". https://chromium-review.googlesource.com/c/chromiumos/third_party/kernel/+/39527. Retrieved 2018-04-12. 

External links