Use of SYSRQ in Linux (Magic key)

Source: Internet
Author: User
Tags dmesg

Magic key: Linux Magic System Request key Hacks


When a Linux system does not respond properly to user requests, you can use the SysRq gadget to control Linux.

One-SysRq enable and shutdown

To enable SYSRQ, you need to set the MAGIC sysrq key (CONFIG_MAGIC_SYSRQ) to Y when configuring the kernel. For kernels that support SYSRQ,/PROC/SYS/KERNEL/SYSRQ controls whether the SYSRQ is enabled or not. If the/PROC/SYS/KERNEL/SYSRQ content is 0, then SYSRQ is disabled; If the/PROC/SYS/KERNEL/SYSRQ content is 1, then SYSRQ is enabled. For more descriptions of/PROC/SYS/KERNEL/SYSRQ, please refer to <kernel source>/documentation/sysrq.txt

You can temporarily enable or disable SYSRQ by running the command echo "0" >www.qixoo.qixoo.com/proc/sys/kernel/sysrq and echo "1" >/PROC/SYS/KERNEL/SYSRQ. If you need to enable or disable Sysrqs permanently, you can set KERNEL.SYSRQ = 1 (enable SSYRQ) or KERNEL.SYSRQ = 0 in/etc/sysctl.conf (disable SYSRQ)

The use of two SYSRQ

The SYSRQ command key has

' R '-Turns off keyboard raw mode and sets it to XLATE.

' K '-Secure Access Key (SAK) kills all programs in the current virtual
Console. Note:see important comments below in SAK section.

' B '-would immediately reboot the system without syncing or unmounting
Your disks.

' O '-would shut your system off (if configured and supported).

' s '-would attempt to sync all mounted filesystems.

' U '-would attempt to remount all mounted filesystems read-only.

' P '-would dump the current registers and the flags to your console.

' t '-would dump a list of current tasks and their information to your
Console.

' m '-would dump current memory info to your console.

' 0 '-' 9 '-sets the console log level, controlling which kernel messages
'll is printed to your console. (' 0 ', for example would make
It so is emergency messages like panics or oopses would
Make it to your console.)

' E '-Send a SIGTERM to all processes, except for init.

' I '-Send a SIGKILL to all processes, except for init.

' L '-Send a SIGKILL to all processes, including init. (Your system
Would be non-functional after this.)

' H '-display Help for actually any other key than those listed
Above'll display Help. But ' h ' are easy to remember:-)


Press ALT at the same time on the terminal, SYSRQ and command key will execute SYSRQ command, SYSRQ key is "prent screen" jian. For example, Alt+sysrq+b restarts the machine.

If you connect to the server using Telnet or SSH, you can execute SYSRQ commands using the echo ' <SYSRQ Command key > ' > Qkxue.net/proc/sysrq-trigger, such as Echo ' B ' >/proc /sysrq-trigger Restart the system.

Three commonly used SYSRQ commands (sequence)

3.1 Restart Machine SYSRQ command sequence is K (SAK) s (sync) u (umount) b (reboot)

3.2 SYSRQ command to display memory information is M

3.3 The SYSRQ command that displays the current task information is T (Task)

Four reference materials

<kernel source>/documentation/sysrq.txt:sysrq.txt The most authoritative documentation, it is best to refer to the documentation that is included with the currently running Kernel. Or go to Http://lxr.linux.no/source/Documentation/sysrq.txt to see

Oracle Metalink Note 228203.1:alt sysrq Keys Utility on Linux

================================================================================================

Transferred from: http://blog.csdn.net/xyyaiguozhe/article/details/12916613

Classic Documents:

Linux Magic System Request Key Hacks
Documentation for SYSRQ.C


* What is the Magic sysrq key?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It's a ' magical ' key combo you can hit which the kernel would respond to
Regardless of whatever else it is doing, unless it's completely locked up.


