How to use Magic System Request Keys in Ubuntu Linux

Sponsored Link
The magic SysRq keys are key combinations within the Linux kernel that allows the user to perform various low level commands regardless of the system's state, except during kernel panics or freezes. It is often used to recover from X-Server freezes, or to reboot a computer without corrupting the filesystem.

Restarting Ubuntu safely when it is frozen

If anyone faces a freeze with Ubuntu where you cannot do anything, then this will certainly be helpful if you want to reboot the OS as cleanly as possible without damaging their HDD's or losing their data.

In case of a freeze where you cannot do anything, simply press Alt+PrintScreen+R+E+I+S+U+B, keep in mind that the underlined keys must be kept pressed through the rest of the sequence AND that you will need to keep holding the sequence keys for a small period of time before going to the next one so that their actions can be carried out properly (For example, hold the R key for about 1-2 seconds before moving on to S). If the sequence does not work at first, then increase the time period between each sequence key press and try again.

If anyone requires a good way of remembering the sequence R+E+I+S+U+B, just remember "Reboot Even If System Utterly Broken".

Raw (take control of keyboard back from X), tErminate (kill -15 programs, allowing them to terminate gracefully), kIll (kill -9 unterminated programs), Sync (flush data to disk), Unmount (remount everything read-only), reBoot.

NOTE:- This keystroke does not work in the event of a kernel freeze as the keystroke sequence depends on the kernel in order to unmount and make the required steps before the restart.

Safely shutting down Ubuntu when it is frozen

The key sequence to achieve this does not differ from the one used to restart Ubuntu safely except for the last key. So here it is as follows:-

Alt+PrintScreen+R+S+E+I+U+O, keep in mind that as in the previous sequence, the underlined keys must be kept pressed through the rest of the sequence AND that you will need to keep holding the sequence keys for a small period of time before going to the next one so that their actions can be carried out properly.

If someone requires a good description on what each keystroke here does, there is not much of a difference from the last one, except(Once again),the final key:-

Raw (take control of keyboard back from X), tErminate (kill -15 programs, allowing them to terminate gracefully), kIll (kill -9 unterminated programs), Sync (flush data to disk), Unmount (remount everything read-only), shutdown.

NOTE:- This keystroke does not work in the event of a kernel freeze as the keystroke sequence depends on the kernel in order to unmount and make the required steps before the shutdown.

Brief descriptions about the keys you can use in magic SysRq sequences

0 -- 9 -- sets the console log level, controlling which kernel messages will be printed to your console so that you don't get flooded.

B -- restarts the system without making steps to ensure that the conditions are good for a safe reboot, using this key alone is like doing a cold reboot.

E -- sends SIGTERM to all processes except init. This means that an attempt is done to end the current processes except init, safely, e.g. saving a document.

F -- call oom_kill(Out Of Memory Killer), which will kill a process that is consuming all available memory.

H -- displays help about the SysRq keys on a terminal though in actuality you can use any key except for the ones specified, to display help.

I -- sends SIGKILL to all processes except init. This means that all the processes except for init are killed, any data in processes that are killed will be lost.

K -- kills all processes on the current terminal. It is a bad idea to do this on a console where X is running as the GUI will stop and you can't see what you type, so you will need to switch to a tty after doing the magic SysRq.

L -- sends SIGKILL to all processes, including init. This means that every process including init will be killed, using this key will render your system non-functional and no further magicSysRq keys can be used. So in this case you will have to cold reboot it.

M -- dumps memory info to your console.

O -- shuts down the system via ACPI or in older systems, APM. As in key "B", using this key alone is like a cold reboot(Or in this case, a cold shutdown).

P -- dumps the current registers and flags to your console.

Q -- dumps all timers info to your console.

R -- takes keyboard and mouse control from the X server. This can be useful if the X-Server crashed, you can change to a console and kill the X-Server or check the error log.

NOTE:- The documentation refers to this key's task as "Turns off keyboard raw mode and sets it to XLATE", but we suppose it's safe enough to assume that it takes back control from X.

S -- writes all data from the disc cache to the hard-discs, it is a sync and is necessary to reduce the chances of data corruption.

T -- dumps a list of current tasks and info to your console.

U -- remounts all mounted filesystems read-only. After using this key, you can reboot the system with Alt+SysRq+B without harming the system.

W -- dumps uninterruptable (blocked) state tasks.

Controlling the use of SysRq keys

There are some ways of controlling the use of SysRq keys(i.e. what can be used, enabling or disabling them completely), two ways of doing this are:-

1) Configuring the SysRq keys during kernel compilation itself.

There isn't much here since you can only disable SysRq keys and not actually control or define what you can and can't use. The option you are looking for is:-

MAGIC_SYSRQ

2) Using proc sysrq trigger calls.

This is much more flexible than changing the configuration of the kernel but this has one downside with security which is explained after(since it is very minor). You use the echo command to achieve this for ease but you could also use any normal text editor to achieve this. Now the command is(you will need root permissions):-

echo * > /proc/sys/kernel/sysrqwhere "*" is a number, which can be any one of these:-

0 -- disable sysrq keys completely

1 -- enable all functions of sysrq

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(control the nice level(priority) of Real Time tasks)

