≡ Menu

Safe Reboot Of Linux Using Magic SysRq Key

SysRq Key
Photo courtesy of KCIvey

This is a guest post written by Lakshmanan G.

If you are working on kernel development, or device drivers, or running a code that could cause kernel panic, SysRq key will be very valuable. The magic SysRq key is a key combination in 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. The key combination consists of Alt+SysRq+commandkey. In many systems the SysRq key is the printscreen key.

First, you need to enable the SysRq key, as shown below.

echo "1" > /proc/sys/kernel/sysrq

List of SysRq Command Keys

Following are the command keys available for Alt+SysRq+commandkey.

  • ‘k’ – Kills all the process running on the current virtual console.
  • ‘s’ – This will attempt to sync all the mounted file system.
  • ‘b’ – Immediately reboot the system, without unmounting partitions or syncing.
  • ‘e’ – Sends SIGTERM to all process except init.
  • ‘m’ – Output current memory information to the console.
  • ‘i’ – Send the SIGKILL signal to all processes except init
  • ‘r’ – Switch the keyboard from raw mode (the mode used by programs such as X11), to XLATE mode.
  • ‘s’ – sync all mounted file system.
  • ‘t’ – Output a list of current tasks and their information to the console.
  • ‘u’ – Remount all mounted filesystems in readonly mode.
  • ‘o’ – Shutdown the system immediately.
  • ‘p’ – Print the current registers and flags to the console.
  • ‘0-9’ – Sets the console log level, controlling which kernel messages will be printed to your console.
  • ‘f’ – Will call oom_kill to kill process which takes more memory.
  • ‘h’ – Used to display the help. But any other keys than the above listed will print help.

We can also do this by echoing the keys to the /proc/sysrq-trigger file. For example, to re-boot a system you can perform the following.

echo "b" > /proc/sysrq-trigger

Perform a Safe reboot of Linux using Magic SysRq Key

To perform a safe reboot of a Linux computer which hangs up, do the following. This will avoid the fsck during the next re-booting. i.e Press Alt+SysRq+letter highlighted below.

  • unRaw (take control of keyboard back from X11,
  • tErminate (send SIGTERM to all processes, allowing them to terminate gracefully),
  • kIll (send SIGILL to all processes, forcing them to terminate immediately),
  • Sync (flush data to disk),
  • Unmount (remount all filesystems read-only),
  • reBoot.

This article was written by Lakshmanan G. He is working in bk Systems (p) Ltd, and interested in contributing to the open source. The Geek Stuff welcomes your tips and guest articles.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • sathiya December 15, 2008, 2:34 am

    Great Article…..
    Now when a system hangs, no need to worry much about the crashes…

    What is SysRq means ??

  • Lakshmanan G. December 15, 2008, 4:05 am

    Thanks for your appreciation.
    System Request is the abbreviation for SysRq.

  • Scott January 12, 2009, 4:35 pm

    How can I fake a kernel panic, in order to test that the settings I’ve made work correctly when a real kernel panic comes along?

  • DarwinSurvivor January 17, 2011, 6:15 am

    You have a slight error in you list of keys to hit for a reboot. The “i” should be bolded in “kill”, not the “k”.

    An easy way to remember is the mnemonic “Raising Elephants Is So Utterly Boring”

  • diptanu February 11, 2011, 6:56 am

    I think (may be wrong also), please correct.
    1st enable the sysrq to 1 in proc/sys/kernel
    2nd press al+sysrq+R (hold for sometime)
    then press these buttons one by one (hold for some time each button) s e i u b

  • jack December 22, 2012, 12:51 am

    HI. Could you please explain the sequence once again. do we need to press the keys in small or capital letters?

  • branko August 31, 2013, 2:09 pm

    “‘h’ – Used to display the help. But any other keys than the above listed will print help.”
    I don’t think that ‘c’ key will print help.. 🙂