* How does 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. By default the file contains 1 which means that every
Possible SYSRQ request is allowed (in older versions SYSRQ was disabled
By default, and your were required to specifically enable it at Run-time
But this isn't the case any more). 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):
2-enable control of console logging level
4-enable Control of keyboard (SAK, Unraw)
8-enable debugging dumps of processes etc.
16-enable Sync Command
32-enable remount read-only
64-enable signalling of processes (term, kill, Oom-kill)
128-allow Reboot/poweroff
256-allow nicing of all RT tasks
You can set the value of the file by the following command:
echo "Number" &GT;/PROC/SYS/KERNEL/SYSRQ
Note The value Of/proc/sys/kernel/sysrq influences only the invocation
via a keyboard. Invocation of any operation Via/proc/sysrq-trigger are always
Allowed (by a, user with admin privileges).


* How does I use the Magic sysrq key?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On X86-you Press the key combo ' Alt-sysrq-<command key> '. Note-some
Keyboards a key labeled ' SYSRQ '. The ' SysRq ' key is
Also known as the ' Print screen ' key. Also some keyboards cannot
Handle so many keys being pressed @ the same time, so you might
There are better luck with "press Alt", "Press SysRq", "Release SysRq",
"Press <command key>", release everything.
On Sparc-you Press ' Alt-stop-<command key> ', I believe.
On the serial console (PC style standard serial ports only)-
You send a to break and then within 5 seconds a command key. Sending
Break twice was interpreted as a normal break.
On powerpc-press ' alt-print screen (or F13)-<command key>
Print screen (or F13) – <command key> may suffice.
On other-if your know of the key combos for other architectures, please
Let me know so I can add them to this section.
On All-write a character To/proc/sysrq-trigger. e.g.:


echo T >/proc/sysrq-trigger


* What is the ' command ' keys?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' B '-would immediately reboot the system without syncing or unmounting
Your disks.
' C '-would perform a system crash by a NULL pointer dereference.
A CrashDump'll be taken if configured.
' d '-Shows all locks is held.
' E '-Send a SIGTERM to all processes, except for init.
' F '-would call Oom_kill to kill a memory hog process.
' G '-used by KGDB (kernel debugger)
' H '-display Help for actually any other key than those listed
Here is display Help. But ' h ' are easy to remember:-)
' I '-Send a SIGKILL to all processes, except for init.
' J '-forcibly "Just thaw it"-filesystems frozen by the Fifreeze ioctl.
' K '-Secure Access Key (SAK) kills all programs in the current virtual
Console. Note:see important comments below in SAK section.
' L '-shows a stack backtrace for all active CPUs.
' m '-would dump current memory info to your console.
' n '-used to make RT tasks nice-able
' O '-would shut your system off (if configured and supported).
' P '-would dump the current registers and the flags to your console.
' Q '-would dump per CPU lists of all armed hrtimers (and not regular
Timer_list timers) and detailed information about all
Clockevent devices.
' R '-Turns off keyboard raw mode and sets it to XLATE.
' s '-would attempt to sync all mounted filesystems.
' t '-would dump a list of current tasks and their information to your
Console.
' U '-would attempt to remount all mounted filesystems read-only.
' V '-forcefully restores framebuffer console
' V '-causes ETM buffer dump [arm-specific]
' W '-dumps tasks that is in uninterruptable (blocked) state.
' X '-used by Xmon interface on PPC/POWERPC platforms.
' Y '-Show global CPU registers [SPARC-64 specific]
' Z '-Dump the ftrace buffer
' 0 '-' 9 '-sets the console log level, controlling which kernel messages
'll is printed to your console. (' 0 ', for example would make
It so is emergency messages like panics or oopses would
Make it to your console.)
* Okay, why can I use them for?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Well, un ' R ' aw was very handy when your X server or a svgalib program crashes.


sa ' K ' (Secure Access Key) is useful if want to be sure there are no
Trojan program running at console whic H could grab your password
When you would try to login. It'll kill all programs on given console,
thus letting your make sure so the login prompt you see is actually
the One from Init, not some Trojan program.
Important:in Its true form it's not a true SAK like the one in A:important
IMPORTANT:C2 compliant system, and I T should not being mistaken As:important
Important:such.: IMPORTANT
It seems others find it useful as (System attent Ion Key) which is
useful if you want to exit a program, that won't let you switch consoles.
(for example, X or a svgalib program.)
Re ' B ' oot is good if you ' re unable to shut down. But you should also ' S ' ync
and ' U ' mount first.



' C ' rash can used to manually trigger a crashdump when the system is hung.
Note that this just triggers a crash if there is no dump mechanism available.
' S ' ync is great when your system was locked up, it allows your to sync your
Disks and would certainly lessen the chance of data loss and fscking. Note
That's the sync hasn ' t taken place until your see the "OK" and "done" appear
On the screen. (If the kernel is really in strife and you are not ever get the
OK or done message ...)


