Emergency reboot/shutdown using SysRq

As you know linux implements some type of mechanism to gracefully shutdown and reboot, this means the daemons are stopping, usually linux stops them one by one, the file cache is synced to disk.

But what sometimes happens is that the system will not reboot or shutdown no mater how many times you issue the shutdown or reboot command.

If the server is close to you, you can always just do a physical reset, but what if it’s far away from you, where you can’t reach it, sometimes it’s not feasible, why if the OpenSSH server crashes and you cannot log in again in the system.

If you ever find yourself in a situation like that, there is another option to force the system to reboot or shutdown.

The magic SysRq key is a key combination understood by the Linux kernel, which allows the user to perform various low-level commands regardless of the system’s state. It is often used to recover from freezes, or to reboot a computer without corrupting the filesystem.

Immediately reboot the system, without unmounting or syncing filesystemsb
Sync all mounted filesystemss
Shut off the systemo
Send the SIGKILL signal to all processes except initi

So if you are in a situation where you cannot reboot or shutdown the server, you can force an immediate reboot by issuing

echo 1 > /proc/sys/kernel/sysrq 
echo b > /proc/sysrq-trigger

If you want you can also force a sync before rebooting by issuing these commands

echo 1 > /proc/sys/kernel/sysrq 
echo s > /proc/sysrq-trigger
echo b > /proc/sysrq-trigger

These are called magic commands, and they’re pretty much synonymous with holding down Alt-SysRq and another key on older keyboards. Dropping 1 into /proc/sys/kernel/sysrq tells the kernel that you want to enable SysRq access (it’s usually disabled). The second command is equivalent to pressing *Alt-SysRq-b on a QWERTY keyboard.

If you want to keep SysRq enabled all the time, you can do that with an entry in your server’s sysctl.conf:

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