Featured image of post SysRq: The Secret Key to Safely Shutting Down a Frozen Linux System

SysRq: The Secret Key to Safely Shutting Down a Frozen Linux System

Sometimes when your Linux system freezes, the magic SysRq key is your only hope for rescue.

We all remembered the classic blue screen of death on Windows, I still get its nightmares too 🥲. And we all escaped to Linux. But over here, everything isn’t sunshine and rainbows either.

Sometimes your system gets locked up, and no key input or tty terminal works. At this time, your only hope left, to gracefully shutdown/reboot the system is Magic SysRq Key.

Magic SysRq Key

It’s a command consisting of key combinations which works at low level regardless of your system state, to recover your computer from freezes or to reboot/shutdown it, without corrupting the filesystem. SysRq aggressively remount the filesystem, terminate/kills the system processes and writes the unwritten data to the disk.

You can power off your system using the button on your PC, but this doesn’t make sure your filesystem/OS gets powered down elegantly.

SysRq key doesn’t work when there is a kernel panic or hardware failure. Honestly, You have got bigger problems than smoothly shutting down your PC.

Requirements

  • Have root/sudo access
  • Kernel compiled with CONFIG_MAGIC_SYSRQ kernel parameter (All mainstream distros has this feature built-in)
  • SysRq or PrtSc key on your QWERTY keyboard
  • On some systems/Laptops, you may need to hold Fn key during invoke process

Configuration

Caution

The various combinations of the SysRq command are possible. We will proceed with 176 as a normal user, that’s enough to effortlessly reboot/shutdown the frozen system. Do your due diligence to enable advanced functionality, as it can arguably weaken system security if it is a multi-access or modem attached system.

See the References section for more study guides on alleged security concerns.

Check what value for SysRq is currently set:

1
cat /proc/sys/kernel/sysrq
Tip

0 means SysRq is disabled, 1 means all features are enabled.

We will set the SysRq value to 176. The value 176 corresponds to specific SysRq functions enabled in the Linux kernel. It is calculated as follows:

  • 128: Allow reboot/poweroff
  • 32: Enable remounting filesystems as read-only
  • 16: Enable sync command

Thus, 128+32+16=176 allows these functionalities while disabling others

Edit this file /etc/sysctl.d/90-sysrq.conf (need sudo privileges) using your favorite text editor and add this line:

1
kernel.sysrq = 176

To apply these changes immediately:

1
sudo sysctl -p /etc/sysctl.d/90-sysrq.conf

To verify, changes have been applied:

1
cat /proc/sys/kernel/sysrq

The output should be 176.

The configuration done this way, will persist across reboots.

SysRq Usage

When your system becomes unresponsive and doesn’t react at all, do this:

  • Press and hold Alt key
  • Press Fn + PrtSc OR SysRq keys, and release both while holding Alt key and press:
    • s to sync
    • r to remount
    • e to terminate the processes
    • i to kill the processes
    • b to reboot

That’s how you can get back your locked/frozen system to life again, without using button.


References

Built with Hugo
Theme Stack designed by Jimmy