Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Tutorials & Tips
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Tutorials & Tips
The place to find Ubuntu related Tips & Tricks.

 
Thread Tools Display Modes
Old November 19th, 2007   #1
PmDematagoda
The Free Multi-Distro Tux
 
PmDematagoda's Avatar
 
Join Date: Aug 2007
Location: Kottawa, Sri Lanka
Beans: 7,299
Ubuntu 8.04 Hardy Heron
Send a message via MSN to PmDematagoda Send a message via Skype™ to PmDematagoda
MagicSysRq keys for assistance with Ubuntu troubles

This guide consists of four parts:-
1) Introduction
2) Restarting Ubuntu safely when it is frozen.
3) Shutting down Ubuntu safely when it is frozen.
4) Brief descriptions about the keys you can use in magic SysRq sequences.
5) Controlling the use of SysRq keys.


1) Introduction

First off I would like to thank the creators of the different Linux Kernel documentation, tutorials and how-tos that made this possible, I would also like to thank pauper and Vadi for their help in this(in someway).

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.


2) 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+SysRq+R+S+E+I+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+S+E+I+U+B, just remember "Raising Skinny Elephants Is Utterly Boring".

If someone requires a good description on what each keystroke does, here is something rather good:-

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.


3) 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+SysRq+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.


4) 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 I 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.


5) 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:-
Code:
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):-
Code:
echo * > /proc/sys/kernel/sysrq
where "*" 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. Also, there may be more ways of controlling the SysRq keys, but as of now the above two are the only ways I know of.

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(very obscure, I know). 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.

One more NOTE:- Even though this guide is made for Ubuntu, the magic SysRq keys can be used on other Linux distributions with little/no alterations.
__________________
Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

C == seriously fast == FTW!

Last edited by PmDematagoda; December 14th, 2008 at 07:41 AM..
PmDematagoda is offline   Reply With Quote
Old November 19th, 2007   #2
davidjmayo
Ubuntu Extra Shot
 
Join Date: Jun 2007
Location: SE Asia (expat)
Beans: 338
Kubuntu 7.10 Gutsy Gibbon
Re: The cleanest way of restarting Ubuntu when it is frozen

Just in case anyone wants to know more about what this does
see here http://en.wikipedia.org/wiki/Magic_SysRq_key
davidjmayo is offline   Reply With Quote
Old November 20th, 2007   #3
stinger30au
Chocolate Ubuntu Mocha Blend
 
Join Date: May 2007
Location: Townsville, Australia
Beans: 1,764
Ubuntu 9.10 Karmic Koala
Re: The cleanest way of restarting Ubuntu when it is frozen

man!!!


hate to try this with small hands!!!!

ou almost need three hands to make it to press all the keys needed!

thanks for the info
__________________
Ubuntu user # 16304 www.nocleanfeed.com
If someone asks you to sudo rm -rf anything, don't do it, and don't run any command with rm in it unless you know exactly what you're doing.things i have learnt changing from Xp pro to Ubuntu
stinger30au is offline   Reply With Quote
Old November 20th, 2007   #4
popch
Skinny Soy Caramel Ubuntu
 
Join Date: Jul 2007
My beans are hidden!
Ubuntu 8.04 Hardy Heron
Re: The cleanest way of restarting Ubuntu when it is frozen

I usually use Alt+Ctrl+Backspace because it's usually the GUI which is hung. In most cases, recovery is complete enough for me to continue working.

I did not know about the method in the OP but would have needed it only once or twice in a long time. Thanks anyway for posting here.
__________________
ch
In Switzerland we make it other
with apologies to Gerard Hoffnung

popch is offline   Reply With Quote
Old November 20th, 2007   #5
wieman01
Himbeer Brombeer Macchiato
 
wieman01's Avatar
 
Join Date: May 2006
Location: 100acrewood
Beans: 7,262
Kubuntu 9.10 Karmic Koala
Re: The cleanest way of restarting Ubuntu when it is frozen

Interesting post... I will most definitely try it out next time my system freezes (sadly just happened this morning). Thanks, man.
wieman01 is offline   Reply With Quote
Old November 20th, 2007   #6
Banacek
Just Give Me the Beans!
 
Join Date: Oct 2007
Beans: 55
Re: The cleanest way of restarting Ubuntu when it is frozen

We seem to have enough sticky threads already. Rather than make this one sticky it would be better to incorporate the posts with the key points of this thread into a thread that's already sticky. 8u)
Banacek is offline   Reply With Quote
Old November 20th, 2007   #7
MrKlean
Quad Shot of Ubuntu
 
Join Date: May 2006
Beans: 448
Re: The cleanest way of restarting Ubuntu when it is frozen

Stinger, only Alt-Print Screen have to be held down LOL! The rest are press and release !! FYI ; )
MrKlean is offline   Reply With Quote
Old November 20th, 2007   #8
Vadi
Happy user
 
Vadi's Avatar
 
Join Date: May 2007
Location: Toronto, Canada
Beans: 4,430
Ubuntu Karmic Koala (testing)
Send a message via AIM to Vadi
Re: The cleanest way of restarting Ubuntu when it is frozen

I was also told you should wait ~5 seconds after each command just to make sure it gets time to do it's stuff.
Vadi is offline   Reply With Quote
Old November 20th, 2007   #9
PmDematagoda
The Free Multi-Distro Tux
 
PmDematagoda's Avatar
 
Join Date: Aug 2007
Location: Kottawa, Sri Lanka
Beans: 7,299
Ubuntu 8.04 Hardy Heron
Send a message via MSN to PmDematagoda Send a message via Skype™ to PmDematagoda
Re: The cleanest way of restarting Ubuntu when it is frozen

Quote:
Originally Posted by Vadi View Post
I was also told you should wait ~5 seconds after each command just to make sure it gets time to do it's stuff.
Thank you very much Vadi, I forgot about that part and I included it now.
__________________
Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

C == seriously fast == FTW!
PmDematagoda is offline   Reply With Quote
Old March 18th, 2008   #10
Marcolin
First Cup of Ubuntu
 
Join Date: Mar 2008
Location: Boise
Beans: 11
Hardy Heron (Ubuntu Development)
Re: The cleanest way of restarting Ubuntu when it is frozen

My system was completely locked and I tried this system. I just saw my screen get all garbled and it didn't reboot. Then I found this post:

Quote:
Originally Posted by Vadi View Post
I was also told you should wait ~5 seconds after each command just to make sure it gets time to do it's stuff.
I tried that and it worked! Thanks Vadi!

EDIT: I should also note that on my laptop, "prnt scrn" is a set as a secondary use. I had to hold down ALT+FN+Prnt Scrn to get it to work.
__________________
HP Pavilion dv6000 AMD Turion64 X2 2.0Ghz 1.5GB RAM Nvidia GeForce Go 7200 Broadcom Wireless 4312

Last edited by Marcolin; March 18th, 2008 at 04:27 PM.. Reason: Updating key sequence
Marcolin is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:42 AM.


vBulletin ©2000 - 2010, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. lingonberry