Skip to content

Instantly share code, notes, and snippets.

@JPvRiel
Created January 9, 2019 02:26
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JPvRiel/bd9ac73655c9e895872d18641b450514 to your computer and use it in GitHub Desktop.
Save JPvRiel/bd9ac73655c9e895872d18641b450514 to your computer and use it in GitHub Desktop.
SysRq magic keys

sysrq magic keys

This is useful for trying to recover from an unresponsive system. However, by default, Ubuntu disables the key to gain raw keyboard access.\

Sometimes part of the issue is that X/Wayland display servers have crashed due to bad graphics drivers and keyboard input isn't processed anymore, whereby changing to another console, e.g. ctrl + alt + F1 doesn't work. Since the default disables keyboard control, alt + sysrq + r, can't help salvage the situation. Hence the suggestion to at least enable system request raw keyboard control when trying to recover a system.

Ubuntu doesn't enable control of keyboard by default

Ubuntu 16.04 LTS default value:

cat /proc/sys/kernel/sysrq
176

Sysrq bit encoded values:

Bit Bit value Decimal Function Default Wanted
0000 0000 0 disable sysrq completely
1 0000 0001 1 enable all functions of sysrq
2 0000 0010 2 enable control of console logging level y
3 0000 0100 4 enable control of keyboard (SAK, unraw) y
4 0000 1000 8 enable debugging dumps of processes etc.
5 0001 0000 16 enable sync command y
6 0010 0000 32 enable remount read-only y
7 0100 0000 64 enable signalling of processes (term, kill, oom-kill) y
8 1000 0000 128 allow reboot/poweroff y
9 0001 0000 0000 256 allow nicing of all RT tasks

176 default decoded = 1011 0000 = 128 + 32 + 16. Notice 3rd bit (decimal 4) isn't included.

The default was set via /etc/sysctl.d/10-magic-sysrq.conf

Note, some functions like being able to dump memory to a console (m sysrq key) could expose sensitive data, so it's a low-risk local access issue.

Validate sysrq key is operational

Swith to a console, login and try invoke sysreq help via alt + sysrq 8 and then alt + sysrq + h. Something like SysRq: HELP should emit to the console if it's working.

Run showkey -k while attempting the sysreq.

Remove gnome alt + printscreen shortcut

Gnome shell uses this shorcut to take a screenshot of the currently active window and save it to a PNG file in pictures. Rather remap or disble this shortcut to avoid conflicts with SysRq, given alt + printscreen is the

Also, `ctrl+alt+print screen

Override and enable keyboard control (r sysrq key)

I chose to have some extra features enabled:

sudo bash -c "echo 'kernel.sysrq = 246' > /etc/sysctl.d/98-magic-sysrq-cust.conf"

246 = 11110110 which added keyboard control, signalling and debugging dumps.

r e i s u b sequence

The sysrq key is typically mapped to the print-screen key and alt + sysrq (print-screen) should invoke it. One alt + sysrq is pressed in combination, they can be released before pressing the system command key that follows.

When pressing ther e i s u b keys, take a fair time to pause between the e and i to allow all processes time to gracefully exit where possible:

  • r: Turns off keyboard raw mode and sets it to XLATE.
  • e: Send a SIGTERM to all processes, except for init.
  • i: Send a SIGKILL to all processes, except for init.
  • s: Will attempt to sync all mounted filesystems.
  • u: Will attempt to remount all mounted filesystems read-only.
  • b: Will immediately reboot the system without syncing or unmounting your disks.

r e i s u b idiom

"Reboot Even If System Utterly Broken" ("BUSIER" backwards).

Recovery alternatives to the full r e i u s b sequence

If after requesting raw access via r doesn't seem to allow switching consoles, as per kernel docs, k might help:

useful when you want to exit a program that will not let you switch consoles. (For example, X or a svgalib program.)

The description for k:

Secure Access Key (SAK) Kills all programs on the current virtual console.

Another key to try if X/Wayland/GPU drivers have caused a non-responsive is v:

Forcefully restores framebuffer console.

In some cases a full reboot via r e i s u b might be unnecessary if alt + sysrq and r allows switching and logging into another console (e.g. ctrl + alt + F1). Commands can be run to kill and restart display managers or use systemd to try reboot instead of sending SIGTERM to every process at once.

Also note, keys 0-9:

Sets the console log level, controlling which kernel messages will be printed to your console. (0, for example would make it so that only emergency messages like PANICs or OOPSes would make it to your console.)

alt + sysrq and 8 was recommend because many distros boot in quiet mode not showing kernel console output.

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment