Pages

Friday, September 14, 2012

Magic SysRq


On A System that is very slow but not dead, there is a way in linux to send commands to the kernel directlty.This can be done using the SysRq.This means even if you cant login , you can either recover from the system or either re-boot in such way that causes less risk.

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.

The SysRq(usually PrintScreen) usually is not enabled by default. To Find out whether it is enabled or not use

[root@vx111a usr]# cat /proc/sys/kernel/sysrq
0

This is not enabled, since '0'.To enable this we can use

[root@vx111a usr]# echo 1 > /proc/sys/kernel/sysrq
[root@vx111a usr]# cat /proc/sys/kernel/sysrq
1

There are 2 ways to use this,

From Console: We can hold 'Alt + SysRq' key along with one of the letters to make SysRq request (in the next section).

From Remote Machine : we can echo letters to the /proc/sysrq-trigger to make SysRq requests (in the next section),but instead of seeing output on the screen we see them in /var/log/message file.

Some Examples

For SysRq Help

[root@vx111a usr]# echo h > /proc/sysrq-trigger
[root@vx111a usr]# cat /var/log/messages

Sep 14 15:40:41 vx111a kernel: SysRq : HELP : loglevel0-8 reBoot Crashdump tErm Full kIll thaw-filesystems(J) saK showMem Nice powerOff showPc unRaw Sync showTasks Unmount shoWcpus

Echoing a letter to the /proc/sysrq-trigger is a good way if we dont want to see the out put on the screen.they go to the /var/log/messages.

See all the Process Running In kernel

[root@vx111a usr]# echo t > /proc/sysrq-trigger

When we check the /var/log/messages, you can see all the process details that are running in the kernel.

See all the Memory related Information

[root@vx111a usr]# echo m > /proc/sysrq-trigger

List of SysRq Command Keys

Here are the list command keys available for SysRq

  • ‘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 un mounting 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),
  •  ‘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.



Happy Learning , More To Come