How do I use magic sysreq keys on a Mac?

I only recently found out about the magic system request keys. How do I use them from the keyboard on Macs with a Linux-based OS installed, or in other cases where the keyboard doesn't have a SysReq key? Is there a sequence that is equivalent to meta-SysReq, or a way to create one?


Solution 1:

Without a key generating the same scan code, which you don't have, the only option is to echo <char> > /proc/sysrq-trigger, where <char> is the key you'd press along with M-SysRq. (This of course assumes you're running a kernel with magic SysRq compiled into it, and have previously done echo 1 > /proc/sys/kernel/sysrq in /etc/rc.local or similar.)

If you want real magic SysRq functionality, rather than the pale imitation obtained by echoing into random /proc entries, you'll want to find yourself a PC-style USB keyboard with a SysRq key. Linux will interpret the scan code properly no matter what architecture it's running on; the tricky part, in this case, is generating the scan code in the first place, which cannot be done from a standard Mac keyboard.

(On the other hand, it seems probable a kernel could be compiled to recognize a different scan code, such as that of the Mac's eject key, in place of SysRq; I've never felt the urge to downgrade my MBP, so wouldn't know firsthand, but I imagine someone's done it. Or perhaps you will be the first; after all, custom-hacking your kernel is half the fun of running Linux, or so I am given to understand.)

Update: Apparently this can be done from userspace:

Best way to go currently is to install (manually compile) the keyfuzz program and put

echo "786616 99" | keyfuzz -s -d /dev/input/by-id/usb-Apple_Inc._Apple_Internal_Keyboard___Trackpad-event-kbd

in your /etc/profile for instance. This maps Alt-Eject on a typical Apple Macbook Pro (mine is mid-2010, 6,2), to the Alt-SysRq combination.

If you're looking for more of an old-fashioned, "Two-Fisted Tales of Linux" sort of experience, there are some kernel patches linked from that comment thread, which may offer guidance in remapping keycodes at the kernel level.

Solution 2:

This answer also posted here.

After 3 or 4 hrs of hard searching, reading, and trial and error, here's a more thorough and generic answer which should work for any Apple/Mac laptop or keyboard In my particular case, however, I'm using this solution for a 2008 MacBook white running Xubuntu 16.04 LTS.

Many thanks to Bengt Olsson for his excellent instructions here which gave me the necessary start to figure out the missing details.

This solution is done in the user space, and does NOT require recompiling the kernel like some suggestions.

  1. Download source for keyfuzz (http://0pointer.de/lennart/projects/keyfuzz/keyfuzz-0.2.tar.gz), and extract it (I can right-click it and go to "Extract Here" to extract it in my GUI file manager, for instance). To read more about keyfuzz, see here. It is released under the GNU GPL 2.0 or later open source license (yaaay open source!).

    • Dont' use a directory too deep (or with spaces possibly) or else the ./configure command below will fail.
  2. Install dependencies, or else running ./configure below may spit out some errors:

    sudo apt-get update
    sudo apt-get install lynx xmltoman gengetopt
    
  3. Compile and install keyfuzz with the usual commands:

    ./configure
    make
    sudo make install
    
  4. Navigate to /dev/input/by-id and find your Apple keyboard that ends in "-event-kbd":

    cd /dev/input/by-id
    ls
    
    • For me it's "usb-Apple_Computer_Apple_Internal_Keyboard___Trackpad-event-kbd"
  5. Add the following line to your /etc/rc.local file (see note below), to cause this command to run each time your computer boots up and loads this user. Notice we are using the Apple keyboard entry we just found above. This command uses keyfuzz to map F12 or F13 on your Apple keyboard or laptop to the system "Magic SysRq key", for soft reboots.

    • To map F12 to SysRq: 458821 99. Many thanks to "pjjjv" for figuring that out here! https://ubuntuforums.org/archive/index.php/t-762665.html

    • To map F13 to SysRq: 458856 99.

    • Since my MacBook 2008 does not have F13, I am using the F12 option above. Also remember to use your proper keyboard entry we found above. The command I added to rc.local for my case is: echo "458821 99" | keyfuzz -s -d /dev/input/by-id/usb-Apple_Computer_Apple_Internal_Keyboard___Trackpad-event-kbd.

    • Note: on recent Ubuntu versions, /etc/rc.local does not exist by default, but as explained in this answer and this answer, it is used if it 1) exists, 2) is executable by at least the root user, and 3) contains a hashbang at the top, such as #!/bin/sh -e. Be sure it is owned by root and executable (sudo gedit /etc/rc.local OR gedit /etc/rc.local followed by sudo chown root:root rc.local; then sudo chmod u+x /etc/rc.local).

    • Here is my entire /etc/rc.local file, with the appropriate entry:

    #!/bin/sh -e
    #
    # rc.local
    #
    # This script is executed at the end of each multiuser runlevel.
    # Make sure that the script will "exit 0" on success or any other
    # value on error.
    #
    # In order to enable or disable this script just change the execution
    # bits.
    #
    # By default this script does nothing.
    
    echo "458821 99" | keyfuzz -s -d /dev/input/by-id/usb-Apple_Computer_Apple_Internal_Keyboard___Trackpad-event-kbd
    
    exit 0
    
  6. Reboot.

Now I can press Alt+F12 (or you can use Alt+F13 as described above) and issue REISUB to soft-reboot your computer. Works perfectly on my MacBook 2008 laptop running Xubuntu 16.04 LTS.

Note that you can see the results returned by issuing REISUB as you press each key by first jumping to an alternate session with Ctrl+Alt+F1 (F1 through F6 work actually) before issuing the command. To get back to your main X Window session use Ctrl+Alt+F7.

If you get a result that says "This sysrq operation is disabled," that's normal for several of the commands, as they pose a potential security risk. The Boot one should still work, however, allowing you to soft-reboot your computer. Here is some more information on this, from mniess here:

Starting with Ubuntu 12.10 a more fine-grained control of which Magic- SysRq features are enabled is possible. Just edit the file /etc/sysctl.d/10-magic-sysrq.conf.

The file is well documented and tells you exactly how to enable which features. The current default was picked for security purposes and should only be changed if you know why you need to (for security implications see bug #194676).