DOC & Help Center

Article & Knowledge Base

Safely Reboot Ubuntu When “Freezes”

Have you ever found your Ubuntu freezes? You move your mouse, press keyboard buttons but nothing happens? Well, the last thing we do to solve this problem is pressing the restart button, isn’t it? As we all know, doing “hard reset” frequently will cause damages on hardware especially hard disk, correct me if I’, wrong. So, is there any “proper” way to restart freezing Ubuntu? Yes, there is. We can use “Magic SysRq Key” to perform clean restart on freezing system. The Sysrq key is located near the Scroll Lock key on the keyboard and it’s generally the same as Print Screen key.

Enable all SysRq functions on Linux

Objective

Learn how to enable the SysRq functions, and how to use invoke them by using command keys.

Requirements

  • Root permissions
  • Linux Kernel compiled with the “CONFIG_MAGIC_SYSRQ” option enabled

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

The SysRq key combination can be used to send commands directly to the Linux kernel in some specific circumstances: the kernel will respond to commands sent with command keys immediately, unless it is completely locked. Various command keys achieve specific tasks, and they can be combined to restore the system to a safe state, or to obtain a clean reboot when nothing else works: this is what we can obtain with the reisub sequence.

In this tutorial we will see how to enable all SysRq functions using a standard installation of Ubuntu 18.04 - Bionic Beaver as a base.

The SysRq magic key

The SysRq key combination consists of three keys to be pressed together: ALT + SysRq + command key. You may be wondering what is the SysRq key on your keyboard. Assuming you are using a QWERTY keyboard, the SysRq key corresponds to the print key.

Finally, a command key is a key on the keyboard that when pressed on this special mode, will immediately send a command to the kernel. We will see some of these keys and the functions associated with them in a moment, but before proceeding we must be sure that the kernel we are using has been compiled with the needed option enabled.

The CONFIG_MAGIC_SYSRQ kernel option

As said above, for the SysRq key combination to work, the kernel must have been built with the CONFIG_MAGIC_SYSRQ option enabled. This is usually the case in all major distributions, nonetheless it can be useful to know how to check its state. Here is how we can do it. The first thing we want to know is the version and the name of the kernel we are using. Obtaining this information is very easy, we just run:

$ uname -r
4.13.0-25-generic

As you probably know, the uname command is used to retrieve some system information. In this case we used it with the -r flag, as we only wanted to know about the kernel release.The result of the command has been 4.13.0-25-generic: that is the name of the kernel used by our system. We can now look inside the /boot directory for the corresponding configuration file: this file contains all the options the kernel has been compiled with. We can search for the value used for CONFIG_MAGIC_SYSRQ in it:

$ ls /boot
abi-4.13.0-25-generic     initrd.img-4.13.0-25-generic  memtest86+_multiboot.bin
config-4.13.0-25-generic  memtest86+.bin                System.map-4.13.0-25-generic
grub                      memtest86+.elf                vmlinuz-4.13.0-25-generic

As expected the file is present: config-4.13.0.25-generic is what we are looking for. We now have everything we need, let’s do the check:

$ grep -i CONFIG_MAGIC_SYSRQ /boot/config-4.13.0-25-generic
CONFIG_MAGIC_SYSRQ=y
CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE=0x01b6
CONFIG_MAGIC_SYSRQ_SERIAL=y

As you can see in the first line, the option CONFIG_MAGIC_SYSRQ has y as its value, meaning that it was set as built-in when the kernel was configured. What does the other lines stands for? The CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE option does specify the default functions enabled: the value is expressed in hexadecimal form, in this case 0x01b6 which does correspond to 438 in decimal form.

As we will see later in this tutorial, this value means that most of the functionalities are enabled by default. However most distributions disable their invocation via key combination for for security reasons. The third option is not very important for us now: it is about enabling the SysRq key over serial.

Checking the current SysRq value

The majority of distributions disable the access to some of SysRq functions via key combinations for security reasons (all functionalities are however always available if invoked via /proc/sysrq-trigger with root privileges). To check what are the available functions in our system we can just run:

$ cat /proc/sys/kernel/sysrq
176

The command returned a value of 176. How this value is obtained, and what does it stands for? Every value corresponds to a certain function, As you can see in the list below:

0 - disable sysrq completely
1 - enable all functions of sysrq
2 - enable control of console logging level
4 - enable control of keyboard (SAK, unraw)
8 - enable debugging dumps of processes etc.
16 - enable sync command
32 - enable remount read-only
64 - enable signaling of processes (term, kill, oom-kill)
128 - allow reboot/poweroff
256 - allow nicing of all RT tasks

While a value of 0 disables all SysRq functions and a value of 1 enables all of them, providing values bigger than 1, we can enable the specific ones. As verified above, we have a SysRq value of 176. This is obtained from the sum of 128 (which allows reboot and poweroff) + 32 (ability to remount filesystems in read-only mode) + 16, which enables sync command. In the same way, the value of 438 is obtained from the sum of 2 + 4 + 16 + 32 + 128 + 256, so all the corresponding functions are enabled.

How to change the SysRq value

We now know what the SysRq value is, but how can we change it? To immediately change this value we just have to write the wanted one to the /proc/sys/kernel/sysrq file, by running:

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

This way, the change will be immediately effective but it will not survive a reboot. How to make it persistent? That is very simple. A generic solution, which works in all linux distributions, is to put the value of kernel.sysrq in the /etc/sysctl.d/99-sysctl.conf file:

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

Please notice how we used the >> redirection operator: this will append the text to the file and will not override other settings it could already contain.

The reisub sequence

Of all command key sequences, reisub is probably the most famous. To better remember this sequence, it is often used as an acronym for “raising elephants is so utterly boring”. What does this sequence accomplishes? Holding alt+sysrq key, we proceed pressing the command keys in sequence, and this is what happens:

First of all r switches the keyboard from raw to XLATE mode, then, e sends a SIGTERM signal to all processes, so that they can be closed in a graceful way if possibile. After that we send a SIGKILL signal by pressing i, to terminate remaining process which didn’t respond to the previous signal. With s we try to sync all mounted filesystems and flush all changes from cache to the disk immediately. By using u we remount all filesystems in read only mode, and finally by pressing b, we perform a system reboot.

The reisub sequence can be used in certain situations when the system becomes very unresponsive, and other solutions are not enough to fix things. The command keys composing this sequence are, however, only a subset of the available ones: for a complete list, you can take a look at the SysRq kernel documentation.

Related Content

Export WordPress Posts To Custom Post Type

Introduction Different types of content should be organized, whether that be by taxonomies, custom post types, or both. Older websites tend to use the standard

Scroll to Top
Scroll to Top