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
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:
|
|
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:
|
|
To apply these changes immediately:
|
|
To verify, changes have been applied:
|
|
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
- What is SysRq Key? — A detailed Wikipedia article about all the functions and commands.
- How to enable all SysRq functions on Linux — A pretty good tutorial which lists down all the functions.
- How can the Magic SysRq key be dangerous for Linux users? — Some people do not consider SysRq to be a security concern.
- Restricting the use of the Magic SysRq key — Debian docs advise restricting it, if the system is modem connected, has an unauthorized access possibility, or in a virtualized environment.
- Linux Magic System Request Key Hacks — Kernel docs about SysRq