Magic SysRq key
{{Short description|Keyboard commands for recovering a Linux machine}}
{{Redirect|Magic key|the TV series|The Magic Key}}
{{how-to|date=June 2012}}
File:KeyboardWithPrintScreenRinged.svg
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.{{cite web
| url=https://www.kernel.org/doc/html/latest/admin-guide/sysrq.html
| title=Linux Magic System Request Key Hacks
| date=2013-08-12
| publisher=kernel.org
| accessdate=2017-05-21
}}
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{{Citation | url = http://www.unixmen.com/use-magic-sysrq-combination-key-to-recover-from-a-frozen-system-in-linux-ubuntu/ | last = Khamlichi | first = Mel | title = Use Magic SysRq Combination Key to Recover from a Frozen System in Linux | publisher = Unixmen | date = 18 Oct 2010 | accessdate = 21 Sep 2014 | archive-url = https://web.archive.org/web/20140820013821/http://www.unixmen.com/use-magic-sysrq-combination-key-to-recover-from-a-frozen-system-in-linux-ubuntu/ | archive-date = 20 August 2014 | url-status = dead }}. or a hardware failure preventing the kernel from running properly.
Implementation
The Linux kernel contains sysrq.c
,{{Cite web |title=linux/drivers/tty/sysrq.c at master · torvalds/linux |url=https://github.com/torvalds/linux/blob/master/drivers/tty/sysrq.c |access-date=2025-03-27 |website=GitHub |language=en}} which implements magic SysRq functionality. Magic SysRq functionality is called by serial devices{{Cite web |title=linux/drivers/usb/serial/generic.c at 6537cfb395f352782918d8ee7b7f10ba2cc3cbf2 · torvalds/linux |url=https://github.com/torvalds/linux/blob/6537cfb395f352782918d8ee7b7f10ba2cc3cbf2/drivers/usb/serial/generic.c#L574 |access-date=2025-03-27 |website=GitHub |language=en}}{{Cite web |title=linux/include/linux/serial_core.h at 6537cfb395f352782918d8ee7b7f10ba2cc3cbf2 · torvalds/linux |url=https://github.com/torvalds/linux/blob/6537cfb395f352782918d8ee7b7f10ba2cc3cbf2/include/linux/serial_core.h#L1170 |access-date=2025-03-27 |website=GitHub |language=en}} at a low level in the driver code.
Commands
The key combination consists of {{key|Alt|SysRq}} and another key, which controls the command issued. {{key|SysRq}} may be released before pressing the command key, as long as {{key|Alt}} remains held down.
The combinations always assume the QWERTY keyboard layout{{Citation needed|reason=It doesn't looks to apply always|date=September 2021}}. For example, on the Dvorak keyboard layout, the combination to shut down the system uses the {{key|R}} key instead of {{key|O}}. Furthermore, some keyboards may not provide a separate {{key|SysRq}} key. In this case, a separate {{key|PrtScn}} key should be present{{Citation needed|reason=What does "should have" entail here?|date=March 2025}}.
On some devices, notably laptops, the {{key|Fn}} key may need to be pressed to use the magic {{key|SysRq}} key, e.g. on Thinkpad Carbon X1 the {{key|SysRq}} is activated by pressing {{key|Alt|Fn|S}} simultaneously, then releasing {{key|Fn}} and {{key|S}} while still holding {{key|Alt}}.{{cite web |title=Alt+SysRq on a laptop |url=https://superuser.com/a/1237766 |website=Super User |access-date=25 July 2023 |language=en}}
SysRq functionalities can also be accessed without a keyboard. See {{section link||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
.[https://www.kernel.org/doc/html/latest/admin-guide/sysrq.html Linux Magic System Request Key Hacks — The Linux Kernel documentation]
class="wikitable" |
Action
! QWERTY ! Dvorak ! AZERTY ! Colemak |
---|
Set the console log level, which controls the types of kernel messages that are output to the console
| {{key|0}} - {{key|9}} | {{key|0}} - {{key|9}} | {{key|0}} - {{key|9}} | {{key|0}} - {{key|9}} |
Immediately reboot the system, without unmounting or syncing filesystems
| {{key|b}} | {{key|x}} | {{key|b}} | {{key|b}} |
Perform a system crash. A crashdump will be taken if it is configured.
| {{key|c}} | {{key|j}} | {{key|c}} | {{key|c}} |
Display all currently held Locks ({{tt|CONFIG_LOCKDEP}} kernel option is required)
| {{key|d}} | {{key|e}} | {{key|d}} | {{key|s}} |
Send the {{tt|SIGTERM}} signal to all processes except {{Tt|init}} (PID 1)
| {{key|e}} | {{key|.}} | {{key|e}} | {{key|f}} |
Call {{tt|oom_kill}}, which kills a process to alleviate an OOM condition
| {{key|f}} | {{key|u}} | {{key|f}} | {{key|t}} |
When using Kernel Mode Setting, switch to the kernel's framebuffer console.{{cite web | publisher = Kernel | work = Git | type = commit | url = https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=79e539453b34e35f39299a899d263b0a1f1670bd | title = DRM: i915: add mode setting support | first=Jesse | last=Barnes | date = 2008-11-07 | accessdate = 2013-08-31}} If the in-kernel debugger {{tt|kdb}} is present, enter the debugger. | {{key|g}} | {{key|i}} | {{key|g}} | {{key|d}} |
Output a terse help document to the console Any key which is not bound to a command should also perform this action | {{key|h}} | {{key|d}} | {{key|h}} | {{key|h}} |
Send the {{tt|SIGKILL}} signal to all processes except {{tt|init}} (PID 1)
| {{key|i}} | {{key|c}} | {{key|i}} | {{key|u}} |
Forcibly "just thaw it" – filesystems frozen by the {{tt|FIFREEZE}} ioctl.
| {{key|j}} | {{key|h}} | {{key|j}} | {{key|n}} |
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 | {{key|k}} | {{key|t}} | {{key|k}} | {{key|e}} |
Shows a stack backtrace for all active CPUs.
| {{key|l}} | {{key|n}} | {{key|l}} | {{key|i}} |
Output current memory information to the console
| {{key|m}} | {{key|m}} | {{key|,}} | {{key|m}} |
Reset the nice level of all high-priority and real-time tasks
| {{key|n}} | {{key|b}} | {{key|n}} | {{key|k}} |
Shut off the system
| {{key|o}} | {{key|r}} | {{key|o}} | {{key|y}} |
Output the current registers and flags to the console
| {{key|p}} | {{key|l}} | {{key|p}} | {{key|;}} |
Display all active high-resolution timers and clock sources.
| {{key|q}} | {{key|'}} | {{key|a}} | {{key|q}} |
Switch the keyboard from raw mode, used by programs such as X11 and SVGAlib, to XLATE mode
| {{key|r}} | {{key|p}} | {{key|r}} | {{key|p}} |
Sync all mounted filesystems
| {{key|s}} | {{key|o}} | {{key|s}} | {{key|r}} |
Output a list of current tasks and their information to the console
| {{key|t}} | {{key|y}} | {{key|t}} | {{key|g}} |
Remount all mounted filesystems in read-only mode
| {{key|u}} | {{key|g}} | {{key|u}} | {{key|l}} |
Forcefully restores framebuffer console. For ARM processors, cause ETM buffer dump instead. | {{key|v}} | {{key|k}} | {{key|v}} | {{key|v}} |
Display list of blocked (D state) tasks
| {{key|w}} | {{key|,}} | {{key|z}} | {{key|w}} |
Used by xmon interface on PowerPC platforms. Show global PMU Registers on sparc64. Dump all TLB entries on MIPS.{{cite web|url=https://www.kernel.org/doc/Documentation/admin-guide/sysrq.rst|title=Linux Magic System Request Key Hacks|date=2021-09-13|access-date=2022-09-25}} | {{key|x}} | {{key|q}} | {{key|x}} | {{key|x}} |
Show global CPU registers (SPARC-64 specific)
| {{key|y}} | {{key|f}} | {{key|y}} | {{key|j}} |
Dump the ftrace buffer
| {{key|z}} | {{key|;}} | {{key|w}} | {{key|z}} |
Debug dump of BPF scheduler
| {{key|D}} | {{dunno}} | {{dunno}} | {{dunno}} |
Replay the kernel log messages on consoles
| {{key|R}} | {{dunno}} | {{dunno}} | {{dunno}} |
Disables the BPF scheduler and revert to CFS
| {{key|S}} | {{dunno}} | {{dunno}} | {{dunno}} |
Example output of the {{key|SysRq|h}} 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) dump-sched-ext(D) replay-kernel-logs(R) reset-sched-ext(S)
Configuration
The feature is controlled both by a compile-time option in the kernel configuration, {{tt|CONFIG_MAGIC_SYSRQ}}, and a sysctl kernel parameter, {{tt|kernel.sysrq}}.
On newer kernels (since 2.6.12{{cite web
| url=http://kernelnewbies.org/Linux_2_6_12
| title=Linux 2.6.12
| date=2012-07-17
| first=Diego
| last=Calleja
| publisher=Linux Kernel Newbies
| accessdate=2013-08-31}}
), it is possible to have more fine-grained control over how the magic SysRq key can be used.{{cite web
| url=https://fedoraproject.org/wiki/QA/Sysrq
| title=QA/Sysrq
| date=2013-05-09
| publisher=Fedora Project
| accessdate=2015-04-09
}} On these machines, the number written to {{tt|/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 {{tt|/etc/sysctl.d/10-magic-sysrq.conf}} .
Uses
Before the advent of journaled filesystems a common use of the magic SysRq key was to perform a safe reboot of a locked-up Linux computer (using the sequence of key presses indicated by the mnemonic REISUB), which lessened the risk of filesystem corruption. With modern filesystems, syncing and unmounting is still useful to force unflushed data to disk, but is no longer necessary to prevent filesystem corruption (and may increase the risk of corruption in case the lock-up is caused by the kernel being in a bad state).{{cite web | publisher = Kernel | work = Git | type = commit | url = https://lore.kernel.org/lkml/20190909183817.GB12602@angband.pl/T/#m11316a7c03c12e46d140fae9c670fa736f3d8ccf | title = Documentation: sysrq: don't recommend 'S' 'U' before 'B'| date = 2019-09-06}} The default value of kernel.sysrq in distributions such as Ubuntu and Debian remains 176{{citation needed|date=August 2024}} (allowing the sync, unmount, and reboot functions) and 438{{cite web|url = https://www.debian.org/doc/manuals/securing-debian-manual/restrict-sysrq.html |title=4.9. Restricting the use of the Magic SysRq key |work=Securing Debian Manual |access-date=August 20, 2024}} (allowing the same functions plus loglevel, unraw, and nice-all-RT-tasks) respectively.
Another past use was to kill a frozen graphical program, as the X Window System used to have complete control over the graphical mode and input devices.
On distributions that do not include a {{tt|textmode}} command,{{clarify|reason=what exactly does this refer to?|date=August 2024}} the key sequence {{key|Ctrl|Alt|F1}} may be able to force a switch to a text console. Some systems may require use of {{key|F2}} or {{key|F3}} instead of {{key|F1}}.
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 {{tt|/proc/sysrq-trigger}} (e.g., {{code|echo s > /proc/sysrq-trigger}}).
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.{{cite web | url = http://www.tldp.org/HOWTO/Remote-Serial-Console-HOWTO/security-sysrq.html | title = Magic SysRq key | work = Remote Serial Console How-To | first1 = Glen | last1 = Turner | first2 = Mark F | last2 = Komarinski | publisher = The Linux Documentation Project | date = 2003-03-31 | accessdate = 2015-05-08}}
The Linux daemons {{tt|sysrqd}}{{cite web | type = project | url= https://github.com/jd/sysrqd | title= sysrqd | first= Julien | last = Danjou | website= GitHub | accessdate= 2016-09-13}} and {{tt|tcpconsole}}{{cite web | url = http://www.vanheusden.com/tcpconsole/ | title = tcpconsole | first = Folkert | last = van Heusden | accessdate = 2013-08-31 | archive-date = 2017-08-31 | archive-url = https://web.archive.org/web/20170831173944/https://www.vanheusden.com/tcpconsole/ | url-status = dead }} provide a method of accessing SysRq features over a TCP connection after authenticating with a plain-text password. The {{tt|hangwatch}}{{cite web | url= https://github.com/jumanjiman/hangwatch | title= hangwatch | first= Paul | last= Morgan | publisher = GitHub | accessdate = 2013-08-31}} 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 {{tt|xm sysrq}} command.{{Citation | chapter-url = https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Virtualization/chap-Virtualization-The_xm_command_quick_reference.html | chapter = 27. The xm command quick reference | title = Enterprise Linux 5 Virtualization Guide | publisher = RedHat | accessdate= 2013-08-31}} Additionally, a SysRq command can be invoked from a Xen paravirtual console by sending a break sequence {{key|Ctrl|O}} followed by the desired key.
The Kernel-based_Virtual_Machine (KVM) hypervisor has functionality to send magic commands to hosted domains via its {{tt|virsh send-key}} command. https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/7/html/virtualization_deployment_and_administration_guide/sect-editing_a_guest_virtual_machines_configuration_file-sending_keystoke_combinations_to_a_specified_domain
eg:
virsh send-key --domain domainname --keycode KEY_LEFTALT KEY_SYSRQ KEY_S
Chromebooks have a keyboard but no dedicated SysRq key. They use {{key|Alt|VolumeUp}} ({{key|Alt|F10}}) instead,{{cite web | url = https://chromium.googlesource.com/chromiumos/docs/+/master/debug_buttons.md | title = Debug Button Shortcuts |website = Chromium OS docs | accessdate = 2018-10-22 }}{{cite web | url = https://chromium-review.googlesource.com/#/c/29110/2/drivers/tty/sysrq.c | title = Change 29110: CHROMIUM: sysrq: treat F10 as magic sysrq key | accessdate = 2016-07-11 }}{{cite web | url = https://groups.google.com/a/chromium.org/d/msg/chromium-os-dev/HnW8maLcDas/NJU1HFmE76kJ | title = Chromium OS dev: SysRq in ChromiumOS' kernel | accessdate = 2016-07-11}} however some keys have a different function.{{cite web | url = https://sites.google.com/a/chromium.org/dev/chromium-os/how-tos-and-troubleshooting/debugging-hangs | title = Helping debug system hangs | website = The Chromium Projects | accessdate = 2018-09-25}}{{cite web | url = https://chromium-review.googlesource.com/c/chromiumos/third_party/kernel/+/39527 | title = CHROMIUM: sysrq: add ability for sysrq-x to signal chrome/X | accessdate = 2018-04-12}}
IBM Power Systems servers can invoke the Magic SysRq feature using {{key|Ctrl|O}} followed by the desired key from the Hardware Management Console.
IBM mainframe partitions can invoke the Magic SysRq feature using {{key|^|-}} followed by the desired key on 3270 or HMC.
See also
- Stop-A, key sequence used to access Sun Microsystems's Open Firmware (OpenBoot)
- Console server
- KVM switch
- System console
References
{{reflist|30em}}
External links
{{Wikibooks|Linux Guide|Freezes}}
{{Commons category}}
- {{cite web | url=https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/admin-guide/sysrq.rst | title=Documentation/sysrq.txt | work = Linux | type = kernel source tree | publisher= kernel.org | accessdate= 2017-01-31}}
- {{cite web | url=https://www.kernel.org/doc/html/latest/admin-guide/sysrq.html | title=Linux Magic System Request Key Hacks | work = Linux | type = linux kernel documentation | publisher= kernel.org | accessdate= 2020-06-01}}
- {{cite web | url=https://www.kernel.org/pub/linux/kernel/people/akpm/patches/2.5/2.5.64/2.5.64-mm7/broken-out/proc-sysrq-trigger.patch | title= Kernel patch to the -mm tree that added the /proc/sysrq-trigger feature: This makes sysrq facilities available to remote users | author-link = Andrew Morton (computer programmer) | first = Andrew | last = Morton | date=2013-03-13 | accessdate= 2015-04-09}}
- {{cite web | work = AP Lawrence | url=http://aplawrence.com/Words2005/2005_04_13.html | title= Magic Sysrq Key on Linux | first= Tony | last= Lawrence | date= 2005-04-13 | accessdate= 2015-04-09}}
- {{cite web | url=http://www.ibm.com/developerworks/linux/library/l-magic.html | title= Magic sys request | publisher= IBM | work = Developer works | first= Tom | last= Gall | date= 2000-04-01 | archiveurl= https://web.archive.org/web/20070527215139/http://www.ibm.com/developerworks/linux/library/l-magic.html | archivedate= 2007-05-27 | url-status= dead | accessdate= 2013-09-03}}
Category:Linux kernel features