SysRq Trigger - Linux Kernel Command Tool

Trigger Linux kernel commands with SysRq. Learn to use the SysRq key for debugging, rebooting, syncing, and managing processes.

SysRq Trigger

The "magic SysRq key" is a powerful debugging and recovery tool built into the Linux kernel. It allows you to send commands directly to the kernel via the /proc filesystem, bypassing normal user-space operations. This can be invaluable in situations where the system is unresponsive or experiencing critical issues.

Enabling SysRq

Before you can use the SysRq key, you need to enable it. This typically requires root privileges.

Temporary Activation (Current Session)

To enable SysRq for the current session:

# Activate the magic SysRq option (as root)
echo 1 > /proc/sys/kernel/sysrq

Or using sudo:

echo 1 | sudo tee /proc/sys/kernel/sysrq

Permanent Activation (Across Reboots)

To ensure SysRq is enabled after a reboot, add the following line to your /etc/sysctl.conf file:

kernel.sysrq = 1

After editing the file, you can apply the changes immediately with sudo sysctl -p.

Common SysRq Commands

The SysRq key is used in conjunction with other keys to trigger specific actions. The general syntax when using the keyboard is Alt + SysRq + KEY.

Key Commands and Functions:

  • k – Kills all processes running on the current virtual console.
  • s – Attempts to sync all mounted file systems. This is crucial before rebooting or shutting down to prevent data loss.
  • b – Immediately reboots the system without unmounting partitions or syncing. Use with extreme caution.
  • e – Sends SIGTERM to all processes except init (PID 1). This allows processes to shut down gracefully.
  • m – Outputs current memory information to the console.
  • i – Sends the SIGKILL signal to all processes except init. This forcefully terminates processes.
  • r – Switches the keyboard from raw mode (used by programs like X11) to XLATE mode.
  • t – Outputs a list of current tasks and their information to the console.
  • u – Remounts all mounted file systems in read-only mode.
  • o – Shuts down the system immediately.
  • p – Prints the current registers and flags to the console.
  • 0-9 – Sets the console log level, controlling which kernel messages will be printed to your console.
  • f – Calls the Out-Of-Memory (OOM) killer to terminate a process consuming excessive memory.
  • h – Displays help information. Any key not listed above will typically print the help screen.

Using SysRq via /proc

You can also trigger SysRq commands by writing to /proc/sysrq-trigger:

# Example: Sync filesystems
echo s | sudo tee /proc/sysrq-trigger

# Example: Reboot the system
echo b | sudo tee /proc/sysrq-trigger

External Resources