' U ' mount is basically useful in the same ways as ' S ' ync. I generally ' S ' ync,
' U ' mount, then re ' B ' oot when my system locks. It ' s saved me many a fsck.
Again, the Unmount (remount read-only) hasn ' t taken place until
"OK" and "done" message appear on the screen.
The Loglevels ' 0 '-' 9 ' was useful when your console was being flooded with
Kernel messages you don't want to see. Selecting ' 0 ' would prevent all but
The most urgent kernel messages from reaching your console. (They'll
Still be logged if SYSLOGD/KLOGD is alive, though.)
T ' E ' rm and K ' I ' ll is useful if you have some sort of runaway process you
is unable to kill any other, especially if it's spawning other
Processes.
"' J ' ust thaw it" is useful if your system becomes unresponsive due to a frozen
(probably root) filesystem via the Fifreeze IOCTL.
* Sometimes sysrq seems to get ' stuck ' after using it, what can I do?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
That's happens to me, also. I ' ve found that tapping shift, alt, and control
On both sides of the keyboard, and hitting an invalid SYSRQ sequence again
Would fix the problem. (i.e., something like alt-sysrq-z). Switching to another
Virtual Console (ALT+FN) and then back again should also help.


* I hit SysRq, but nothing seems to happen, what ' s wrong?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There is some keyboards that produce a different keycode for SYSRQ than the
Pre-defined value of KEY_SYSRQ in Include/linux/input.h, or which
Don ' t has a sysrq key at all. In these cases, run ' showkey-s ' to the Find an
Appropriate scancode sequence, and use ' Setkeycodes <sequence> "to map
This sequence to the usual SYSRQ code (e.g., ' setkeycodes e05b 99 '). It ' s
Probably best to put the command in a boot script. Oh, and by the
Exit ' Showkey ' by isn't typing anything for ten seconds.
* I want to add SYSRQ key events to a module, how do does it work?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In order to register a basic function with the table, you must first include
The header ' Include/linux/sysrq.h ', this would define everything else you need.
Next, you must create a sysrq_key_op struct, and populate it with a) the key
handler function you'll use, B) a help_msg string, that'll print when SYSRQ
Prints help, and C) a action_msg string, that would print right before your
Handler is called. Your handler must conform to the prototype in ' Sysrq.h '.

After the sysrq_key_op are created, you can call the kernel function
Register_sysrq_key (int key, struct sysrq_key_op *op_p); This would
Register the operation pointed to by ' op_p ' at table key ' key ',
If the slot in the table is blank. At module unload time, you must call
The function unregister_sysrq_key (int key, struct sysrq_key_op *op_p), which
Would remove the key OP pointed to by ' op_p ' from the key ' key ', if and only if
It is the currently registered in the that slot. The the slot has been
Overwritten since you registered it.


The Magic SYSRQ system works by registering key operations against a key op
Lookup table, which is defined in ' drivers/char/sysrq.c '. This key table has
A number of operations registered into it in compile time, but is mutable,
and 2 functions is exported for interface to it:
Register_sysrq_key and Unregister_sysrq_key.
Of course, never ever leave an invalid pointer in the table. i.e., when
Your module, called Register_sysrq_key () exits, it must call
Unregister_sysrq_key () to the SYSRQ key table entry the IT used.
Null pointers in the table is always safe. :)


If for some reason your feel the need to call the HANDLE_SYSRQ function from
Within a function called by HANDLE_SYSRQ, you must is aware that is in
A lock (you're also in an interrupt handler, which means don ' t sleep!), so
You must call __handle_sysrq_nolock instead.


* When I hits a SYSRQ key combination only the header appears on the console?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SYSRQ output is subject to the same console loglevel control as all
Other console output. This means, if the kernel was booted ' quiet '
As is common on distro kernels the output could not be appear on the actual
Console, even though it'll appear in the DMESG buffer, and is accessible
via the DMESG command and to the consumers of/proc/kmsg. As a specific
Exception the header line from the SYSRQ command was passed to all console
Consumers as if the current loglevel is maximum. If only the header
Is emitted it was almost certain that the kernel loglevel was too low.
Should you require the output of the console channel then you'll need
To temporarily up the console loglevel using alt-sysrq-8 or:

echo 8 >/proc/sysrq-trigger
Remember to return the loglevel to normal after triggering the SYSRQ
Command you is interested in.


* I have more questions, who can I ask?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
and I ' ll answer any questions on the registration system you got, also
Responding as soon as possible.
-crutcher
* Credits

Use of SYSRQ in Linux (Magic key)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.