On Linux, the magic SysRq key can send commands directly to the Linux kernel. You can use it to recover from freezes or cleanly restart your system, even if nothing appears to be responding.
The magic SysRq key is implemented as part of Linux’s keyboard driver – it will work as long as the Linux kernel is still running. Only a kernel panic should disable this key combination.
Image Credit: solylunafamilia on Flickr
Using the Magic SysRq Key
The SysRq key is located near the Scroll Lock key on your keyboard – the Sys Rq key is generally the same as your Print Screen key. The “magic” SysRq combination is Alt+SysRq.
To use this key combination, your Linux kernel must have been compiled with the CONFIG_MAGIC_SYSRQ compile option – most Linux distributions will have this enabled by default. Assuming it’s compiled into your kernel, it can be enabled or disabled on a running system by changing the value of /proc/sys/kernel/sysrq. To check if it’s enabled, run the following command:
If you see a “1”, all functions of the magic SysRq key are enabled. A greater number indicates only certain functions are enabled—see the official Linux kernel help for more information.
If you see a “0”, you can enable all magic SysRq options yourself by running the following command:
sudo echo “1” > /proc/sys/kernel/sysrq
On Ubuntu, the magic SysRq options are set at boot according to the contents of the /etc/sysctl.d/10-magic-sysrq.conf file. Edit this file to choose your desired values.
Dealing With a Frozen X Server
If your X server (the program that runs your graphical desktop) freezes, you may find yourself unable to use your system. There are a few magic SysRq commands that can help:
- Alt + SysRq + r – Takes the keyboard out of raw mode, taking control away from the X server. After running this combination, you should be able to use the Ctrl + Alt + F1 (and other F-key) keyboard shortcuts to switch to another console and restart X.
- Alt + SysRq + k – Kills all programs on the current virtual console, including X. You can also enable the Ctrl+Alt+Backspace keyboard shortcut in Ubuntu’s keyboard preferences to restart your X server – however, this shortcut is implemented by the X server and may not always work.
Cleanly Restarting Your System
Used in sequence, some of these actions can be used to cleanly end processes, flush data to disk, unmount all file systems, and restart your computer. To perform this process, press and hold the Alt + SysRq key combination and – while holding the Alt and SysRq keys down — type the following keys in order, pausing for several seconds in between each key:
The mnemonic “Raising Elephants Is So Utterly Boring” is often used to remember this sequence. Here’s what each key does:
- r – Puts the keyboard into raw mode, taking control of it away from the X server.
- e – Sends the terminate signal to all processes, asking them to end gracefully.
- i – Sends the kill signal to all processes, forcing them to end immediately.
- s – Flushes data from your cache to disk.
- u – Remounts all file systems read-only.
- b – Reboots your computer.
Here are some other actions you can perform with the magic SysRq key. To perform an action, press and hold the Alt + SysRq keys while typing the letter:
- n – Resets the nice level (priority) of all high and realtime priority processes.
- f – Calls oom_kill, which will kill a memory-hogging process.
- o – Shuts off the computer.
There are other actions, but they may be more useful to developers than the average Linux geek. For more information on the magic SysRq key – and a full list of actions you can perform – consult the official magic SysRq documentation for the Linux kernel.