SEARCH

How-To Geek

Use the Magic SysRq Key on Linux to Fix Frozen X Servers, Cleanly Reboot, and Run Other Low-Level Commands

sysrq key

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:

cat /proc/sys/kernel/sysrq

If you see a “1”, the magic SysRq key is enabled.

image

If you see a “0”, you’ll have to enable it yourself by running the following command as root:

echo “1” > /proc/sys/kernel/sysrq

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:

reisub

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.

More Commands

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 sysrq.txt file included with the Linux kernel.

Chris Hoffman is a technology writer and all-around computer geek. He's as at home using the Linux terminal as he is digging into the Windows registry. Connect with him on Google+.

  • Published 07/16/12

Comments (18)

  1. Green Star

    This is really informative article, I encounter many times my xserver just froze, some how many times I was able to switch to other console using Ctrl+Alt+{any function key but not the F7} and reboot from there, is there any way just to restart the xserver from other console instead of rebooting whole system? I am running Ubuntu 12.04-64

  2. Me

    If that button was on Windows, it would be flame as being a sign that Windows crash burn all the time. And now suddenly, I see a comment about Linux crashing, and enough that it has a button for that situation. Linux wasn’t suppossed to be better than Windows?, meaning never crash…I like Linux, but really one must wonder? Blue button of death….

    Am I going to get flamed now…..

  3. BigJohn

    Huh?
    When you learn more about computers and their OS’s, you will see that your statement makes no sense.

  4. Citrus Rain

    I’ll have to try and remember this next time GNOME 3 is frozen upon login.

  5. Me

    @BigJohn

    Don’t worry I have seen more than you think….

    Everytime you hear things about Linux in forum and all, it never crash, so what is this button used for then…

    My point, you may want to think about twice when you say Linux is Iron man. It too crash. That’s all.

    (English not my first language, huh!.)

  6. fubeca6

    @Green Star – I believe that something along the lines of “#service Xorg restart” might do the trick (for Ubuntu) – I can’t verify though, I’m at work and stuck on a windows box. Another think you can do is “#top
    ” and get the process ID of the X server and manually kill it with “#killall [process_id]“. You’d then have to restart the process, either with “#service _______ start” or “#/etc/init.d/ . . . restart”

    Again, I can’t be 100% as I’m not at my Linux machine, but someone else might be able to expound/correct on this.

  7. Sudo Bash

    @Me

    The Linux *Kernel* almost never crashes; however, I have run many stupid third-party applications which
    have locked up my computer. The kernel was always fine in these cases so these keys will work.

    The term “Linux” refers only to the kernel. Everything else on Ubuntu, Mint, etc is just a program to run on the Linux kernel. And those may crash often.

  8. Lew

    cat /proc/sys/kernel/sysrq
    438

    is what I get.
    What does this mean?

  9. Vincent

    @Me
    All OS’es get so much flack for any crashes….
    For example that multi-OS malware that was discovered a few days back exploits Mac, Windows, and Linux, People think it’s a system vulnerability, but it actually exploits a hole in the Java Runtime.

  10. fengshaun

    Well, this’ll come in handy when flash freezes everything…for the 100th time! :)

  11. Anonymous

    @Lew
    438 means that some of the sysrq functions are disabled.

    To temporarily enable all of the sysrq functions, use:

    echo “1″ >/proc/sys/kernel/sysrq

    To make it permanent, add the line:

    kernel.sysrq=1

    to

    /etc/sysctl.conf

  12. Charles Anderson

    Nice article.

  13. jacob

    A couple of issues with this otherwise informative article:
    1. It does not tell me how to configure the kernel to boot with this capability enabled. (I just went through a quick scan of CompizConfig to look for it. Not there.)

    2. Even when I sudo to root (sudo su -) and have the # prompt, if I try that echo command I get:
    root@maxwell:~# echo 1 >/proc/sys/kernel/sysreq
    -su: /proc/sys/kernel/sysreq: No such file or directory
    So I can’t simply create a new kernel parameter this way. And, of course, including it in a startup shell script guarantees a boot-time error. (I don’t know if that will freeze the boot process and I don’t want to find out.!)

    Anonymous (on July 17) solved the problem already by suggesting we add that line to etc/sysctl.con directly.

    Or, perhaps (as suggested in /etc/config.d/README) create a file named something like 60-extras.conf to contain that line

  14. ds

    Maybe the screen should turn blue when it locks up.

  15. anonymous

    > Maybe the screen should turn blue when it locks up.

    It will, if the value of /proc/sys/kernel/sysrq is 0x816b00b5

  16. anonymous

    I think it is easier to remember busier backwards as the mnemonic…

  17. Kurt

    Did not work on my system. Ubuntu 12.04, 64 bit, kernel linux 3.2.0-26-generic, GNOME 3.4.1. Don’t see unity listed though.

    cat /proc/sys/kernel/sysrq returns a 1.

    Print Screen and CNTRL Print Screen take snapshots of the screen. ALT Print Screen does nothing.

  18. Bill777

    I note that some distros have taken to using the right-side Alt instead
    AltGr+SysRq+whatever rather than Alt+SysRq+whatever
    It requires less gymnasticism with the fingers.

    Yet others have have gone completely out of their minds & taken to disabling the whole Magic SysRq thing altogether. So, no controlled shutdown, just potential corruption/loss of data.

    There are many other “magic SysRq” commands; Wikipedia has a full list.

    Alt+SysRq+f is perhaps most useful & conservative, it issues an OOM (out of memory) which identifies the highest memory user & kills it (or sacrificially kills a sub-process) to release memory.
    If Firefox freezes or slows, this will often kill child process plugin-container (the Adobe Crash Player). Once Flashplayer had been stopped, very often you can have your computer back.
    (The technical messages are a little alarming! Do not listen to the voices!)

    A downside of this is that if the biggest process is something rather more essential to the system, you may have trouble recovering. Nothing much is lost though, so long as you can follow the REISUB shutdown.

    Beware! some commands seem to take for ever, for example J, thawfilesystem; If you try that, I suggest you return the next day to see if it has finished, otherwise it’s the OFF button!

    It is easier to follow what is going on if one switches to a console (Ctl+Alt+F1 say), when it becomes more obvious why one should wait for completion between the REISUB commands.
    One also sees the feed back from L (backtrace) or M (memory usage) etc., and a smattering of error reports.

    Controlled shutdown is however a last resort (just before press and hold the off button, or yanking the power cord!).

    What one should really do is get to a console (Ctrl+Al+F1 say, then often Ctrl+Alt+F8 to get back to GUI screen) and use “top” or “htop” (“q” to exit) to see what is going on & proceed from there.

    As a Firefox user, my own first resort when a system goes slow is to get a terminal/console &
    pkill plugin-container
    (or equivalent)
    which will get rid of notorious memory hog & lockup-monger Adobe Flashplayer. Then you can have your computer back.

    If I can’t get the cursor to move, Alt+SysRq+f will invoke OOM & probably do the same thing.
    This is perhaps most useful & conservative, it issues an OOM (out of memory) which identifies the highest memory user & kills it (or sacrificially kills a sub-process) to release memory.
    If Firefox freezes or slows, this will often kill child process plugin-container (the Adobe Crash Player).
    (The technical messages are a little alarming! Do not listen to the voices!)

    A downside of this is that if the biggest process is something rather more essential to the system, you may have trouble recovering. Nothing much is lost though, so long as you can follow the REISUB shutdown.

    To get everything working again if there are screen problems, next resort may be to restart the screen handler. On many distros, Ctrl+F2 will bring up a command line.
    Type command r to restart the screen handler.

    xkill is another useful command-line program; it generates a special cursor which will kill a window (and backbround process) which won’t go away.

    A little more drastic is to restart the X server, which will often rescue a session.
    That’s usually Ctl+ALt+Backspace, unless some idiot has disabled it for some reason which they thought was a good idea at the time.
    It’s possible to use a console too, but best you go look that up elsewhere; you’ll need to know the name of the X server for the distro you are using, run level, security & such.

    Then you can set about saving data & restarting whatever you were doing.

Enter Your Email Here to Get Access for Free:

Go check your email!