So you can define what SysRq keys can be used, and also define whether they are all on or off.

Now for the "downside". For example you disable SysRq keys when you want to stop people(local) from doing key presses and then shutting down or messing up the PC during an important task. Now with configuring the kernel, you can stop SysRq keys from being used at all from the beginning of the boot process right uptil the end, with calling the proc sysrq triggers however, your option only takes place when it is executed(i.e. after the system has booted up) so there is a certain area of vulnerability with calling the triggers whereas there is no such thing in configuring the kernel, some people are that desperate to secure their systems to care about a few seconds, however do not blame me for it.

Something about the magicSysRq keys is that they can be used in any sequence and in any way to achieve the required objective, for example you can just press Alt+SysRq+B to do something like a cold reboot.

Article Credit Goes to here

Sponsored Link

Incoming search terms:

Related posts

17 thoughts on “How to use Magic System Request Keys in Ubuntu Linux

  1. How safe is this? I mean, Would I be able to do it in a console waiting for a user/password with Linux working properly? If so, I would be able to restart any computer with Linux in the planet just by using its keyboard ;)

    Thanks in advance for satisfying my curiosity!

    Jorge

    [Reply]

  2. Please proofread!

    “Raw (take control of keyboard back from X), tErminate (kill -15 programs, allowing them to terminate gracefully), kIll (kill -9 unterminated programs), Sync (flush data to disk), Unmount (remount everything read-only), reBoot.” = Alt+PrintScreen+R+E+I+S+U+B NOT “Alt+PrintScreen+R+S+E+I+U+B”

    [Reply]

  3. Now I’m confused. The article is supposed to be about SysRq key combinations, but the text talks about Alt+PrintScreen. Now this may not make a difference on many keyboards, but I’m typing this on a laptop where SysRq and PrintScreen are on separate, adjacent keys.

    So which is it? SysRq or PrintScreen?

    [Reply]

  4. “keep in mind that the underlined keys must be kept pressed”

    This would be really cool if there were underlined keys to begin with. I presume the Alt and PrntScreen keys were intended to be underlined.

    As Kim said, “please proofread”.

    [Reply]

  5. @ Ben2K

    Um, well then try each and see which works… jeesh, you have two options, it’s not f’ing rocket science here.

    [Reply]

  6. Duh! Why didn’t I thnk of that. Oh. I did. Both produce print-screen behavior. So my system doesn’t seem to respect this feature.

    My point was to try to get the article’s author to be more precise in his labeling.

    [Reply]

  7. @ Ben2K

    Your laptop probably needs you to also press the Function key to enable the SysReq key as that key rather a Print Screen key.

    Laptops make it difficult due to the bad habit of requiring the pressing of that extra key.
    So now you will have to press the Alt – Function – SysReq keys while pressing each of the R E I S U B keys.
    Having a great finger span like guitar or piano players have will be a huge help.
    Or you can use your nose!

    TaZMAn

    [Reply]

  8. Some laptops which require the user to press a Fn (Function key) to access the SysRq / PrntScrn function of a key may not work with these commands, it is a known bug that should be fixed by the next release.

    If you really can’t wait re-bind the key somewhere else (a good way is just to switch PrntScrn and SysRq round).

    [Reply]

  9. BTW, there are some Logitech keyboards that have no system requisite key. That’s why I traded my keyboard to a friend even though this is an m$ keyboard :/

    [Reply]

  10. My laptop requires a “function key” to shift between and . I found the following dance makes the magic-keys work.
    ** press and hold through the following
    ** press and hold
    ** tap then pause 3 seconds
    ** tap then pause 3 seconds
    ** release
    … repeat for all of the you want
    ** release

    When I tap I get a text splash of the same help text as the ‘H’ magic-key.

    When I tap I get a brief string announcing whatever that key is supposed to do.
    Any details generated by the magic-key get written into /var/log/syslog.

    I have never been able to get magic-keys to work while X11 is running — whether X11 is stalled or not. Also, if I have a blinking LED situation (caps-lock, new-moon, etc) the magic-keys don’t seem to work.

    ~~~ 0;-Dan

    [Reply]

  11. Sorry, but some of the text did not make it into my original post.
    =================================================
    My laptop requires a “function key” {Fn} to shift between {PrtSc} and {SysRQ} . I found the following dance makes the magic-keys work.
    ** press and hold {ALT} through the following
    ** press and hold {Fn}
    ** tap {PrtSc/SysRQ} then pause 3 seconds
    ** tap {magic-key> then pause 3 seconds
    ** release {Fn}
    … repeat for all of the you want
    ** release {ALT}

    When I tap {SysRq} I get a text splash of the same help text as the ‘H’ magic-key.

    When I tap {magic-key} I get a brief string announcing whatever that key is supposed to do.
    Any details generated by the magic-key get written into /var/log/syslog.

    I have never been able to get magic-keys to work while X11 is running — whether X11 is stalled or not. Also, if I have a blinking LED situation (caps-lock, new-moon, etc) the magic-keys don’t seem to work.

    ~~~ 0;-Dan

    [Reply]

Leave a comment

Your email address will not be published. Required fields are marked *