Back to homepage

GNU/Linux 上 Sysrq 的用法

nios34于 March 3, 2021 发表在技术博文


你可能注意到你的键盘上的 PrintScreen 键也写着 SysRq,让我们来看看它的是干啥的。

内核配置

Linux 内核将 Sysrq 的功能分割出来了,你可以用这个命令查看它的状态:

# grep "CONFIG_MAGIC_SYSRQ" /boot/config-*

如果写着 Y 那就说明启用了(一般的发行版都是启用的),否则使用下面的方法:

临时

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

永久

# echo "kernel.sysrq = 1" >> /etc/sysctl.conf

文档

'r' - Turns off keyboard raw mode and sets it to XLATE.

'k' - Secure Access Key (SAK) Kills all programs on the current virtual
console. NOTE: See important comments below in SAK section.

'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.

'0'-'9' - Sets the console log level, controlling which kernel messages
will be printed to your console. ('0', for example would make
it so that only emergency messages like PANICs or OOPSes would
make it to your console.)

'e' - Send a SIGTERM to all processes, except for init.

'i' - Send a SIGKILL to all processes, except for init.

'l' - Send a SIGKILL to all processes, INCLUDING init. (Your system
will be non-functional after this.)

'h' - Will display help ( actually any other key than those listed
above will display help. but 'h' is easy to remember :-)

这就是 Magic Sysrq 的所有指令键。

如何按出来呢?举个例子:要重启系统的话,使用 Alt + Sysrq + B。

用处

你一定遇到过死机的情况,有些情况下 TTY 也救不你。这时就需要安全的重启,并且这操作要不会被其他程序干扰。

你也许会说:很简单,Alt + Sysrq + B。

额,然后你的数据就没了。所以,通用的做法是:

按顺序按下:R E I S U B

按下一个时等一会再按下一个,否则内核没法处理完成你的指令。

最后在紧急同步以后,你的计算机会重启。操作完成。 :-)