SysRQ in Linux

On 2011年02月12日, in linux, tips, by netoearth

magic sysrq当Linux 系统不能正常响应用户请求时, 可以使用SysRq小工具控制Linux.

Sometimes happen that your desktop freeze and you cannot do anything, so the only choice is to unplug the power cord ?
No, Linux has the magic SysRq key that can sync your disk and issue a reboot of your system…and many more things.

What is the magic SysRq key?

It is a ‘magical’ key combo you can hit which the kernel will respond to
regardless of whatever else it is doing, unless it is completely locked up.

网上有道题,问在只有 shell,init、halt、shutdown 等命令都不工作的情况下如何重启系统。答案就是 SysRq,通过 SysRq – B 来完成系统的重启。

How to enable the magic SysRq key

If you use a custom kernel you need to say “yes” to ‘Magic SysRq key (CONFIG_MAGIC_SYSRQ)’ when configuring the kernel. When running a kernel with SysRq compiled in, /proc/sys/kernel/sysrq controls the functions allowed to be invoked via the SysRq key.

On the latest Ubuntu/Debian and Red Hat based distributions, SysRq is compiled but not enabled, so you must enable it at boot by editing the following file:

For Ubuntu – Debian

sudo vi /etc/sysctl.conf


For Red Hat – Fedora – CentOS

sudo vi  /etc/sysctl.config

In both cases you need to add this parameter:

 kernel.sysrq = 1

Using SysRq

Some keyboards may not provide a separate SysRq key. In this case, a separate “Print” key should be present. Under graphical environments (such as Gnome or KDE) ‘Alt’+’PrintScrn/SysRq’+key combination generally only leads to a screenshot to be dumped. To avoid this Print Screen feature the magic SysRq combination should be additionally prefixed by Ctrl, becoming ‘Ctrl’+’Alt’+’SysRq’+key.

Keys combinations

Full documentation is included in the kernel sources in Documentation/sysrq.txt.

These are some of the most useful keys:

‘b’ – Will immediately reboot the system without syncing or unmounting your disks.

‘o’ – Will shut your system off (if configured and supported).

‘s’ – Will attempt to sync all mounted filesystems.

‘u’ – Will attempt to remount all mounted filesystems read-only.

‘p’ – Will dump the current registers and flags to your console.

‘t’ – Will dump a list of current tasks and their information to your console.

‘m’ – Will dump current memory info to your console.

‘e’ – Send a SIGTERM to all processes, except for init.

‘i’ – Send a SIGKILL to all processes, except for init.

‘l’ – Shows a stack backtrace for all active CPUs.

Common use

A common idiom to perform a safe reboot of a Linux computer which has otherwise locked up, is “Raising Elephants Is So Utterly Boring”, “Reboot Even If System Utterly Broken” or simply remembering the word “BUSIER” backwards, is often useful. It stands for:

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

This can prevent a fsck being required on reboot and gives some programs a chance to save emergency backups of unsaved work.

In practice, each command may require a few seconds to complete, especially if feedback is unavailable from the screen due to a freeze or display corruption. For example, sending SIGKILL to processes which have not yet finished terminating can cause data loss.


Tagged with:  

Comments are closed.